Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OWA-102: fix modal closing behavior #649

Merged
merged 1 commit into from
Nov 20, 2024
Merged
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
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
Loading