-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
Dialog.tsx
485 lines (446 loc) · 13.8 KB
/
Dialog.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import React, {
CSSProperties,
FC,
forwardRef,
Fragment,
HTMLAttributes,
ReactNode,
} from "react";
import cn from "classnames";
import CSSTransition from "react-transition-group/CSSTransition";
import { Overlay } from "@react-md/overlay";
import {
ConditionalPortal,
RenderConditionalPortalProps,
} from "@react-md/portal";
import { OverridableCSSTransitionProps } from "@react-md/transition";
import {
bem,
FocusContainer,
FocusContainerOptionsProps,
LabelRequiredForA11y,
useCloseOnEscape,
useScrollLock,
WithForwardedRef,
} from "@react-md/utils";
import useNestedDialogFixes from "./useNestedDialogFixes";
export interface DialogProps
extends OverridableCSSTransitionProps,
RenderConditionalPortalProps,
FocusContainerOptionsProps,
HTMLAttributes<HTMLDivElement> {
/**
* An id required for a11y for the dialog.
*/
id: string;
/**
* The role for the dialog component. This should normally stay as the default of `"dialog"` **unless**
* you want the screen reader to interrupt the normal workflow for this message. It is good to set this
* value to `"alertdialog"` error message confirmations or general confirmation prompts.
*
* Note: The `dialog` technically supports being rendered as a `menu`, but this is really only
* for mobile sheet support and will not provide the menu keyboard functionality automatically.
*/
role?: "dialog" | "alertdialog" | "menu" | "none";
/**
* A label to apply to the dialog. Either this or the `aria-labelledby` prop are required
* for accessibility.
*/
"aria-label"?: string;
/**
* An id pointing to an element that is a label for the dialog. Either this or the
* `aria-label` prop are required for accessibility.
*/
"aria-labelledby"?: string;
/**
* Boolean if the dialog is currently visible.
*/
visible: boolean;
/**
* A function used to close the dialog when the overlay is clicked or the escape key
* is pressed when the `modal` prop is not enabled.
*/
onRequestClose: () => void;
/**
* The tab index for the sheet. This should normally stay at `-1`.
*/
tabIndex?: number;
/**
* Boolean if there should be an overlay displayed with the sheet. This is recommended/required
* on mobile devices.
*/
overlay?: boolean;
/**
* An optional style to apply to the overlay.
*/
overlayStyle?: CSSProperties;
/**
* An optional className to apply to the overlay.
*/
overlayClassName?: string;
/**
* Boolean if the overlay should be "hidden" from the user once it's visible be keeping the
* opacity set to `0`. This is really only used for custom dialogs like the `FixedDialog`.
*/
overlayHidden?: boolean;
/**
* An optional style to apply to the dialog container when the `type` is set to `"centered"` or
* when the `forceContainer` prop is enabled. You probably don't want to use this prop in most
* cases.
*/
containerStyle?: CSSProperties;
/**
* An optional className to apply to the dialog container when the `type` is set to `"centered"`
* or when the `forceContainer` prop is enabled. You probably don't want to use this prop in most
* cases.
*/
containerClassName?: string;
/**
* Boolean if the dialog should be "forcefully" wrapped in the `.md-dialog-container` element. You
* probably don't want to use this in most cases, but the container element will be used when
* the `type` prop is set to `"centered"`.
*/
forceContainer?: boolean;
/**
* Boolean if the dialog should act as a modal. This means that the user will no longer be able
* to close the dialog by pressing the escape key or by clicking on the overlay. You will
* be required to update the dialog to have an action that closes the dialog instead.
*/
modal?: boolean;
/**
* The display type for the modal. If you would like to position the modal in different locations
* within the page, you should set this prop to `"custom"` and add custom styles to position it
* instead.
*/
type?: "full-page" | "centered" | "custom";
/**
* Either the "first" or "last" string to focus the first or last focusable element within the
* container or a query selector string to find a focusable element within the container.
*/
defaultFocus?: "first" | "last" | string;
/**
* Boolean if the dialog should no longer add scroll locking to the page when visible. You
* normally want this prop to stay `false`, but it is useful when using custom dialogs that
* are more for popovers and don't require full user attention.
*/
disableScrollLock?: boolean;
/**
* Boolean if the ability to close the dialog via the escape key should be disabled. You should
* really not be using this as it breaks accessibility in most cases.
*
* Note: When the `modal` prop is enabled, this flag will be considered `true` as well so that
* the escape keypress no longer closes the dialog.
*/
disableEscapeClose?: boolean;
/**
* Boolean if the dialog's focus container logic should be disabled. This should normally be kept
* at the default of `false` unless implementing a custom dialog that does not require consistent
* user focus.
*/
disableFocusContainer?: boolean;
/**
* The Dialog component will attempt to automatically fix nested dialogs behind the scenes using
* the `NestedDialogContextProvider` component. This prop will disable that feature if it does
* not seem to be working as expected.
*/
disableNestedDialogFixes?: boolean;
/**
* Boolean if the `appear`, `enter`, and `exit` transitions should be disabled for the dialog.
* This is just a shortcut so all three of those props don't need to be disabled.
*/
disableTransition?: boolean;
/**
* The component to render the dialog as. This really shouldn't be used outside of the `@react-md/layout`
* and `@react-md/sheet` packages to conditionally render a navigation panel.
*/
component?: "div" | "nav";
}
type StrictProps = LabelRequiredForA11y<DialogProps>;
type WithRef = WithForwardedRef<HTMLDivElement>;
type DefaultProps = Required<
Pick<
DialogProps,
| "role"
| "tabIndex"
| "modal"
| "type"
| "appear"
| "enter"
| "exit"
| "disableTransition"
| "classNames"
| "timeout"
| "defaultFocus"
| "mountOnEnter"
| "unmountOnExit"
| "forceContainer"
| "disableScrollLock"
| "disableEscapeClose"
| "disableFocusContainer"
| "disableNestedDialogFixes"
| "portal"
| "overlayHidden"
| "component"
>
>;
type WithDefaultProps = StrictProps & DefaultProps & WithRef;
// used to disable the overlay click-to-close functionality when the `modal` prop is enabled.
const noop = (): void => {};
const block = bem("rmd-dialog");
const Dialog: FC<StrictProps & WithRef> = providedProps => {
const {
children,
forwardedRef,
className,
forceContainer,
containerStyle,
containerClassName,
overlay: propOverlay,
overlayStyle,
overlayClassName,
overlayHidden,
visible,
onRequestClose,
portal,
portalInto,
portalIntoId,
appear,
enter,
exit,
disableTransition,
classNames,
timeout,
mountOnEnter,
unmountOnExit,
onEnter,
onEntering,
onEntered,
onExit,
onExiting,
onExited,
modal,
role,
type,
disableScrollLock,
disableEscapeClose: propDisableEscapeClose,
disableFocusContainer,
disableFocusOnMount,
disableFocusOnUnmount,
disableNestedDialogFixes,
onKeyDown,
...props
} = providedProps as WithDefaultProps;
const { id } = props;
const isNoneRole = role === "none";
const isFullPage = type === "full-page";
const isCentered = type === "centered";
const { disableOverlay, disableEscapeClose } = useNestedDialogFixes({
id,
visible,
disabled: disableNestedDialogFixes,
disableEscapeClose: propDisableEscapeClose,
});
useScrollLock(visible && !isNoneRole && !disableScrollLock);
let overlayEl: ReactNode = null;
if (typeof propOverlay === "boolean" ? propOverlay : !isFullPage) {
// do not add the portal props to the overlay element since the portalling
// is handled in here. With how portals work, this would be rendered **after**
// the dialog instead of before which breaks some dialog styles
overlayEl = (
<Overlay
id={`${id}-overlay`}
style={overlayStyle}
className={cn("rmd-dialog-overlay", overlayClassName)}
hidden={overlayHidden || disableOverlay}
visible={visible}
clickable={!modal}
onRequestClose={modal ? noop : onRequestClose}
/>
);
}
let dialog = (
<FocusContainer
{...props}
role={isNoneRole ? undefined : role}
aria-modal={(!isNoneRole && !!overlayEl) || undefined}
disableTabFocusWrap={isNoneRole || disableFocusContainer}
disableFocusOnMount={
isNoneRole || disableFocusContainer || disableFocusOnMount
}
disableFocusOnUnmount={
isNoneRole || disableFocusContainer || disableFocusOnUnmount
}
onKeyDown={useCloseOnEscape(
onRequestClose,
disableEscapeClose || isNoneRole,
onKeyDown
)}
className={cn(
block({
centered: isCentered,
"full-page": isFullPage,
}),
className
)}
ref={forwardedRef}
>
{children}
</FocusContainer>
);
if (isCentered || forceContainer) {
// the additional container is only required when we don't have a full page dialog. it's just
// used to apply flex center to the dialog and add some margin
dialog = (
<span
id={`${id}-container`}
style={containerStyle}
className={cn("rmd-dialog-container", containerClassName)}
>
{dialog}
</span>
);
}
return (
<ConditionalPortal
portal={!isNoneRole && portal}
portalInto={portalInto}
portalIntoId={portalIntoId}
>
<Fragment>
{overlayEl}
<CSSTransition
appear={!disableTransition && appear}
enter={!disableTransition && enter}
exit={!disableTransition && exit}
in={visible}
classNames={classNames}
timeout={timeout}
onEnter={onEnter}
onEntering={onEntering}
onEntered={onEntered}
onExit={onExit}
onExiting={onExiting}
onExited={onExited}
mountOnEnter={mountOnEnter}
unmountOnExit={unmountOnExit}
>
{dialog}
</CSSTransition>
</Fragment>
</ConditionalPortal>
);
};
const defaultProps: DefaultProps = {
role: "dialog",
type: "centered",
tabIndex: -1,
portal: true,
modal: false,
overlayHidden: false,
mountOnEnter: true,
unmountOnExit: true,
appear: false,
enter: true,
exit: true,
disableTransition: false,
timeout: {
enter: 200,
exit: 150,
},
classNames: {
appear: "rmd-dialog--enter",
appearActive: "rmd-dialog--enter-active",
enter: "rmd-dialog--enter",
enterActive: "rmd-dialog--enter-active",
exit: "rmd-dialog--exit",
exitActive: "rmd-dialog--exit-active",
},
component: "div",
defaultFocus: "first",
forceContainer: false,
disableScrollLock: false,
disableEscapeClose: false,
disableFocusContainer: false,
disableNestedDialogFixes: false,
};
Dialog.defaultProps = defaultProps;
if (process.env.NODE_ENV !== "production") {
Dialog.displayName = "Dialog";
let PropTypes = null;
try {
PropTypes = require("prop-types");
} catch (e) {}
if (PropTypes) {
Dialog.propTypes = {
id: PropTypes.string.isRequired,
role: PropTypes.oneOf(["dialog", "alertdialog", "menu", "none"]),
"aria-label": PropTypes.string,
"aria-labelledby": PropTypes.string,
className: PropTypes.string,
type: PropTypes.oneOf(["custom", "centered", "full-page"]),
tabIndex: PropTypes.number,
modal: PropTypes.bool,
visible: PropTypes.bool.isRequired,
onRequestClose: PropTypes.func.isRequired,
mountOnEnter: PropTypes.bool,
unmountOnExit: PropTypes.bool,
overlay: PropTypes.bool,
overlayStyle: PropTypes.object,
overlayClassName: PropTypes.string,
overlayHidden: PropTypes.bool,
containerStyle: PropTypes.object,
containerClassName: PropTypes.string,
forceContainer: PropTypes.bool,
component: PropTypes.oneOf(["div", "nav"]),
children: PropTypes.node,
classNames: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
appear: PropTypes.string,
appearActive: PropTypes.string,
enter: PropTypes.string,
enterActive: PropTypes.string,
enterDone: PropTypes.string,
exit: PropTypes.string,
exitActive: PropTypes.string,
exitDone: PropTypes.string,
}),
]),
timeout: PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
enter: PropTypes.number,
exit: PropTypes.number,
}),
]),
appear: PropTypes.bool,
enter: PropTypes.bool,
exit: PropTypes.bool,
disableTransition: PropTypes.bool,
onEnter: PropTypes.func,
onEntering: PropTypes.func,
onEntered: PropTypes.func,
onExit: PropTypes.func,
onExiting: PropTypes.func,
onExited: PropTypes.func,
portal: PropTypes.bool,
portalInto: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.object,
]),
portalIntoId: PropTypes.string,
defaultFocus: PropTypes.oneOfType([
PropTypes.oneOf(["first", "last"]),
PropTypes.string,
]),
disableScrollLock: PropTypes.bool,
disableEscapeClose: PropTypes.bool,
disableFocusContainer: PropTypes.bool,
disableNestedDialogFixes: PropTypes.bool,
};
}
}
export default forwardRef<HTMLDivElement, StrictProps>((props, ref) => (
<Dialog {...props} forwardedRef={ref} />
));