-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
LiveAnnouncer.tsx
142 lines (120 loc) · 3.86 KB
/
LiveAnnouncer.tsx
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
type Assertiveness = 'assertive' | 'polite';
/* Inspired by https://github.com/AlmeroSteyn/react-aria-live */
const LIVEREGION_TIMEOUT_DELAY = 7000;
let liveAnnouncer: LiveAnnouncer = null;
/**
* Announces the message using screen reader technology.
*/
export function announce(
message: string,
assertiveness: Assertiveness = 'assertive',
timeout = LIVEREGION_TIMEOUT_DELAY
) {
if (!liveAnnouncer) {
liveAnnouncer = new LiveAnnouncer();
}
liveAnnouncer.announce(message, assertiveness, timeout);
}
/**
* Stops all queued announcements.
*/
export function clearAnnouncer(assertiveness: Assertiveness) {
if (liveAnnouncer) {
liveAnnouncer.clear(assertiveness);
}
}
/**
* Removes the announcer from the DOM.
*/
export function destroyAnnouncer() {
if (liveAnnouncer) {
liveAnnouncer.destroy();
liveAnnouncer = null;
}
}
// LiveAnnouncer is implemented using vanilla DOM, not React. That's because as of React 18
// ReactDOM.render is deprecated, and the replacement, ReactDOM.createRoot is moved into a
// subpath import `react-dom/client`. That makes it hard for us to support multiple React versions.
// As a global API, we can't use portals without introducing a breaking API change. LiveAnnouncer
// is simple enough to implement without React, so that's what we do here.
// See this discussion for more details: https://github.com/reactwg/react-18/discussions/125#discussioncomment-2382638
class LiveAnnouncer {
node: HTMLElement;
assertiveLog: HTMLElement;
politeLog: HTMLElement;
constructor() {
this.node = document.createElement('div');
this.node.dataset.liveAnnouncer = 'true';
// copied from VisuallyHidden
Object.assign(this.node.style, {
border: 0,
clip: 'rect(0 0 0 0)',
clipPath: 'inset(50%)',
height: 1,
margin: '0 -1px -1px 0',
overflow: 'hidden',
padding: 0,
position: 'absolute',
width: 1,
whiteSpace: 'nowrap'
});
this.assertiveLog = this.createLog('assertive');
this.node.appendChild(this.assertiveLog);
this.politeLog = this.createLog('polite');
this.node.appendChild(this.politeLog);
document.body.prepend(this.node);
}
createLog(ariaLive: string) {
let node = document.createElement('div');
node.setAttribute('role', 'log');
node.setAttribute('aria-live', ariaLive);
node.setAttribute('aria-relevant', 'additions');
return node;
}
destroy() {
if (!this.node) {
return;
}
document.body.removeChild(this.node);
this.node = null;
}
announce(message: string, assertiveness = 'assertive', timeout = LIVEREGION_TIMEOUT_DELAY) {
if (!this.node) {
return;
}
let node = document.createElement('div');
node.textContent = message;
if (assertiveness === 'assertive') {
this.assertiveLog.appendChild(node);
} else {
this.politeLog.appendChild(node);
}
if (message !== '') {
setTimeout(() => {
node.remove();
}, timeout);
}
}
clear(assertiveness: Assertiveness) {
if (!this.node) {
return;
}
if (!assertiveness || assertiveness === 'assertive') {
this.assertiveLog.innerHTML = '';
}
if (!assertiveness || assertiveness === 'polite') {
this.politeLog.innerHTML = '';
}
}
}