-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathDialog.js
561 lines (531 loc) · 16 KB
/
Dialog.js
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import useId from '@mui/utils/useId';
import capitalize from '../utils/capitalize';
import Modal from '../Modal';
import Fade from '../Fade';
import Paper from '../Paper';
import dialogClasses, { getDialogUtilityClass } from './dialogClasses';
import DialogContext from './DialogContext';
import Backdrop from '../Backdrop';
import { styled, useTheme } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import useSlot from '../utils/useSlot';
const DialogBackdrop = styled(Backdrop, {
name: 'MuiDialog',
slot: 'Backdrop',
overrides: (props, styles) => styles.backdrop,
})({
// Improve scrollable dialog support.
zIndex: -1,
});
const useUtilityClasses = (ownerState) => {
const { classes, scroll, maxWidth, fullWidth, fullScreen } = ownerState;
const slots = {
root: ['root'],
container: ['container', `scroll${capitalize(scroll)}`],
paper: [
'paper',
`paperScroll${capitalize(scroll)}`,
`paperWidth${capitalize(String(maxWidth))}`,
fullWidth && 'paperFullWidth',
fullScreen && 'paperFullScreen',
],
};
return composeClasses(slots, getDialogUtilityClass, classes);
};
const DialogRoot = styled(Modal, {
name: 'MuiDialog',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})({
'@media print': {
// Use !important to override the Modal inline-style.
position: 'absolute !important',
},
});
const DialogContainer = styled('div', {
name: 'MuiDialog',
slot: 'Container',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [styles.container, styles[`scroll${capitalize(ownerState.scroll)}`]];
},
})({
height: '100%',
'@media print': {
height: 'auto',
},
// We disable the focus ring for mouse, touch and keyboard users.
outline: 0,
variants: [
{
props: {
scroll: 'paper',
},
style: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
},
},
{
props: {
scroll: 'body',
},
style: {
overflowY: 'auto',
overflowX: 'hidden',
textAlign: 'center',
'&::after': {
content: '""',
display: 'inline-block',
verticalAlign: 'middle',
height: '100%',
width: '0',
},
},
},
],
});
const DialogPaper = styled(Paper, {
name: 'MuiDialog',
slot: 'Paper',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.paper,
styles[`scrollPaper${capitalize(ownerState.scroll)}`],
styles[`paperWidth${capitalize(String(ownerState.maxWidth))}`],
ownerState.fullWidth && styles.paperFullWidth,
ownerState.fullScreen && styles.paperFullScreen,
];
},
})(
memoTheme(({ theme }) => ({
margin: 32,
position: 'relative',
overflowY: 'auto',
'@media print': {
overflowY: 'visible',
boxShadow: 'none',
},
variants: [
{
props: {
scroll: 'paper',
},
style: {
display: 'flex',
flexDirection: 'column',
maxHeight: 'calc(100% - 64px)',
},
},
{
props: {
scroll: 'body',
},
style: {
display: 'inline-block',
verticalAlign: 'middle',
textAlign: 'initial',
},
},
{
props: ({ ownerState }) => !ownerState.maxWidth,
style: {
maxWidth: 'calc(100% - 64px)',
},
},
{
props: {
maxWidth: 'xs',
},
style: {
maxWidth:
theme.breakpoints.unit === 'px'
? Math.max(theme.breakpoints.values.xs, 444)
: `max(${theme.breakpoints.values.xs}${theme.breakpoints.unit}, 444px)`,
[`&.${dialogClasses.paperScrollBody}`]: {
[theme.breakpoints.down(Math.max(theme.breakpoints.values.xs, 444) + 32 * 2)]: {
maxWidth: 'calc(100% - 64px)',
},
},
},
},
...Object.keys(theme.breakpoints.values)
.filter((maxWidth) => maxWidth !== 'xs')
.map((maxWidth) => ({
props: { maxWidth },
style: {
maxWidth: `${theme.breakpoints.values[maxWidth]}${theme.breakpoints.unit}`,
[`&.${dialogClasses.paperScrollBody}`]: {
[theme.breakpoints.down(theme.breakpoints.values[maxWidth] + 32 * 2)]: {
maxWidth: 'calc(100% - 64px)',
},
},
},
})),
{
props: ({ ownerState }) => ownerState.fullWidth,
style: {
width: 'calc(100% - 64px)',
},
},
{
props: ({ ownerState }) => ownerState.fullScreen,
style: {
margin: 0,
width: '100%',
maxWidth: '100%',
height: '100%',
maxHeight: 'none',
borderRadius: 0,
[`&.${dialogClasses.paperScrollBody}`]: {
margin: 0,
maxWidth: '100%',
},
},
},
],
})),
);
/**
* Dialogs are overlaid modal paper based components with a backdrop.
*/
const Dialog = React.forwardRef(function Dialog(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiDialog' });
const theme = useTheme();
const defaultTransitionDuration = {
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
};
const {
'aria-describedby': ariaDescribedby,
'aria-labelledby': ariaLabelledbyProp,
'aria-modal': ariaModal = true,
BackdropComponent,
BackdropProps,
children,
className,
disableEscapeKeyDown = false,
fullScreen = false,
fullWidth = false,
maxWidth = 'sm',
onBackdropClick,
onClick,
onClose,
open,
PaperComponent = Paper,
PaperProps = {},
scroll = 'paper',
slots = {},
slotProps = {},
TransitionComponent = Fade,
transitionDuration = defaultTransitionDuration,
TransitionProps,
...other
} = props;
const ownerState = {
...props,
disableEscapeKeyDown,
fullScreen,
fullWidth,
maxWidth,
scroll,
};
const classes = useUtilityClasses(ownerState);
const backdropClick = React.useRef();
const handleMouseDown = (event) => {
// We don't want to close the dialog when clicking the dialog content.
// Make sure the event starts and ends on the same DOM element.
backdropClick.current = event.target === event.currentTarget;
};
const handleBackdropClick = (event) => {
if (onClick) {
onClick(event);
}
// Ignore the events not coming from the "backdrop".
if (!backdropClick.current) {
return;
}
backdropClick.current = null;
if (onBackdropClick) {
onBackdropClick(event);
}
if (onClose) {
onClose(event, 'backdropClick');
}
};
const ariaLabelledby = useId(ariaLabelledbyProp);
const dialogContextValue = React.useMemo(() => {
return { titleId: ariaLabelledby };
}, [ariaLabelledby]);
const backwardCompatibleSlots = {
transition: TransitionComponent,
...slots,
};
const backwardCompatibleSlotProps = {
transition: TransitionProps,
paper: PaperProps,
backdrop: BackdropProps,
...slotProps,
};
const externalForwardedProps = {
slots: backwardCompatibleSlots,
slotProps: backwardCompatibleSlotProps,
};
const [RootSlot, rootSlotProps] = useSlot('root', {
elementType: DialogRoot,
shouldForwardComponentProp: true,
externalForwardedProps,
ownerState,
className: clsx(classes.root, className),
ref,
});
const [BackdropSlot, backdropSlotProps] = useSlot('backdrop', {
elementType: DialogBackdrop,
shouldForwardComponentProp: true,
externalForwardedProps,
ownerState,
});
const [PaperSlot, paperSlotProps] = useSlot('paper', {
elementType: DialogPaper,
shouldForwardComponentProp: true,
externalForwardedProps,
ownerState,
className: clsx(classes.paper, PaperProps.className),
});
const [ContainerSlot, containerSlotProps] = useSlot('container', {
elementType: DialogContainer,
externalForwardedProps,
ownerState,
className: clsx(classes.container),
});
const [TransitionSlot, transitionSlotProps] = useSlot('transition', {
elementType: Fade,
externalForwardedProps,
ownerState,
additionalProps: {
appear: true,
in: open,
timeout: transitionDuration,
role: 'presentation',
},
});
return (
<RootSlot
closeAfterTransition
slots={{ backdrop: BackdropSlot }}
slotProps={{
backdrop: {
transitionDuration,
as: BackdropComponent,
...backdropSlotProps,
},
}}
disableEscapeKeyDown={disableEscapeKeyDown}
onClose={onClose}
open={open}
onClick={handleBackdropClick}
{...rootSlotProps}
{...other}
>
<TransitionSlot {...transitionSlotProps}>
{/* roles are applied via cloneElement from TransitionComponent */}
{/* roles needs to be applied on the immediate child of Modal or it'll inject one */}
<ContainerSlot onMouseDown={handleMouseDown} {...containerSlotProps}>
<PaperSlot
as={PaperComponent}
elevation={24}
role="dialog"
aria-describedby={ariaDescribedby}
aria-labelledby={ariaLabelledby}
aria-modal={ariaModal}
{...paperSlotProps}
>
<DialogContext.Provider value={dialogContextValue}>{children}</DialogContext.Provider>
</PaperSlot>
</ContainerSlot>
</TransitionSlot>
</RootSlot>
);
});
Dialog.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* The id(s) of the element(s) that describe the dialog.
*/
'aria-describedby': PropTypes.string,
/**
* The id(s) of the element(s) that label the dialog.
*/
'aria-labelledby': PropTypes.string,
/**
* Informs assistive technologies that the element is modal.
* It's added on the element with role="dialog".
* @default true
*/
'aria-modal': PropTypes.oneOfType([PropTypes.oneOf(['false', 'true']), PropTypes.bool]),
/**
* A backdrop component. This prop enables custom backdrop rendering.
* @deprecated Use `slots.backdrop` instead. While this prop currently works, it will be removed in the next major version.
* Use the `slots.backdrop` prop to make your application ready for the next version of Material UI.
* @default styled(Backdrop, {
* name: 'MuiModal',
* slot: 'Backdrop',
* overridesResolver: (props, styles) => {
* return styles.backdrop;
* },
* })({
* zIndex: -1,
* })
*/
BackdropComponent: PropTypes.elementType,
/**
* @ignore
*/
BackdropProps: PropTypes.object,
/**
* Dialog children, usually the included sub-components.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* If `true`, hitting escape will not fire the `onClose` callback.
* @default false
*/
disableEscapeKeyDown: PropTypes.bool,
/**
* If `true`, the dialog is full-screen.
* @default false
*/
fullScreen: PropTypes.bool,
/**
* If `true`, the dialog stretches to `maxWidth`.
*
* Notice that the dialog width grow is limited by the default margin.
* @default false
*/
fullWidth: PropTypes.bool,
/**
* Determine the max-width of the dialog.
* The dialog width grows with the size of the screen.
* Set to `false` to disable `maxWidth`.
* @default 'sm'
*/
maxWidth: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl', false]),
PropTypes.string,
]),
/**
* Callback fired when the backdrop is clicked.
* @deprecated Use the `onClose` prop with the `reason` argument to handle the `backdropClick` events.
*/
onBackdropClick: PropTypes.func,
/**
* @ignore
*/
onClick: PropTypes.func,
/**
* Callback fired when the component requests to be closed.
*
* @param {object} event The event source of the callback.
* @param {string} reason Can be: `"escapeKeyDown"`, `"backdropClick"`.
*/
onClose: PropTypes.func,
/**
* If `true`, the component is shown.
*/
open: PropTypes.bool.isRequired,
/**
* The component used to render the body of the dialog.
* @default Paper
*/
PaperComponent: PropTypes.elementType,
/**
* Props applied to the [`Paper`](https://mui.com/material-ui/api/paper/) element.
* @default {}
* @deprecated Use `slotProps.paper` instead. This prop will be removed in v7. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
PaperProps: PropTypes.object,
/**
* Determine the container for scrolling the dialog.
* @default 'paper'
*/
scroll: PropTypes.oneOf(['body', 'paper']),
/**
* The props used for each slot inside.
* @default {}
*/
slotProps: PropTypes.shape({
backdrop: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
container: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
paper: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
transition: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
}),
/**
* The components used for each slot inside.
* @default {}
*/
slots: PropTypes.shape({
backdrop: PropTypes.elementType,
container: PropTypes.elementType,
paper: PropTypes.elementType,
root: PropTypes.elementType,
transition: PropTypes.elementType,
}),
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* The component used for the transition.
* [Follow this guide](https://mui.com/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
* @default Fade
* @deprecated Use `slots.transition` instead. This prop will be removed in v7. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
TransitionComponent: PropTypes.elementType,
/**
* The duration for the transition, in milliseconds.
* You may specify a single timeout for all transitions, or individually with an object.
* @default {
* enter: theme.transitions.duration.enteringScreen,
* exit: theme.transitions.duration.leavingScreen,
* }
*/
transitionDuration: PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
appear: PropTypes.number,
enter: PropTypes.number,
exit: PropTypes.number,
}),
]),
/**
* Props applied to the transition element.
* By default, the element is based on this [`Transition`](https://reactcommunity.org/react-transition-group/transition/) component.
* @deprecated Use `slotProps.transition` instead. This prop will be removed in v7. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details.
*/
TransitionProps: PropTypes.object,
};
export default Dialog;