Skip to content

Commit f9c15ef

Browse files
committed
Fix click from inside to outside for modal backdrop
1 parent dd217b4 commit f9c15ef

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/components/modal/Modal.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useRef } from 'react';
22
import PropTypes from 'prop-types';
33
import ReactDOM from 'react-dom';
44
import cx from 'classnames';
@@ -34,6 +34,8 @@ const Modal = React.forwardRef((props, ref) => {
3434
} = props;
3535

3636
const modal = useModal(visible, onToggle);
37+
const waitingForMouseUp = useRef(false);
38+
const ignoreBackdropClick = useRef(false);
3739

3840
// Return null if not mounted.
3941
if (!modal.mounted || !modal.visible) {
@@ -67,25 +69,43 @@ const Modal = React.forwardRef((props, ref) => {
6769
return;
6870
}
6971

70-
if (event.target === modal.ref.current) {
71-
modal.setVisible(false);
72+
if (
73+
ignoreBackdropClick.current ||
74+
event.target !== event.currentTarget
75+
) {
76+
ignoreBackdropClick.current = false;
77+
return;
7278
}
79+
80+
modal.setVisible(false);
81+
}}
82+
onMouseUp={(event) => {
83+
if (waitingForMouseUp.current && event.target === modal.ref.current) {
84+
ignoreBackdropClick.current = true;
85+
}
86+
87+
waitingForMouseUp.current = false;
7388
}}
7489
onKeyUp={(event) => {
75-
if (event.key === 'Escape') {
76-
event.preventDefault();
90+
if (event.key !== 'Escape') {
91+
return;
92+
}
7793

78-
if (backdrop === 'static') {
79-
return;
80-
}
94+
event.preventDefault();
8195

82-
modal.setVisible(false);
96+
if (backdrop === 'static') {
97+
return;
8398
}
99+
100+
modal.setVisible(false);
84101
}}
85102
essentials={{ className: 'modal show' }}
86103
>
87104
<BaseView
88105
accessibilityRole="document"
106+
onMouseDown={() => {
107+
waitingForMouseUp.current = true;
108+
}}
89109
essentials={{ className: dialogClasses }}
90110
>
91111
<ModalContext.Provider value={modal}>

0 commit comments

Comments
 (0)