sdxc

[ @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(options?: Announcer.Options)

Parameters

ParameterTypeDescription
options?Announcer.OptionsHow long each message holds before the queue advances on its own.

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

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

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.Options

Options for constructing an Announcer.

MemberTypeDescription
hold?numberMilliseconds each message stays current before the queue advances on its own: long enough for a screen reader to reach it, short enough that a run of quick changes is heard one by one. Omitted, the live region advances the queue with next().
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.