@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 71 lines 2.75 KB
Raw
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.
4 const 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.
9 export const CLEAR_SPOKEN_AFTER_MS = 7_000;
10
11 let regions: readonly [HTMLElement, HTMLElement] | null = null;
12 let pending: { message: string; source: string | undefined }[] = [];
13 let spokenSources: (string | undefined)[] = [];
14 let flushTimer: ReturnType<typeof setTimeout> | undefined;
15 let clearTimer: ReturnType<typeof setTimeout> | undefined;
16
17 function scheduleFlush() {
18 clearTimeout(flushTimer);
19 flushTimer = setTimeout(flush, FOCUS_SETTLE_MS);
20 }
21
22 function clearSpoken() {
23 clearTimeout(clearTimer);
24 spokenSources = [];
25 for (const region of regions ?? []) region.textContent = "";
26 }
27
28 function 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);
38 }
39
40 function 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;
47 }
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 */
53 export 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();
65 }
66
67 /** Drops a message from `source`, waiting or already spoken, once it no longer applies. */
68 export function cancelAnnouncement(source: string) {
69 pending = pending.filter((entry) => entry.source !== source);
70 if (spokenSources.includes(source)) clearSpoken();
71 }