Components / Foundations
LiveRegion
A hidden live region for screen reader announcements that belong to one component: "Copied", "Moved to position 3 of 8", "Zoom 150%", "3 of 12". It is a foundation: it has no visual output. For announcements from anywhere in an app, use Announcer and useAnnounce(), which speak through LiveRegions near the root.
Who uses itLink to "Who uses it"
Announcer, CopyButton, DocumentViewport, FileDrop, HotkeyField, PanZoomCanvas, SearchField, SortableList, Tabs, TagInput and VirtualList.
When to use it directlyLink to "When to use it directly"
When a component you build announces the outcome of its own actions, or a value it shows (a match count, a zoom level), and must work without an Announcer above it, or speaks elements (a Kbd) rather than strings.
Use something else when the message comes from app code rather than a component: Announcer. When the message is visible text: Notice or Toast, which are live regions themselves.
UsageLink to "Usage"
import { LiveRegion, useLiveMessage } from "@arshad-shah/cynosure";
function Saver() {
const [message, say] = useLiveMessage();
return (
<>
<Button onClick={() => save().then(() => say("Draft saved"))}>Save</Button>
<LiveRegion {...message} />
</>
);
}
// Following a value: announced when the words change, not on every render.
<LiveRegion>{`${count} results`}</LiveRegion>PropsLink to "Props"
| Prop | What it does | Type | Default |
|---|---|---|---|
children | What screen readers announce: text or elements (a Kbd). Empty ("", null, false) says nothing. | ReactNode | |
sequence | Changes with every announcement, so the same words twice in a row are read twice (useLiveMessage gives it). Omit it to announce only when the words change. | number | |
politeness | polite (default, role="status") or assertive (role="alert", for errors). | Politeness | |
className | Added to the region, for the owning component's own rules (a reset, a hidden-host fix). | string |
Generated from the type definitions in @arshad-shah/cynosure 0.5.0.
useLiveMessage() returns [message, say]. say(words) announces the words, again when they are the same as last time; say("") clears the region without a sound. Spread message on the region.
Behaviour and accessibilityLink to "Behaviour and accessibility"
- Polite by default:
role="status",aria-live="polite",aria-atomic="true".politeness="assertive"makes itrole="alert", for errors. - Always in the page: the region is rendered empty from the start, so screen readers have registered it before the first message.
- Repeats are read: each
sayputs the words in a new node (a newsequence), and screen readers read added content, so pressing Copy twice is announced twice. There is no clear-then-set step and no timer. - Elements: the message may hold elements; a
Kbdis read by its spoken name ("Control plus K"). - Hidden: out of sight with
.cy-visually-hidden(one pixel, clipped), neverdisplay: noneoraria-hidden, so it stays in the accessibility tree, and steady under hostile host CSS.
RelatedLink to "Related"
These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.