Repository navigation
Expand file tree
/
Copy pathLiveRegionContext.tsx
More file actions
93 lines (82 loc) · 2.88 KB
/
Copy pathLiveRegionContext.tsx
File metadata and controls
93 lines (82 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import type { ReactNode } from 'react';
import { createContext, useCallback, useContext, useMemo, useRef, useState } from 'react';
import { useAppSelector } from '../app/hooks';
export type AnnouncePriority = 'polite' | 'assertive';
export interface AnnounceOptions {
/** When live region verbosity is minimal, skip this announcement (navigation titles never use this). */
skipIfMinimal?: boolean;
}
interface LiveRegionContextValue {
announce: (message: string, priority?: AnnouncePriority, options?: AnnounceOptions) => void;
}
const LiveRegionContext = createContext<LiveRegionContextValue | undefined>(undefined);
// QNBS-v3: Central aria-live regions instead of dynamically created nodes — stable for screen readers.
export function LiveRegionProvider({ children }: { children: ReactNode }) {
const liveRegionVerbosity = useAppSelector(
(s) => s.settings?.accessibility?.liveRegionVerbosity ?? 'normal',
);
const [politeMessage, setPoliteMessage] = useState('');
const [assertiveMessage, setAssertiveMessage] = useState('');
const politeSeq = useRef(0);
const assertiveSeq = useRef(0);
const flushPolite = useCallback((message: string) => {
politeSeq.current += 1;
const seq = politeSeq.current;
setPoliteMessage('');
requestAnimationFrame(() => {
requestAnimationFrame(() => {
if (seq === politeSeq.current) {
setPoliteMessage(message);
}
});
});
}, []);
const flushAssertive = useCallback((message: string) => {
assertiveSeq.current += 1;
const seq = assertiveSeq.current;
setAssertiveMessage('');
requestAnimationFrame(() => {
requestAnimationFrame(() => {
if (seq === assertiveSeq.current) {
setAssertiveMessage(message);
}
});
});
}, []);
const announce = useCallback(
(message: string, priority: AnnouncePriority = 'polite', options?: AnnounceOptions) => {
if (!message.trim()) return;
if (liveRegionVerbosity === 'minimal' && options?.skipIfMinimal) {
return;
}
if (priority === 'assertive') {
flushAssertive(message);
} else {
flushPolite(message);
}
},
[flushAssertive, flushPolite, liveRegionVerbosity],
);
const value = useMemo(() => ({ announce }), [announce]);
return (
<LiveRegionContext.Provider value={value}>
{children}
<div aria-live="polite" aria-atomic="true" className="sr-only">
{politeMessage}
</div>
<div aria-live="assertive" aria-atomic="true" className="sr-only">
{assertiveMessage}
</div>
</LiveRegionContext.Provider>
);
}
export function useAnnounce(): LiveRegionContextValue['announce'] {
const ctx = useContext(LiveRegionContext);
// Return a no-op announcer when the provider is not present (tests or lightweight renders).
if (!ctx) {
return () => {
/* no-op */
};
}
return ctx.announce;
}