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

fix(Dialog): track mousedown event to prevent accidental closing #4986

Conversation

francinelucca
Copy link
Contributor

@francinelucca francinelucca commented Sep 18, 2024

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 last mousedown event originated on the backdrop.

Changed

  • Modify onBackdropClick logic to only trigger the onClose callback if the mousedown event originates on the backdrop (i.e., lastMouseDownIsBackdrop is true).

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

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

Copy link

changeset-bot bot commented Sep 18, 2024

🦋 Changeset detected

Latest commit: 159567c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

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) {
Copy link
Contributor Author

@francinelucca francinelucca Sep 18, 2024

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!

@github-actions github-actions bot temporarily deployed to storybook-preview-4986 September 18, 2024 18:35 Inactive
Copy link
Contributor

github-actions bot commented Sep 18, 2024

size-limit report 📦

Path Size
packages/react/dist/browser.esm.js 97.48 KB (-0.06% 🔽)
packages/react/dist/browser.umd.js 97.75 KB (+0.05% 🔺)

@primer-integration
Copy link

👋 Hi from github/github! Your integration PR is ready: https://github.com/github/github/pull/342878

@francinelucca francinelucca changed the title WIP: fix(Dialog): track mousedown event to prevent accidental closing fix(Dialog): track mousedown event to prevent accidental closing Sep 19, 2024
@francinelucca francinelucca added staff Author is a staff member status: review needed labels Sep 19, 2024
@francinelucca francinelucca marked this pull request as ready for review September 19, 2024 13:20
@francinelucca francinelucca requested a review from a team as a code owner September 19, 2024 13:20
…revent-dialog-from-closing-on-accidental-mouse-release
@keithamus
Copy link
Member

Do you think it's viable to write a test for this?

@francinelucca
Copy link
Contributor Author

francinelucca commented Sep 19, 2024

Do you think it's viable to write a test for this?

So I tried using fireEvent.mouseUp/mouseDown but couldn't get a combination of mouseDown/mouseUp to trigger a click on the backdrop so wasn't able to simulate this scenario. Any suggestions? I need to "start" the click at the dialog and "end" it in the backdrop 😅 @keithamus

@keithamus
Copy link
Member

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
Comment on lines 88 to 106
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()
})

Copy link
Contributor Author

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 🤔

Copy link
Member

@keithamus keithamus left a 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.

@francinelucca francinelucca added this pull request to the merge queue Sep 19, 2024
Merged via the queue into main with commit d6471aa Sep 19, 2024
32 checks passed
@francinelucca francinelucca deleted the francinelucca/3477-prcdialog-best-practice-prevent-dialog-from-closing-on-accidental-mouse-release branch September 19, 2024 20:18
@primer primer bot mentioned this pull request Sep 19, 2024
TylerJDev pushed a commit that referenced this pull request Sep 23, 2024
* 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
@primer primer bot mentioned this pull request Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
staff Author is a staff member status: review needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants