Skip to content

Commit

Permalink
fix(Dialog): track mousedown event to prevent accidental closing
Browse files Browse the repository at this point in the history
  • Loading branch information
francinelucca committed Sep 18, 2024
1 parent 04e8c9c commit b79a346
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/react/src/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,15 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
footerButton.ref = autoFocusedFooterButtonRef
}
}
const [lastMouseDownIsBackdrop, setLastMouseDownIsBackdrop] = useState<boolean>(false)
const defaultedProps = {...props, title, subtitle, role, dialogLabelId, dialogDescriptionId}
const onBackdropClick = useCallback(
(e: SyntheticEvent) => {
if (e.target === e.currentTarget) {
if (e.target === e.currentTarget && lastMouseDownIsBackdrop) {
onClose('escape')
}
},
[onClose],
[onClose, lastMouseDownIsBackdrop],
)

const dialogRef = useRef<HTMLDivElement>(null)
Expand Down Expand Up @@ -479,7 +480,14 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
return (
<>
<Portal>
<Backdrop ref={backdropRef} {...positionDataAttributes} onClick={onBackdropClick}>
<Backdrop
ref={backdropRef}
{...positionDataAttributes}
onClick={onBackdropClick}
onMouseDown={e => {
setLastMouseDownIsBackdrop(e.target === e.currentTarget)
}}
>
<StyledDialog
width={width}
height={height}
Expand Down

0 comments on commit b79a346

Please sign in to comment.