Skip to content

Commit d039942

Browse files
committed
Fix modal closing when selection ended outside of the modal (#645)
The modal would close when the user selected something outside the modal and ended the selection outside of the modal.
1 parent 8ddc79b commit d039942

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/components/Modal/Modal.jsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const Modal = ({
6161
...restProps
6262
}) => {
6363
const internalDialogRef = useRef();
64+
const mouseDownTarget = useRef(null);
6465

6566
useEffect(() => {
6667
internalDialogRef.current.showModal();
@@ -79,7 +80,13 @@ export const Modal = ({
7980
[closeButtonRef, restProps.onCancel],
8081
);
8182
const onClick = useCallback(
82-
(e) => dialogOnClickHandler(e, closeButtonRef, internalDialogRef, allowCloseOnBackdropClick),
83+
(e) => dialogOnClickHandler(
84+
e,
85+
closeButtonRef,
86+
internalDialogRef,
87+
allowCloseOnBackdropClick,
88+
mouseDownTarget.current,
89+
),
8390
[allowCloseOnBackdropClick, closeButtonRef, internalDialogRef],
8491
);
8592
const onClose = useCallback(
@@ -101,11 +108,17 @@ export const Modal = ({
101108
primaryButtonRef,
102109
],
103110
);
111+
112+
const onMouseDown = useCallback((e) => {
113+
mouseDownTarget.current = e.target;
114+
}, []);
115+
104116
const events = {
105117
onCancel,
106118
onClick,
107119
onClose,
108120
onKeyDown,
121+
onMouseDown,
109122
};
110123

111124
if (portalId === null) {

src/components/Modal/_helpers/dialogOnClickHandler.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ export const dialogOnClickHandler = (
1717
closeButtonRef,
1818
dialogRef,
1919
allowCloseOnBackdropClick,
20+
mouseDownTarget,
2021
) => {
2122
// If it is not allowed to close modal on backdrop click, do nothing.
2223
if (!allowCloseOnBackdropClick) {
2324
return;
2425
}
2526

27+
// If the click started on the inside of the dialog, do nothing.
28+
if (e.target !== mouseDownTarget) {
29+
return;
30+
}
31+
2632
// Detection of the click on the backdrop is based on the following conditions:
2733
// 1. The click target is the dialog itself. This prevents detection of clicks on the dialog's children.
2834
// 2. The click is outside the dialog's boundaries.

0 commit comments

Comments
 (0)