question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clicks outside a dialog can trigger elements "behind" the dialog backdrop

See original GitHub issue

What 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:

  1. The toggle button is visible while the dialog is open.
  2. You tap on the toggle button while the dialog is open.
  3. 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 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
RobinMalfaitcommented, Jun 3, 2022

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!

0reactions
fer-ricommented, Jun 10, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found