-
Notifications
You must be signed in to change notification settings - Fork 536
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
fix(Dialog): track mousedown event to prevent accidental closing #4986
fix(Dialog): track mousedown event to prevent accidental closing #4986
Conversation
🦋 Changeset detectedLatest commit: 159567c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
const defaultedProps = {...props, title, subtitle, role, dialogLabelId, dialogDescriptionId} | ||
const onBackdropClick = useCallback( | ||
(e: SyntheticEvent) => { | ||
if (e.target === e.currentTarget) { | ||
if (e.target === e.currentTarget && lastMouseDownIsBackdrop) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the current test case (mouseDown on dialog, drag to backdrop, mouseUp), the e.target and e.currentTarget are both the same (the backdrop) since the click is generated at the backdrop on mouseUp. Circumventing this by explicitly checking that the mouseDown event that triggered the click was originated at the backdrop, but open to other ides on how to handle this if any recs!
size-limit report 📦
|
👋 Hi from github/github! Your integration PR is ready: https://github.com/github/github/pull/342878 |
…revent-dialog-from-closing-on-accidental-mouse-release
Do you think it's viable to write a test for this? |
So I tried using |
Yes I guess any test that manually fired events would be too contrived. I would just like to find a way to preserve the functionality against refactors. |
…og-from-closing-on-accidental-mouse-release' of github.com:primer/react into francinelucca/3477-prcdialog-best-practice-prevent-dialog-from-closing-on-accidental-mouse-release
it('does not call `onClose` when click was not originated from backdrop', async () => { | ||
const user = userEvent.setup() | ||
const onClose = jest.fn() | ||
|
||
const {getByRole} = render(<Dialog onClose={onClose}>Pay attention to me</Dialog>) | ||
|
||
expect(onClose).not.toHaveBeenCalled() | ||
|
||
const dialog = getByRole('dialog') | ||
const backdrop = dialog.parentElement! | ||
|
||
fireEvent.mouseDown(dialog) | ||
fireEvent.mouseUp(backdrop) | ||
// trigger the click on the backdrop, mouseUp doesn't do it for us | ||
fireEvent.click(backdrop) | ||
|
||
expect(onClose).not.toHaveBeenCalled() | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how do you feel about this @keithamus ? I do have to manually trigger the click
event but it will fail if the current implementation were to be refactored out 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for adding the test.
* fix(Dialog): track mousedown event to prevent accidental closing * Create eighty-houses-beg.md * test(Dialog): add test for accidental closure behavior * test(Dialog): remove unused var
Closes https://github.com/github/primer/issues/3477
Keeps a state of whether the last
mousedown
event originated on the backdrop to prevent closing the dialog when the backdrop is clicked accidentally.Changelog
New
lastMouseDownIsBackdrop
state variable to keep track of whether the lastmousedown
event originated on the backdrop.Changed
onBackdropClick
logic to only trigger theonClose
callback if themousedown
event originates on the backdrop (i.e.,lastMouseDownIsBackdrop
istrue
).Rollout strategy
Testing & Reviewing
Follow repro instructions from issue on preview deployment and validate behavior is no longer experienced.
Note: I tried to add a unit test for this but couldn't get a combination of
mouseDown
/mouseUp
to trigger a click on the backdrop so wasn't able to simulate this particular scenario. Open to any suggestions on how to do that.Merge checklist