Clicks outside a dialog can trigger elements "behind" the dialog backdrop
See original GitHub issueWhat package within Headless UI are you using? @headlessui/react
What version of that package are you using? v1.6.4
What browser are you using? Reproducible in Chrome on Android and Chrome in Touch Device mode
Reproduction URL https://kui8lz.csb.app/
Describe your issue
The dialog will immediately reopen if you click the toggle button as you can see in this video.
This only happens in a specific configuration:
- The toggle button is visible while the dialog is open.
- You tap on the toggle button while the dialog is open.
- The touchup event happens after the out transition finished. So you either have a fast transition or you tap slowly.
- the outside-click is handled outside of React callbacks, which is why React does a rerender in the closed state before calling the toggle button. This makes the toggle button open the dialog again.
I would expect either
- the outside click captures the event and my listener on the toggle button is not called OR
- the toggle button receives the event while still in the open state
Possible fixes
- We add
event.preventDefault()
here this would be my preferred solution, because it emphasizes the “modal” aspect- We make this behavior optIn by adding a prop such as
captureCloseEvent: boolean
- We make this behavior optIn by adding a prop such as
- We pass the event to the onClose function so the lib use can decide what to do
- We use the return value of onClose to decide whether to call
preventDefault
or not
If you tell me which fix you prefer, I’ll be happy to make a PR.
Thanks for the good work! 🥳
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6
Top Results From Across the Web
How to close the new html <dialog> tag by clicking on its
Backdrop clicks can be detected using the dialog bounding rect. var dialog = document.getElementsByTagName('dialog')[0]; dialog.
Read more >The Dialog element - HTML: HyperText Markup Language
The ::backdrop CSS pseudo-element can be used to style the backdrop that is displayed behind a <dialog> element when the dialog is displayed ......
Read more >A look at the dialog element's super powers - Stefan Judis
If you open a modal dialog, you can't click other elements anymore and that's not only because the backdrop covers them. If you...
Read more >Some Hands-On with the HTML Dialog Element | CSS-Tricks
This is me looking at the HTML <dialog> element for the first time. ... A common UI pattern is to darken the background...
Read more >click outside mdl-dialog to dismiss · Issue #5030 - GitHub
It might be worth adding support for this to mdl-dialog, such as by adding a special CSS class. The mdl javascript could automatically...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Awesome that it works!
We switched back to a
click
listener (which completes on keyup). This will be included in the next release. Thanks for testing it out!Hi, may I know ETA for the next release?
Thanks