/
index.tsx
228 lines (203 loc) · 6.86 KB
/
index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Welcome to @reach/alert!
*
* An alert is an element that displays a brief, important message in a way that
* attracts the user's attention without interrupting the user's task.
* Dynamically rendered alerts are automatically announced by most screen
* readers, and in some operating systems, they may trigger an alert sound.
*
* The approach here is to allow developers to render a visual <Alert> and then
* we mirror that to a couple of aria-live regions behind the scenes. This way,
* most of the time, developers don't have to think about visual vs. aria
* alerts.
*
* Limitations: Developers can't read from context inside of an Alert because
* we aren't using ReactDOM.createPortal(), we're actually creating a couple of
* brand new React roots. We could use createPortal but then apps would need to
* render the entire app tree in an <AlertProvider>, or maybe there's a way
* with default context to do it, but we haven't explored that yet. So, we'll
* see how this goes. If it becomes a problem we can introduce a portal later.
*
* @see Docs https://reacttraining.com/reach-ui/alert
* @see Source https://github.com/reach/reach-ui/tree/main/packages/alert
* @see WAI-ARIA https://www.w3.org/TR/wai-aria-practices-1.2/#alert
*/
import React, { forwardRef, useEffect, useRef, useMemo } from "react";
import { render } from "react-dom";
import VisuallyHidden from "@reach/visually-hidden";
import { getOwnerDocument, usePrevious, useForkedRef } from "@reach/utils";
import PropTypes from "prop-types";
/*
* Singleton state is fine because you don't server render
* an alert (SRs don't read them on first load anyway)
*/
let keys: RegionKeys = {
polite: -1,
assertive: -1,
};
let elements: ElementTypes = {
polite: {},
assertive: {},
};
let liveRegions: RegionElements = {
polite: null,
assertive: null,
};
let renderTimer: number | null;
////////////////////////////////////////////////////////////////////////////////
/**
* Alert
*
* Screen-reader-friendly alert messages. In many apps developers add "alert"
* messages when network events or other things happen. Users with assistive
* technologies may not know about the message unless you develop for it.
*
* @see Docs https://reacttraining.com/reach-ui/alert
*/
export const Alert = forwardRef<HTMLDivElement, AlertProps>(function Alert(
{ children, type: regionType = "polite", ...props },
forwardedRef
) {
const ownRef = useRef(null);
const ref = useForkedRef(forwardedRef, ownRef);
const child = useMemo(
() => (
<div {...props} ref={ref} data-reach-alert>
{children}
</div>
),
// eslint-disable-next-line react-hooks/exhaustive-deps
[children, props]
);
useMirrorEffects(regionType, child, ownRef);
return child;
});
/**
* @see Docs https://reacttraining.com/reach-ui/alert#alert-props
*/
export type AlertProps = {
/**
* Controls whether the assistive technology should read immediately
* ("assertive") or wait until the user is idle ("polite").
*
* @see Docs https://reacttraining.com/reach-ui/alert#alert-type
*/
type?: "assertive" | "polite";
children: React.ReactNode;
} & React.HTMLAttributes<HTMLDivElement>;
if (__DEV__) {
Alert.displayName = "Alert";
Alert.propTypes = {
children: PropTypes.node,
type: PropTypes.oneOf(["assertive", "polite"]),
};
}
export default Alert;
////////////////////////////////////////////////////////////////////////////////
function createMirror(type: "polite" | "assertive", doc: Document): Mirror {
let key = ++keys[type];
let mount = (element: JSX.Element) => {
if (liveRegions[type]) {
elements[type][key] = element;
renderAlerts();
} else {
let node = doc.createElement("div");
node.setAttribute(`data-reach-live-${type}`, "true");
liveRegions[type] = node;
doc.body.appendChild(liveRegions[type]!);
mount(element);
}
};
let update = (element: JSX.Element) => {
elements[type][key] = element;
renderAlerts();
};
let unmount = () => {
delete elements[type][key];
renderAlerts();
};
return { mount, update, unmount };
}
function renderAlerts() {
if (renderTimer != null) {
window.clearTimeout(renderTimer);
}
renderTimer = window.setTimeout(() => {
Object.keys(elements).forEach((elementType) => {
let regionType: RegionTypes = elementType as RegionTypes;
let container = liveRegions[regionType]!;
if (container) {
render(
<VisuallyHidden>
<div
// The status role is a type of live region and a container whose
// content is advisory information for the user that is not
// important enough to justify an alert, and is often presented as
// a status bar. When the role is added to an element, the browser
// will send out an accessible status event to assistive
// technology products which can then notify the user about it.
// https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_status_role
role={regionType === "assertive" ? "alert" : "status"}
aria-live={regionType}
>
{Object.keys(elements[regionType]).map((key) =>
React.cloneElement(elements[regionType][key], {
key,
ref: null,
})
)}
</div>
</VisuallyHidden>,
liveRegions[regionType]
);
}
});
}, 500);
}
function useMirrorEffects(
regionType: RegionTypes,
element: JSX.Element,
ref: React.RefObject<any>
) {
const prevType = usePrevious<RegionTypes>(regionType);
const mirror = useRef<Mirror | null>(null);
const mounted = useRef(false);
useEffect(() => {
const ownerDocument = getOwnerDocument(ref.current) || document;
if (!mounted.current) {
mounted.current = true;
mirror.current = createMirror(regionType, ownerDocument);
mirror.current.mount(element);
} else if (prevType !== regionType) {
mirror.current && mirror.current.unmount();
mirror.current = createMirror(regionType, ownerDocument);
mirror.current.mount(element);
} else {
mirror.current && mirror.current.update(element);
}
}, [element, regionType, prevType, ref]);
useEffect(() => {
return () => {
mirror.current && mirror.current.unmount();
};
}, []);
}
////////////////////////////////////////////////////////////////////////////////
// Types
type Mirror = {
mount: (element: JSX.Element) => void;
update: (element: JSX.Element) => void;
unmount: () => void;
};
type RegionTypes = "polite" | "assertive";
type ElementTypes = {
[key in RegionTypes]: {
[key: string]: JSX.Element;
};
};
type RegionElements<T extends HTMLElement = HTMLDivElement> = {
[key in RegionTypes]: T | null;
};
type RegionKeys = {
[key in RegionTypes]: number;
};