[Bug]: Dialog blocks click event listener
See original GitHub issueWhat package within Headless UI are you using?
What version of that package are you using?
1.4.1
What browser are you using?
Chrome
Reproduction repository
https://codesandbox.io/s/bug-headless-ui-dialog-click-outside-8n3jh
Describe your issue
Click event listener is not emitted on dialogs.
The problem with this is that I’m trying to make a dropdown that closes when clicking outside. To do that I want to add a global event listener for the click
event, but it’s not being triggered.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:7
Top Results From Across the Web
Violation 'click' handler took 239ms - When dialog rendering ...
I am using <Dialog></Dialog> Material UI to render the React Hook Form (form Popup). When the data is small. It is working well....
Read more >The Dangers of Stopping Event Propagation - CSS-Tricks
This bug happens because the Bootstrap code responsible for closing the dropdown menu is listening for click events on the document.
Read more >Inconsistent popup-blocker behavior with document ...
Bug 1379466 - Stop dispatching click events for non-primary mouse clicks on the web. 4 years ago. Ian Moody [:Kwan] (UTC+1).
Read more >How to Make Dialogs (The Java™ Tutorials > Creating a GUI ...
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane...
Read more >React: Event Bubbling and Capturing - Robin Wieruch
In conclusion, whenever there is a element with a handler nested in another element with a handler, where both are listening to the...
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
Hey! Thank you for your bug report! Much appreciated! 🙏
This is done on purpose. You should think of a
Dialog
as a separate page where you shouldn’t interact with things behind it. Putting an event listener on thedocument
is technically interacting with things behind theDialog
.If you want to implement a dropdown that closes when you click outside, then you should be able to use our
Menu
component that has this functionality outside of the box.Here is an example: https://codesandbox.io/s/bug-headless-ui-dialog-click-outside-forked-21t72y?file=/src/App.vue
The Dialog component blocks
click
event by default: Vue: https://github.com/tailwindlabs/headlessui/blob/6fc28c610f14a3ab88a425cf97a67cee9bcb8e71/packages/%40headlessui-vue/src/components/dialog/dialog.ts#L224-L226React: https://github.com/tailwindlabs/headlessui/blob/6fc28c610f14a3ab88a425cf97a67cee9bcb8e71/packages/%40headlessui-react/src/components/dialog/dialog.tsx#L285-L295
And unfortunately we can not override it, because these default props are destructed after the actual props we pass to the component: Vue: https://github.com/tailwindlabs/headlessui/blob/6fc28c610f14a3ab88a425cf97a67cee9bcb8e71/packages/%40headlessui-vue/src/components/dialog/dialog.ts#L250
React: https://github.com/tailwindlabs/headlessui/blob/6fc28c610f14a3ab88a425cf97a67cee9bcb8e71/packages/%40headlessui-react/src/components/dialog/dialog.tsx#L324
For now as work around, I have used
mousedown
instead ofclick
event.