sdxc

Type to search, or start from one of these:

[ @sdxc/ui/behaviors ]

Announcer

Headless queue of aria-live announcements: a live-region island subscribes to its state and re-renders on change.

import { Announcer } from "@sdxc/ui/behaviors";

Usage

Owns a priority-ordered queue of aria-live announcements. A live-region island subscribes to "change", renders Announcer.current into an aria-live element, and calls Announcer.next once it has been read.

Signature

new Announcer()

Properties

PropertyTypeDescription
currentAnnouncer.Message | undefinedThe message a live region should currently render, or undefined when the queue is empty.
messagesreadonly Announcer.Message[]Every queued message, in the order a live region should announce them.

Methods

MethodDescription
announce(text: string, priority?: Announcer.Priority): stringQueues a message for announcement. Assertive messages are inserted ahead of any polite messages already queued so they interrupt the live region, and behind assertive messages queued earlier; polite messages append.
dismiss(id: string): voidRemoves one queued message by id, wherever it sits in the queue, dispatching "change" only when a message with that id was queued.
next(): voidAdvances past the current message so the next queued one becomes Announcer.current, dispatching "change" only when the queue held a message.
clear(): voidEmpties the queue, dispatching "change" only when it held messages.

Examples

announcer.addEventListener("change", () => handle.update(), { signal: handle.signal });

The events, constants and types its module publishes, each imported from @sdxc/ui/behaviors too.

Interface

Announcer.Message

One queued announcement.

MemberTypeDescription
idreadonly stringStable id used to target this message with Announcer.dismiss.
textreadonly stringAnnouncement copy, read verbatim by the live region.
priorityreadonly Priority
Interface

Announcer.Events

Events dispatched by Announcer.

MemberTypeDescription
changeEventDispatched after a message is queued, dismissed, advanced past, or the queue is cleared.
Type

Announcer.Priority

Politeness a queued message announces with, mirrored onto a live region's aria-live attribute. "assertive" interrupts the current utterance and moves ahead of any "polite" messages already queued.

type Announcer.Priority = "polite" | "assertive"

Written by Sergio Xalambrí. Follow @sergiodxa for new packages, or sponsor the work.