Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/components/Modal/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const Modal = ({
...restProps
}) => {
const internalDialogRef = useRef();
const mouseDownTarget = useRef(null);

useEffect(() => {
internalDialogRef.current.showModal();
Expand All @@ -79,7 +80,13 @@ export const Modal = ({
[closeButtonRef, restProps.onCancel],
);
const onClick = useCallback(
(e) => dialogOnClickHandler(e, closeButtonRef, internalDialogRef, allowCloseOnBackdropClick),
(e) => dialogOnClickHandler(
e,
closeButtonRef,
internalDialogRef,
allowCloseOnBackdropClick,
mouseDownTarget.current,
),
[allowCloseOnBackdropClick, closeButtonRef, internalDialogRef],
);
const onClose = useCallback(
Expand All @@ -101,11 +108,17 @@ export const Modal = ({
primaryButtonRef,
],
);

const onMouseDown = useCallback((e) => {
mouseDownTarget.current = e.target;
}, []);

const events = {
onCancel,
onClick,
onClose,
onKeyDown,
onMouseDown,
};

if (portalId === null) {
Expand Down
6 changes: 6 additions & 0 deletions src/components/Modal/_helpers/dialogOnClickHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ export const dialogOnClickHandler = (
closeButtonRef,
dialogRef,
allowCloseOnBackdropClick,
mouseDownTarget,
) => {
// If it is not allowed to close modal on backdrop click, do nothing.
if (!allowCloseOnBackdropClick) {
return;
}

// If the click started on the inside of the dialog, do nothing.
if (e.target !== mouseDownTarget) {
return;
}

// Detection of the click on the backdrop is based on the following conditions:
// 1. The click target is the dialog itself. This prevents detection of clicks on the dialog's children.
// 2. The click is outside the dialog's boundaries.
Expand Down