-
Notifications
You must be signed in to change notification settings - Fork 559
/
Copy pathindex.tsx
372 lines (341 loc) · 10.8 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* eslint-disable jsx-a11y/no-static-element-interactions */
/**
* Welcome to @reach/dialog!
*
* An accessible dialog or "modal" window.
*
* @see Docs https://reacttraining.com/reach-ui/dialog
* @see Source https://github.com/reach/reach-ui/tree/master/packages/dialog
* @see WAI-ARIA https://www.w3.org/TR/wai-aria-practices-1.2/#dialog_modal
*/
import React, { forwardRef, useCallback, useEffect, useRef } from "react";
import Portal from "@reach/portal";
import {
checkStyles,
getOwnerDocument,
isString,
noop,
useForkedRef,
wrapEvent,
} from "@reach/utils";
import FocusLock from "react-focus-lock";
import { RemoveScroll } from "react-remove-scroll";
import PropTypes from "prop-types";
const overlayPropTypes = {
initialFocusRef: () => null,
allowPinchZoom: PropTypes.bool,
onDismiss: PropTypes.func,
};
////////////////////////////////////////////////////////////////////////////////
/**
* DialogOverlay
*
* Low-level component if you need more control over the styles or rendering of
* the dialog overlay.
*
* Note: You must render a `DialogContent` inside.
*
* @see Docs https://reacttraining.com/reach-ui/dialog#dialogoverlay
*/
export const DialogOverlay = forwardRef<HTMLDivElement, DialogProps>(
function DialogOverlay({ isOpen = true, ...props }, forwardedRef) {
useEffect(() => checkStyles("dialog"), []);
// We want to ignore the immediate focus of a tooltip so it doesn't pop
// up again when the menu closes, only pops up when focus returns again
// to the tooltip (like native OS tooltips).
useEffect(() => {
if (isOpen) {
// @ts-ignore
window.__REACH_DISABLE_TOOLTIPS = true;
} else {
window.requestAnimationFrame(() => {
// Wait a frame so that this doesn't fire before tooltip does
// @ts-ignore
window.__REACH_DISABLE_TOOLTIPS = false;
});
}
}, [isOpen]);
return isOpen ? (
<Portal data-reach-dialog-wrapper="">
<DialogInner ref={forwardedRef} {...props} />
</Portal>
) : null;
}
);
if (__DEV__) {
DialogOverlay.displayName = "DialogOverlay";
DialogOverlay.propTypes = {
...overlayPropTypes,
isOpen: PropTypes.bool,
};
}
////////////////////////////////////////////////////////////////////////////////
/**
* DialogInner
*/
const DialogInner = forwardRef<HTMLDivElement, DialogProps>(
function DialogInner(
{
allowPinchZoom,
initialFocusRef,
onClick,
onDismiss = noop,
onMouseDown,
onKeyDown,
...props
},
forwardedRef
) {
const mouseDownTarget = useRef<EventTarget | null>(null);
const overlayNode = useRef<HTMLDivElement | null>(null);
const ref = useForkedRef(overlayNode, forwardedRef);
const activateFocusLock = useCallback(() => {
if (initialFocusRef && initialFocusRef.current) {
initialFocusRef.current.focus();
}
}, [initialFocusRef]);
function handleClick(event: React.MouseEvent) {
if (mouseDownTarget.current === event.target) {
event.stopPropagation();
onDismiss(event);
}
}
function handleKeyDown(event: React.KeyboardEvent) {
if (event.key === "Escape") {
event.stopPropagation();
onDismiss(event);
}
}
function handleMouseDown(event: React.MouseEvent) {
mouseDownTarget.current = event.target;
}
useEffect(
() =>
overlayNode.current ? createAriaHider(overlayNode.current) : void null,
[]
);
return (
<FocusLock autoFocus returnFocus onActivation={activateFocusLock}>
<RemoveScroll allowPinchZoom={allowPinchZoom}>
<div
{...props}
ref={ref}
data-reach-dialog-overlay=""
/*
* We can ignore the `no-static-element-interactions` warning here
* because our overlay is only designed to capture any outside
* clicks, not to serve as a clickable element itself.
*/
onClick={wrapEvent(onClick, handleClick)}
onKeyDown={wrapEvent(onKeyDown, handleKeyDown)}
onMouseDown={wrapEvent(onMouseDown, handleMouseDown)}
/>
</RemoveScroll>
</FocusLock>
);
}
);
if (__DEV__) {
DialogOverlay.displayName = "DialogOverlay";
DialogOverlay.propTypes = {
...overlayPropTypes,
};
}
////////////////////////////////////////////////////////////////////////////////
/**
* DialogContent
*
* Low-level component if you need more control over the styles or rendering of
* the dialog content.
*
* Note: Must be a child of `DialogOverlay`.
*
* Note: You only need to use this when you are also styling `DialogOverlay`,
* otherwise you can use the high-level `Dialog` component and pass the props
* to it. Any props passed to `Dialog` component (besides `isOpen` and
* `onDismiss`) will be spread onto `DialogContent`.
*
* @see Docs https://reacttraining.com/reach-ui/dialog#dialogcontent
*/
export const DialogContent = forwardRef<HTMLDivElement, DialogContentProps>(
function DialogContent({ onClick, onKeyDown, ...props }, forwardedRef) {
return (
<div
aria-modal="true"
role="dialog"
tabIndex={-1}
{...props}
ref={forwardedRef}
data-reach-dialog-content=""
onClick={wrapEvent(onClick, event => {
event.stopPropagation();
})}
/>
);
}
);
/**
* @see Docs https://reacttraining.com/reach-ui/dialog#dialogcontent-props
*/
export type DialogContentProps = {
/**
* Accepts any renderable content.
*
* @see Docs https://reacttraining.com/reach-ui/dialog#dialogcontent-children
*/
children?: React.ReactNode;
} & React.HTMLAttributes<HTMLDivElement>;
if (__DEV__) {
DialogContent.displayName = "DialogContent";
DialogContent.propTypes = {
"aria-label": ariaLabelType,
"aria-labelledby": ariaLabelType,
};
}
////////////////////////////////////////////////////////////////////////////////
/**
* Dialog
*
* High-level component to render a modal dialog window over the top of the page
* (or another dialog).
*
* @see Docs https://reacttraining.com/reach-ui/dialog#dialog
*/
export const Dialog = forwardRef<HTMLDivElement, DialogProps>(function Dialog(
{ isOpen, onDismiss = noop, initialFocusRef, allowPinchZoom, ...props },
forwardedRef
) {
return (
<DialogOverlay
initialFocusRef={initialFocusRef}
allowPinchZoom={allowPinchZoom}
isOpen={isOpen}
onDismiss={onDismiss}
>
<DialogContent ref={forwardedRef} {...props} />
</DialogOverlay>
);
});
/**
* @see Docs https://reacttraining.com/reach-ui/dialog#dialog-props
*/
export type DialogProps = {
allowPinchZoom?: boolean;
/**
* Controls whether the dialog is open or not.
*
* @see Docs https://reacttraining.com/reach-ui/dialog#dialog-isopen
*/
isOpen?: boolean;
/**
* This function is called whenever the user hits "Escape" or clicks outside
* the dialog. _It's important to close the dialog `onDismiss`_.
*
* The only time you shouldn't close the dialog on dismiss is when the dialog
* requires a choice and none of them are "cancel". For example, perhaps two
* records need to be merged and the user needs to pick the surviving record.
* Neither choice is less destructive than the other, so in these cases you
* may want to alert the user they need to a make a choice on dismiss instead
* of closing the dialog.
*
* @see Docs https://reacttraining.com/reach-ui/dialog#dialog-ondismiss
*/
onDismiss?: (event?: React.SyntheticEvent) => void;
/**
* Accepts any renderable content.
*
* @see Docs https://reacttraining.com/reach-ui/dialog#dialog-children
*/
children?: React.ReactNode;
/**
* By default the first focusable element will receive focus when the dialog
* opens but you can provide a ref to focus instead.
*
* @see Docs https://reacttraining.com/reach-ui/dialog#dialogoverlay-initialfocusref
*/
initialFocusRef?: React.RefObject<any>;
} & React.HTMLAttributes<HTMLDivElement>;
if (__DEV__) {
Dialog.displayName = "Dialog";
Dialog.propTypes = {
isOpen: PropTypes.bool,
onDismiss: PropTypes.func,
"aria-label": ariaLabelType,
"aria-labelledby": ariaLabelType,
};
}
export default Dialog;
////////////////////////////////////////////////////////////////////////////////
function createAriaHider(dialogNode: HTMLElement) {
let originalValues: any[] = [];
let rootNodes: HTMLElement[] = [];
let ownerDocument = getOwnerDocument(dialogNode) || document;
if (!dialogNode) {
if (__DEV__) {
console.warn(
"A ref has not yet been attached to a dialog node when attempting to call `createAriaHider`."
);
}
return noop;
}
Array.prototype.forEach.call(
ownerDocument.querySelectorAll("body > *"),
node => {
const portalNode = dialogNode.parentNode?.parentNode?.parentNode;
if (node === portalNode) {
return;
}
let attr = node.getAttribute("aria-hidden");
let alreadyHidden = attr !== null && attr !== "false";
if (alreadyHidden) {
return;
}
originalValues.push(attr);
rootNodes.push(node);
node.setAttribute("aria-hidden", "true");
}
);
return () => {
rootNodes.forEach((node, index) => {
let originalValue = originalValues[index];
if (originalValue === null) {
node.removeAttribute("aria-hidden");
} else {
node.setAttribute("aria-hidden", originalValue);
}
});
};
}
function ariaLabelType(
props: { [key: string]: any },
propName: string,
compName: string,
location: string,
propFullName: string
) {
const details =
"\nSee https://www.w3.org/TR/wai-aria/#aria-label for details.";
if (!props["aria-label"] && !props["aria-labelledby"]) {
return new Error(
`A <${compName}> must have either an \`aria-label\` or \`aria-labelledby\` prop.
${details}`
);
}
if (props["aria-label"] && props["aria-labelledby"]) {
return new Error(
"You provided both `aria-label` and `aria-labelledby` props to a <" +
compName +
">. If the a label for this component is visible on the screen, that label's component should be given a unique ID prop, and that ID should be passed as the `aria-labelledby` prop into <" +
compName +
">. If the label cannot be determined programmatically from the content of the element, an alternative label should be provided as the `aria-label` prop, which will be used as an `aria-label` on the HTML tag." +
details
);
} else if (props[propName] != null && !isString(props[propName])) {
return new Error(
`Invalid prop \`${propName}\` supplied to \`${compName}\`. Expected \`string\`, received \`${
Array.isArray(propFullName) ? "array" : typeof propFullName
}\`.`
);
}
return null;
}