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.
This commit is contained in:
parent
6db4ecc3a7
commit
b71e94b23d
4 changed files with 183 additions and 6 deletions
|
|
@ -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<HTMLDivElement>) => {
|
||||
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({
|
|||
<Dialog open={open} onOpenChange={(next) => !next && close()}>
|
||||
<DialogContent
|
||||
showCloseButton={false}
|
||||
onKeyDown={onConversationKeyDown}
|
||||
className={`flex w-[calc(100%-1.5rem)] max-w-2xl flex-col gap-0 overflow-hidden rounded-none border border-white/20 bg-zinc-950 p-0 text-white sm:w-full sm:rounded-none ${
|
||||
// A frame of fixed height. The dialog used to size itself to its
|
||||
// content, so the choice footer appearing made it taller, and being
|
||||
|
|
@ -770,6 +804,11 @@ export function NpcConversation({
|
|||
else skipBeatRef.current = true;
|
||||
}}
|
||||
data-log="conversation"
|
||||
role="log"
|
||||
aria-live="polite"
|
||||
aria-relevant="additions"
|
||||
aria-busy={streamingId !== null || typing !== null || playerTyping}
|
||||
aria-label={`Conversation with ${npc.name}`}
|
||||
className="min-h-0 flex-1 overflow-y-auto p-5"
|
||||
>
|
||||
{/* Bottom-anchored like a messenger: a short conversation sits
|
||||
|
|
@ -830,11 +869,19 @@ export function NpcConversation({
|
|||
<TooltipProvider delayDuration={150}>
|
||||
{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 = (
|
||||
<button
|
||||
type="button"
|
||||
disabled={locked}
|
||||
onClick={() => pick(hop, index, choiceLabel(hop, index))}
|
||||
aria-keyshortcuts={shortcut ?? undefined}
|
||||
style={{ animationDelay: `${Math.min(index, 6) * 40}ms` }}
|
||||
className={`w-full rounded-none border px-3 py-2 text-left text-sm transition-colors animate-in fade-in-0 slide-in-from-bottom-1 fill-mode-both duration-200 motion-reduce:animate-none ${
|
||||
locked
|
||||
|
|
@ -846,6 +893,14 @@ export function NpcConversation({
|
|||
{locked && (
|
||||
<LockIcon className="size-3 shrink-0 text-amber-400/70" />
|
||||
)}
|
||||
{shortcut && (
|
||||
<span
|
||||
aria-hidden
|
||||
className="shrink-0 rounded-sm border border-white/15 bg-black/40 px-1 font-mono text-[10px] leading-tight text-white/40"
|
||||
>
|
||||
{shortcut}
|
||||
</span>
|
||||
)}
|
||||
<DialogueText text={choiceLabel(hop, index)} />
|
||||
{isNew && !locked && (
|
||||
<span className="ml-auto shrink-0 rounded-sm bg-cyan-400 px-1 font-mono text-[9px] font-bold uppercase leading-tight text-black">
|
||||
|
|
@ -892,6 +947,27 @@ export function NpcConversation({
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A line that may still be typing.
|
||||
*
|
||||
* The visible copy truncates character by character, which is exactly what a
|
||||
* live region must not be fed: it would announce a stutter of fragments. So
|
||||
* while a reveal is running the visible text is hidden from assistive tech and
|
||||
* the whole sentence sits in an `sr-only` copy that never changes. A screen
|
||||
* reader therefore gets the line once, in full, at the moment it arrives.
|
||||
*/
|
||||
function RevealingText({ text, reveal }: { readonly text: string; readonly reveal?: number }) {
|
||||
if (reveal === undefined) return <DialogueText text={text} />;
|
||||
return (
|
||||
<>
|
||||
<span aria-hidden>
|
||||
<DialogueText text={text} reveal={reveal} />
|
||||
</span>
|
||||
<span className="sr-only">{visibleText(text)}</span>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function TimelineEntry({
|
||||
entry,
|
||||
speakerDefault,
|
||||
|
|
@ -934,7 +1010,7 @@ function TimelineEntry({
|
|||
return (
|
||||
<div className="flex flex-col items-center gap-2 py-1 text-center">
|
||||
<p className="max-w-[42rem] text-sm italic leading-relaxed text-white/55">
|
||||
<DialogueText text={text} reveal={reveal} />
|
||||
<RevealingText text={text} reveal={reveal} />
|
||||
</p>
|
||||
{voiceFallback}
|
||||
<DialogueAttachments attachments={resolved?.attachments} />
|
||||
|
|
@ -945,7 +1021,7 @@ function TimelineEntry({
|
|||
return (
|
||||
<div className="flex flex-col gap-1">
|
||||
<p className="border-l-2 border-amber-500/50 pl-3 text-sm italic text-amber-100/80">
|
||||
<DialogueText text={text} reveal={reveal} />
|
||||
<RevealingText text={text} reveal={reveal} />
|
||||
</p>
|
||||
{voiceFallback}
|
||||
<DialogueAttachments attachments={resolved?.attachments} />
|
||||
|
|
@ -957,7 +1033,7 @@ function TimelineEntry({
|
|||
return (
|
||||
<div className="flex flex-col gap-1">
|
||||
<p className="text-sm italic text-white/50">
|
||||
<DialogueText text={text} reveal={reveal} />
|
||||
<RevealingText text={text} reveal={reveal} />
|
||||
</p>
|
||||
{voiceFallback}
|
||||
</div>
|
||||
|
|
@ -969,7 +1045,7 @@ function TimelineEntry({
|
|||
if (!text) return null;
|
||||
return (
|
||||
<p className="text-sm italic text-white/60">
|
||||
<DialogueText text={text} reveal={reveal} />
|
||||
<RevealingText text={text} reveal={reveal} />
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
|
@ -980,7 +1056,7 @@ function TimelineEntry({
|
|||
<span className={DOSSIER.readoutLabel}>{speaker}</span>
|
||||
<p className="text-sm text-white/90">
|
||||
{text ? (
|
||||
<DialogueText text={text} reveal={reveal} />
|
||||
<RevealingText text={text} reveal={reveal} />
|
||||
) : (
|
||||
<span className="italic text-white/30">…</span>
|
||||
)}
|
||||
|
|
|
|||
|
|
@ -226,6 +226,27 @@ fall back to their Play button.
|
|||
- The component stores the line's resolved text on the log entry and renders that,
|
||||
so the reveal length and the rendered string cannot drift apart.
|
||||
|
||||
### Accessibility
|
||||
|
||||
The log is a live region (`role="log"`, `aria-live="polite"`, `aria-relevant="additions"`)
|
||||
with a per-NPC `aria-label`, and `aria-busy` is true while a line types or the beat
|
||||
runs, so a screen reader is never fed a half-finished sentence.
|
||||
|
||||
**The reveal is why this needed care.** A polite live region would announce every
|
||||
mutation of the truncating text, which is a stutter of fragments. `RevealingText`
|
||||
therefore hides the visible copy (`aria-hidden`) while a `reveal` value is present and
|
||||
renders the whole line once in an `sr-only` sibling that never changes. The line is
|
||||
announced once, in full, when it arrives.
|
||||
|
||||
Keyboard: digits `1`-`9` take a choice (`choiceIndexFromKey` in `keys.ts`, mapped over
|
||||
the *selectable* choices so a locked option never consumes a digit); the same number is
|
||||
shown on the button as a mono badge and exposed as `aria-keyshortcuts`. `Enter` or
|
||||
`Space` finishes the line that is typing, or starts the next one during the beat.
|
||||
`Escape` closes, from Radix. The handler sits on `DialogContent` and returns early when
|
||||
the event came from a control, so a focused button keeps its own activation.
|
||||
|
||||
Measured: 2587ms to idle naturally against 382ms when `Enter` is pressed at 300ms.
|
||||
|
||||
### The conversation frame
|
||||
|
||||
The dialog is a **fixed-height frame** (`h-[min(80dvh,36rem)]`), not a box that
|
||||
|
|
|
|||
34
src/lib/npc-dialogue/keys.ts
Normal file
34
src/lib/npc-dialogue/keys.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
* Keyboard picks for a conversation's choices.
|
||||
*
|
||||
* Digits 1 through 9 select a choice by its position among the ones the player
|
||||
* can actually take, so a locked option does not shift the numbers in a way
|
||||
* that makes "2" pick the third button. Pure, so the mapping is tested rather
|
||||
* than trusted.
|
||||
*/
|
||||
|
||||
const MAX_SHORTCUT_CHOICES = 9;
|
||||
|
||||
/** Zero-based index of the choice a keypress selects, or null if it selects
|
||||
* none. Only single digits 1-9 count: "0" is not a choice, and a modifier
|
||||
* combination belongs to the browser. */
|
||||
export function choiceIndexFromKey(key: string, selectableCount: number): number | null {
|
||||
if (key.length !== 1) return null;
|
||||
const digit = key.charCodeAt(0) - 48;
|
||||
if (digit < 1 || digit > MAX_SHORTCUT_CHOICES) return null;
|
||||
const index = digit - 1;
|
||||
return index < selectableCount ? index : null;
|
||||
}
|
||||
|
||||
/** The digit shown on a choice, 1-9, or null past the shortcut range. */
|
||||
export function choiceShortcutLabel(selectableIndex: number): string | null {
|
||||
if (selectableIndex < 0 || selectableIndex >= MAX_SHORTCUT_CHOICES) return null;
|
||||
return String(selectableIndex + 1);
|
||||
}
|
||||
|
||||
/** Whether a keypress should finish a line that is still typing (or start the
|
||||
* next one when the beat is showing). Space and Enter only, and never on a
|
||||
* focused control, where the browser's own activation is the right answer. */
|
||||
export function isSkipKey(key: string): boolean {
|
||||
return key === "Enter" || key === " ";
|
||||
}
|
||||
46
tests/int/npc-dialogue-keys.int.spec.ts
Normal file
46
tests/int/npc-dialogue-keys.int.spec.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// @vitest-environment node
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { choiceIndexFromKey, choiceShortcutLabel, isSkipKey } from "@/lib/npc-dialogue/keys";
|
||||
|
||||
/**
|
||||
* Digit shortcuts for choices. The number shown on a button and the number the
|
||||
* keyboard accepts must agree, and a locked choice must not shift the numbering,
|
||||
* so both are computed from the selectable list rather than the server index.
|
||||
*/
|
||||
describe("conversation keyboard picks", () => {
|
||||
it("maps 1-9 onto the selectable choices", () => {
|
||||
expect(choiceIndexFromKey("1", 4)).toBe(0);
|
||||
expect(choiceIndexFromKey("2", 4)).toBe(1);
|
||||
expect(choiceIndexFromKey("4", 4)).toBe(3);
|
||||
});
|
||||
|
||||
it("ignores digits past the number of choices", () => {
|
||||
expect(choiceIndexFromKey("5", 4)).toBeNull();
|
||||
expect(choiceIndexFromKey("1", 0)).toBeNull();
|
||||
});
|
||||
|
||||
it("ignores anything that is not a digit 1-9", () => {
|
||||
for (const key of ["0", "a", "Enter", " ", "Escape", "ArrowDown", "10"]) {
|
||||
expect(choiceIndexFromKey(key, 9)).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("labels choices with the same numbers the keyboard accepts", () => {
|
||||
expect([0, 1, 2].map((i) => choiceShortcutLabel(i))).toEqual(["1", "2", "3"]);
|
||||
expect(choiceShortcutLabel(8)).toBe("9");
|
||||
expect(choiceShortcutLabel(9)).toBeNull();
|
||||
expect(choiceShortcutLabel(-1)).toBeNull();
|
||||
// Every label the button shows resolves to the same choice on keypress.
|
||||
for (let i = 0; i < 9; i += 1) {
|
||||
expect(choiceIndexFromKey(choiceShortcutLabel(i) as string, 9)).toBe(i);
|
||||
}
|
||||
});
|
||||
|
||||
it("treats only Enter and Space as skip keys", () => {
|
||||
expect(isSkipKey("Enter")).toBe(true);
|
||||
expect(isSkipKey(" ")).toBe(true);
|
||||
expect(isSkipKey("a")).toBe(false);
|
||||
expect(isSkipKey("Escape")).toBe(false);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue