Skip to content

Commit

Permalink
fix: fix modal closing behavior (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirovladimitrovski authored Nov 20, 2024
1 parent 9adddea commit b65f0c5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/ui-react/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const Modal: React.FC<Props> = ({
...ariaAttributes
}: Props) => {
const modalRef = useRef<HTMLDialogElement>() as React.MutableRefObject<HTMLDialogElement>;
const mouseEventTargetsPairRef = useRef<Record<'downTarget' | 'upTarget', HTMLElement | null>>({ downTarget: null, upTarget: null });

const { handleOpen, handleClose } = useModal();

Expand Down Expand Up @@ -89,6 +90,15 @@ const Modal: React.FC<Props> = ({
}, [openModalEvent, closeModalEvent, open]);

const clickHandler: ReactEventHandler<HTMLDialogElement> = (event) => {
const {
current: { downTarget, upTarget },
} = mouseEventTargetsPairRef;

// modal should only close if both mousedown and mouseup are done on the overlay
if (downTarget !== upTarget) {
return;
}

// Backdrop click (the dialog itself) will close the modal
if (event.target === modalRef.current) {
onClose?.();
Expand All @@ -102,6 +112,12 @@ const Modal: React.FC<Props> = ({
onKeyDown={keyDownHandler}
onClose={closeHandler}
onClick={clickHandler}
onMouseDown={(e) => {
mouseEventTargetsPairRef.current.downTarget = e.target as HTMLElement;
}}
onMouseUp={(e) => {
mouseEventTargetsPairRef.current.upTarget = e.target as HTMLElement;
}}
ref={modalRef}
role={role}
{...ariaAttributes}
Expand Down

0 comments on commit b65f0c5

Please sign in to comment.