CngxLiveAnnouncer
projects/common/a11y/aria/live-announcer.ts
Import#
import { CngxLiveAnnouncer } from '@cngx/common/a11y'
Description#
Imperative, reusable polite/assertive live-region announcer.
The imperative counterpart to CngxLiveRegion: where the directive
decorates an element you render (the content IS the announcement), this
service owns a single visually-hidden aria-live element appended to
document.body and exposes an announce(message) call for template-less
directives and event handlers (e.g. CngxCopyValue copy success,
CngxOtpInput completion).
One root element is created lazily on the first announcement and reused for every subsequent call. The text is cleared and re-set on the next macrotask so an identical consecutive message is still re-read by the screen reader.
SSR-safe: all DOM access is guarded by isPlatformBrowser; on the server
announce() is a no-op and no element is created.
const announcer = inject(CngxLiveAnnouncer);
announcer.announce('Copied');
announcer.announce('Save failed', 'assertive');Index#
Methods
Methods#
announce(message: string, politeness: "polite" | "assertive")Announces message through the shared live region.
utterance; 'assertive' interrupts immediately.
Text read by the screen reader.
"polite" | "assertive"= 'polite''polite' (default) queues after the current
utterance; 'assertive' interrupts immediately.