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
| 1 | import { useState } from "react"; |
| 2 | |
| 3 | import { CLEAR_SPOKEN_AFTER_MS } from "../lib/announce"; |
| 4 | |
| 5 | /** |
| 6 | * A polite live region written straight to the DOM, so speaking never |
| 7 | * re-renders. `attach` is the region element's ref callback; writes while no |
| 8 | * region is attached are dropped. |
| 9 | */ |
| 10 | export function createLiveRegion() { |
| 11 | let region: HTMLElement | null = null; |
| 12 | let clearTimer: ReturnType<typeof setTimeout> | null = null; |
| 13 | |
| 14 | const cancelClear = () => { |
| 15 | if (clearTimer !== null) clearTimeout(clearTimer); |
| 16 | clearTimer = null; |
| 17 | }; |
| 18 | |
| 19 | const clear = () => { |
| 20 | cancelClear(); |
| 21 | if (region !== null) region.textContent = ""; |
| 22 | }; |
| 23 | |
| 24 | const announce = (text: string) => { |
| 25 | if (region === null) return; |
| 26 | cancelClear(); |
| 27 | // Screen readers ignore a write that leaves the text unchanged, so a repeat |
| 28 | // gets a trailing non-breaking space to count as new. |
| 29 | region.textContent = region.textContent === text ? `${text}\u00A0` : text; |
| 30 | clearTimer = setTimeout(clear, CLEAR_SPOKEN_AFTER_MS); |
| 31 | }; |
| 32 | |
| 33 | const attach = (node: HTMLElement | null) => { |
| 34 | region = node; |
| 35 | }; |
| 36 | |
| 37 | return { attach, announce, clear }; |
| 38 | } |
| 39 | |
| 40 | export function useLiveRegion() { |
| 41 | const [liveRegion] = useState(createLiveRegion); |
| 42 | return { regionRef: liveRegion.attach, announce: liveRegion.announce, clear: liveRegion.clear }; |
| 43 | } |