createAttentionStore
Observe browser focus and visibility for your existing interface.
Create and register app elements
AttentionStore reads browser focus and visibility. It does not inspect a screen reader's cursor or move focus.
import { createAttentionStore } from "@generative-a11y/dom";
const attention = createAttentionStore({ document });
const unregisterComposer = attention.registerComposer(composer);
const unregisterConversation = attention.registerConversation(conversation);
const unregisterNewest = attention.registerNewestResponse(latestResponse);
const unsubscribe = attention.subscribe(() => {
console.log(attention.getSnapshot());
});
// Call when the owning surface is removed.
function disposeAttention() {
unsubscribe();
unregisterNewest();
unregisterConversation();
unregisterComposer();
attention.dispose();
}Register the elements your app already owns. Each registration and subscription
returns a cleanup function; dispose() releases the store and its browser
listeners.
Prop
Type
AttentionSnapshot
A field stays unknown when the browser cannot provide enough information. AttentionStore never guesses.
| Field | Values | Browser signal |
|---|---|---|
| visibility | visible, hidden, unknown | Document visibility |
| windowFocus | focused, blurred, unknown | Window focus |
| focusArea | composer, conversation, elsewhere, none, unknown | Deep active element |
| newestResponse | visible, outside, unobserved, unknown | Intersection observer |
| mode | foreground, background, reading-history, away, unknown | Derived from the fields above |
bindAttention
bindAttention({ runtime, attentionStore }) explicitly forwards initial and
changed observations to the core runtime. Both inputs are borrowed. dispose()
unsubscribes and sends attention.changed with mode unknown, without
disposing either input. Competing bindings for one runtime throw. Core filtering
still requires policy.attention.enabled.
Actual newest-response intersection contributes browser evidence; this cannot identify a screen reader virtual cursor. Read the attention guide.