From b71e94b23d4c0c7b7a9ad710bde65d40ba0c5ad9 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Tue, 24 Nov 2026 20:17:04 -0500 Subject: [PATCH] feat(npc-dialogue): announce lines to screen readers and pick choices by number The conversation had no live region and no keyboard path, so a screen reader user opened it and heard nothing while the only way to answer was to Tab through buttons. The log is now a polite live region with aria-busy while a line types. A polite region would otherwise announce every mutation of the truncating text, so the visible copy is hidden from assistive tech during a reveal and the whole line sits once in an sr-only sibling that never changes. Digits 1-9 take a choice, numbered over the selectable list so a locked option does not consume a digit, with the number shown on the button and exposed as aria-keyshortcuts. Enter or Space finishes the typing line or starts the next one during the beat; Escape closes, from Radix. Measured: sr-only copy present at full length while the visible copy read 'Hol', removed once settled, aria-busy true then false; pressing 2 took 'Follow the river'; Enter cut a 2587ms line to 382ms. --- .../frontend/personnel/NpcConversation.tsx | 88 +++++++++++++++++-- src/lib/npc-dialogue/AGENTS.md | 21 +++++ src/lib/npc-dialogue/keys.ts | 34 +++++++ tests/int/npc-dialogue-keys.int.spec.ts | 46 ++++++++++ 4 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 src/lib/npc-dialogue/keys.ts create mode 100644 tests/int/npc-dialogue-keys.int.spec.ts diff --git a/src/components/frontend/personnel/NpcConversation.tsx b/src/components/frontend/personnel/NpcConversation.tsx index 21a7b653..8f58bc5f 100644 --- a/src/components/frontend/personnel/NpcConversation.tsx +++ b/src/components/frontend/personnel/NpcConversation.tsx @@ -7,7 +7,8 @@ import type { GameNpc } from "@/payload-types"; import type { ChoiceGate, DialogueNext, DialogueNode, FlowData } from "@/lib/npc-dialogue/dialogue"; import { collectNodeTokens } from "@/lib/npc-dialogue/interpolate"; import type { ResolvedNodeTexts } from "@/lib/npc-dialogue/interpolate"; -import { DialogueText, visibleLength } from "@/lib/npc-dialogue/DialogueText"; +import { DialogueText, visibleLength, visibleText } from "@/lib/npc-dialogue/DialogueText"; +import { choiceIndexFromKey, choiceShortcutLabel, isSkipKey } from "@/lib/npc-dialogue/keys"; import { DEFAULT_PACING_PRESET, beatMs, @@ -506,6 +507,35 @@ export function NpcConversation({ } }; + /** Digits pick a choice, Enter or Space finishes the line that is typing (or + * starts the next one during the beat). Attached to the dialog's content, so + * it only acts when focus is inside the conversation, and it steps aside for + * a focused control, where the browser's own activation is correct. */ + const onConversationKeyDown = (event: React.KeyboardEvent) => { + if (event.metaKey || event.ctrlKey || event.altKey) return; + const target = event.target as HTMLElement | null; + if (target?.closest("button, a, input, select, textarea, [role='button']")) return; + + const choiceIndex = choiceIndexFromKey(event.key, selectableChoices.length); + if (choiceIndex !== null) { + const choice = selectableChoices[choiceIndex]; + if (choice) { + event.preventDefault(); + pick(choice.hop, choice.index, choiceLabel(choice.hop, choice.index)); + } + return; + } + + if (!isSkipKey(event.key)) return; + if (streamingId) { + event.preventDefault(); + skipRevealRef.current = streamingId; + } else if (typing !== null) { + event.preventDefault(); + skipBeatRef.current = true; + } + }; + const close = () => { sessionRef.current += 1; audioRef.current?.pause(); @@ -567,6 +597,9 @@ export function NpcConversation({ }, []) : []; const anySelectableChoice = visibleChoices.some((choice) => !choice.locked); + /** Only the choices a keypress may take. Numbering and the keyboard both run + * off this list, so a locked option never consumes a digit. */ + const selectableChoices = visibleChoices.filter((choice) => !choice.locked); /** The node whose options belong on screen. Null while anything is still * arriving, which is what opens and closes the footer: the row animates from @@ -701,6 +734,7 @@ export function NpcConversation({ !next && close()}> {/* Bottom-anchored like a messenger: a short conversation sits @@ -830,11 +869,19 @@ export function NpcConversation({ {visibleChoices.map(({ hop, index, locked }) => { const isNew = unseenTargets.has(hop.target); + // The number matches the key that picks it; locked + // choices are unnumbered because no key reaches them. + const shortcut = locked + ? null + : choiceShortcutLabel( + selectableChoices.findIndex((choice) => choice.index === index), + ); const button = (