Skip to content

Commit 8fc052a

Browse files
authored
[Dialog][Modal] Remove disableBackdropClick (#23607)
1 parent e28f1af commit 8fc052a

File tree

13 files changed

+87
-53
lines changed

13 files changed

+87
-53
lines changed

docs/pages/api-docs/dialog.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ The `MuiDialog` name can be used for providing [default props](/customization/gl
3232
| <span class="prop-name">aria-labelledby</span> | <span class="prop-type">string</span> | | The id(s) of the element(s) that label the dialog. |
3333
| <span class="prop-name">children</span> | <span class="prop-type">node</span> | | Dialog children, usually the included sub-components. |
3434
| <span class="prop-name">classes</span> | <span class="prop-type">object</span> | | Override or extend the styles applied to the component. See [CSS API](#css) below for more details. |
35-
| <span class="prop-name">disableBackdropClick</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, clicking the backdrop will not fire the `onClose` callback. |
3635
| <span class="prop-name">disableEscapeKeyDown</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, hitting escape will not fire the `onClose` callback. |
3736
| <span class="prop-name">fullScreen</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the dialog is full-screen. |
3837
| <span class="prop-name">fullWidth</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the dialog stretches to `maxWidth`.<br>Notice that the dialog width grow is limited by the default margin. |

docs/pages/api-docs/modal.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ This component shares many concepts with [react-overlays](https://react-bootstra
4242
| <span class="prop-name">closeAfterTransition</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | When set to true the Modal waits until a nested Transition is completed before closing. |
4343
| <span class="prop-name">container</span> | <span class="prop-type">HTML element<br>&#124;&nbsp;func</span> | | A HTML element or function that returns one. The `container` will have the portal children appended to it.<br>By default, it uses the body of the top-level document object, so it's simply `document.body` most of the time. |
4444
| <span class="prop-name">disableAutoFocus</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the modal will not automatically shift focus to itself when it opens, and replace it to the last focused element when it closes. This also works correctly with any modal children that have the `disableAutoFocus` prop.<br>Generally this should never be set to `true` as it makes the modal less accessible to assistive technologies, like screen readers. |
45-
| <span class="prop-name">disableBackdropClick</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, clicking the backdrop will not fire `onClose`. |
4645
| <span class="prop-name">disableEnforceFocus</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the modal will not prevent focus from leaving the modal while open.<br>Generally this should never be set to `true` as it makes the modal less accessible to assistive technologies, like screen readers. |
4746
| <span class="prop-name">disableEscapeKeyDown</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, hitting escape will not fire `onClose`. |
4847
| <span class="prop-name">disablePortal</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | The `children` will be inside the DOM hierarchy of the parent component. |

docs/src/pages/components/dialogs/ConfirmationDialog.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ function ConfirmationDialogRaw(props) {
6161

6262
return (
6363
<Dialog
64-
disableBackdropClick
65-
disableEscapeKeyDown
6664
maxWidth="xs"
6765
TransitionProps={{ onEntering: handleEntering }}
6866
aria-labelledby="confirmation-dialog-title"

docs/src/pages/components/dialogs/ConfirmationDialog.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ function ConfirmationDialogRaw(props: ConfirmationDialogRawProps) {
6969

7070
return (
7171
<Dialog
72-
disableBackdropClick
73-
disableEscapeKeyDown
7472
maxWidth="xs"
7573
TransitionProps={{ onEntering: handleEntering }}
7674
aria-labelledby="confirmation-dialog-title"

docs/src/pages/components/selects/DialogSelect.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,16 @@ export default function DialogSelect() {
3535
setOpen(true);
3636
};
3737

38-
const handleClose = () => {
39-
setOpen(false);
38+
const handleClose = (event, reason) => {
39+
if (reason !== 'backdropClick') {
40+
setOpen(false);
41+
}
4042
};
4143

4244
return (
4345
<div>
4446
<Button onClick={handleClickOpen}>Open select dialog</Button>
45-
<Dialog
46-
disableBackdropClick
47-
disableEscapeKeyDown
48-
open={open}
49-
onClose={handleClose}
50-
>
47+
<Dialog disableEscapeKeyDown open={open} onClose={handleClose}>
5148
<DialogTitle>Fill the form</DialogTitle>
5249
<DialogContent>
5350
<form className={classes.container}>

docs/src/pages/components/selects/DialogSelect.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ export default function DialogSelect() {
3737
setOpen(true);
3838
};
3939

40-
const handleClose = () => {
41-
setOpen(false);
40+
const handleClose = (
41+
event: React.SyntheticEvent<unknown>,
42+
reason?: string,
43+
) => {
44+
if (reason !== 'backdropClick') {
45+
setOpen(false);
46+
}
4247
};
4348

4449
return (
4550
<div>
4651
<Button onClick={handleClickOpen}>Open select dialog</Button>
47-
<Dialog
48-
disableBackdropClick
49-
disableEscapeKeyDown
50-
open={open}
51-
onClose={handleClose}
52-
>
52+
<Dialog disableEscapeKeyDown open={open} onClose={handleClose}>
5353
<DialogTitle>Fill the form</DialogTitle>
5454
<DialogContent>
5555
<form className={classes.container}>

docs/src/pages/guides/migration-v4/migration-v4.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,21 @@ const classes = makeStyles(theme => ({
412412
/>
413413
```
414414

415+
- Remove the `disableBackdropClick` prop because redundant.
416+
Ignore close events from `onClose` when `reason === 'backdropClick'` instead.
417+
418+
```diff
419+
<Dialog
420+
- disableBackdropClick
421+
- onClose={handleClose}
422+
+ onClose={(event, reason) => {
423+
+ if (reason !== 'backdropClick') {
424+
+ onClose(event, reason);
425+
+ }
426+
+ }}
427+
/>
428+
```
429+
415430
- [withMobileDialog] Remove this higher-order component. The hook API allows a simpler and more flexible solution:
416431

417432
```diff
@@ -589,6 +604,35 @@ const classes = makeStyles(theme => ({
589604

590605
### Modal
591606

607+
- Remove the `disableBackdropClick` prop because redundant.
608+
Ignore close events from `onClose` when `reason === 'backdropClick'` instead.
609+
610+
```diff
611+
<Modal
612+
- disableBackdropClick
613+
- onClose={handleClose}
614+
+ onClose={(event, reason) => {
615+
+ if (reason !== 'backdropClick') {
616+
+ onClose(event, reason);
617+
+ }
618+
+ }}
619+
/>
620+
```
621+
622+
- Remove the `onEscapeKeyDown` prop because redundant.
623+
Use `onClose` with `reason === "escapeKeyDown"` instead.
624+
625+
```diff
626+
<Modal
627+
- onEscapeKeyDown={handleEscapeKeyDown}
628+
+ onClose={(event, reason) => {
629+
+ if (reason === 'escapeKeyDown') {
630+
+ handleEscapeKeyDown(event);
631+
+ }
632+
+ }}
633+
/>
634+
```
635+
592636
- Remove `onRendered` prop.
593637
Depending on your use case either use a [callback ref](https://reactjs.org/docs/refs-and-the-dom.html#callback-refs) on the child element or an effect hook in the child component.
594638

packages/material-ui/src/Dialog/Dialog.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ export interface DialogProps
5353
/** Styles applied to the `Paper` component if `fullScreen={true}`. */
5454
paperFullScreen?: string;
5555
};
56-
/**
57-
* If `true`, clicking the backdrop will not fire the `onClose` callback.
58-
* @default false
59-
*/
60-
disableBackdropClick?: boolean;
6156
/**
6257
* If `true`, hitting escape will not fire the `onClose` callback.
6358
* @default false

packages/material-ui/src/Dialog/Dialog.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ const Dialog = React.forwardRef(function Dialog(props, ref) {
145145
children,
146146
classes,
147147
className,
148-
disableBackdropClick = false,
149148
disableEscapeKeyDown = false,
150149
fullScreen = false,
151150
fullWidth = false,
@@ -182,7 +181,7 @@ const Dialog = React.forwardRef(function Dialog(props, ref) {
182181
onBackdropClick(event);
183182
}
184183

185-
if (!disableBackdropClick && onClose) {
184+
if (onClose) {
186185
onClose(event, 'backdropClick');
187186
}
188187
};
@@ -196,7 +195,6 @@ const Dialog = React.forwardRef(function Dialog(props, ref) {
196195
...BackdropProps,
197196
}}
198197
closeAfterTransition
199-
disableBackdropClick={disableBackdropClick}
200198
disableEscapeKeyDown={disableEscapeKeyDown}
201199
onClose={onClose}
202200
open={open}
@@ -272,11 +270,6 @@ Dialog.propTypes = {
272270
* @ignore
273271
*/
274272
className: PropTypes.string,
275-
/**
276-
* If `true`, clicking the backdrop will not fire the `onClose` callback.
277-
* @default false
278-
*/
279-
disableBackdropClick: PropTypes.bool,
280273
/**
281274
* If `true`, hitting escape will not fire the `onClose` callback.
282275
* @default false

packages/material-ui/src/Dialog/Dialog.test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,26 @@ describe('<Dialog />', () => {
115115
});
116116

117117
it('can ignore backdrop click and Esc keydown', () => {
118+
function DialogWithBackdropClickDisabled(props) {
119+
const { onClose, ...other } = props;
120+
function handleClose(event, reason) {
121+
if (reason !== 'backdropClick') {
122+
onClose(event, reason);
123+
}
124+
}
125+
126+
return <Dialog onClose={handleClose} {...other} />;
127+
}
118128
const onClose = spy();
119129
const { getByRole } = render(
120-
<Dialog
130+
<DialogWithBackdropClickDisabled
121131
open
122-
disableBackdropClick
123132
disableEscapeKeyDown
124133
onClose={onClose}
125134
transitionDuration={0}
126135
>
127136
foo
128-
</Dialog>,
137+
</DialogWithBackdropClickDisabled>,
129138
);
130139
const dialog = getByRole('dialog');
131140
expect(dialog).not.to.equal(null);

0 commit comments

Comments
 (0)