Skip to content

Commit

Permalink
Merge d831be3 into d1b48bc
Browse files Browse the repository at this point in the history
  • Loading branch information
francinelucca authored Sep 19, 2024
2 parents d1b48bc + d831be3 commit 6453f27
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-houses-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

fix(Dialog): track mousedown event to prevent accidental closing
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 6453f27

Please sign in to comment.