Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

announce.ts

71 lines2.8 KB

1// Screen readers skip a live region that appears already filled, and moving focus cuts off
2// whatever they were saying. So the regions exist before the first message lands, and
3// messages wait until focus has stayed put for a moment, then are spoken together.
4const FOCUS_SETTLE_MS = 250;
5​
6// NVDA reads a polite region's leftover text again when focus returns from a
7// dialog, so an announcement is cleared once it has had time to be spoken.
8// Clearing doesn't cut off speech that already started.
9export const CLEAR_SPOKEN_AFTER_MS = 7_000;
10​
11let regions: readonly [HTMLElement, HTMLElement] | null = null;
12let pending: { message: string; source: string | undefined }[] = [];
13let spokenSources: (string | undefined)[] = [];
14let flushTimer: ReturnType<typeof setTimeout> | undefined;
15let clearTimer: ReturnType<typeof setTimeout> | undefined;
16​
17function scheduleFlush() {
18 clearTimeout(flushTimer);
19 flushTimer = setTimeout(flush, FOCUS_SETTLE_MS);
21​
22function clearSpoken() {
23 clearTimeout(clearTimer);
24 spokenSources = [];
25 for (const region of regions ?? []) region.textContent = "";
27​
28function flush() {
29 if (!regions || pending.length === 0) return;
30 clearSpoken();
31 // Alternating between two regions lets the same message be spoken twice in a row.
32 const [target, other] = regions;
33 regions = [other, target];
34 target.textContent = pending.map((entry) => entry.message).join(". ");
35 spokenSources = pending.map((entry) => entry.source);
36 pending = [];
37 clearTimer = setTimeout(clearSpoken, CLEAR_SPOKEN_AFTER_MS);
39​
40function createRegion() {
41 const region = document.createElement("div");
42 region.className = "sr-only";
43 region.setAttribute("aria-live", "polite");
44 region.setAttribute("aria-atomic", "true");
45 document.body.append(region);
46 return region;
48​
49/**
50 * Tells screen reader users what an action did when focus alone doesn't show it. A message
51 * replaces one still waiting from the same `source`, so a superseded status isn't read out.
52 */
53export function announce(message: string, source?: string) {
54 // Stores announce without a mounted owner, so they also run where no DOM exists (unit tests).
55 if (typeof document === "undefined") return;
56 if (!regions) {
57 regions = [createRegion(), createRegion()];
58 document.addEventListener("focusin", () => {
59 if (pending.length > 0) scheduleFlush();
60 });
61 }
62 if (source !== undefined) pending = pending.filter((entry) => entry.source !== source);
63 if (pending.at(-1)?.message !== message) pending.push({ message, source });
64 scheduleFlush();
66​
67/** Drops a message from `source`, waiting or already spoken, once it no longer applies. */
68export function cancelAnnouncement(source: string) {
69 pending = pending.filter((entry) => entry.source !== source);
70 if (spokenSources.includes(source)) clearSpoken();