headlessui: [Bug]: Dialog blocks click event listener

What package within Headless UI are you using?

@headlessui/vue

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.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 21 (2 by maintainers)

Most upvoted comments

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 the document is technically interacting with things behind the Dialog.

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

@RobinMalfait I echo the concerns of others in this thread - calling event.stopPropagation on all click events seems unnecessary, and is breaking how the intl-tel-input plugin works - basically any popup within the dialog is unclosable because the click-off-to-close listener is on the root document.documentElement and so never gets fired. In my experience, the normal way to handle this dialog click management situation, is to have the click listener on the grey background element, and check within the handler function that the element that was clicked on was indeed that grey element (hence ignoring any clicks within the dialog itself), if that makes sense? Is there a reason why this is not sufficient in this case? Thanks for your time.

@RobinMalfait would you please be kind enough to tell what are the reasons to do event.stopPropagation() in a listener put on document? What’s the benefit from putting it on document instead of the gray fixed-positioned overlay of a dialog?

I also ran into the same issue. We have a third-party script that expects an element with a specific selector to be present on the page and has a document click listener that looks for this element to be a target and then does its logic. Now I have to come up with some hacks to get around this problem because this button still has to be in a dialog. I believe this must be also an issue with Google Tag Manager as I am sure its click Tag works in the same way, which means tracking clicks with GTM inside the dialog most likely is not even working. @RobinMalfait I strongly recommend reconsidering this decision! Also, how am I supposed to even get around this? I literally need this to work, I cannot rewrite the 3rd party widget to account for this limitation.

I have the same issue. Click event is blocked within dialog. In my case I have a Click event that will hide or show some div within the dialog itself. How am I supposed to handle this? And I don’t understand why it is done on purpose! Like no one will ever need a click event within the dialog?

I am having the same issue. My tooltips will not close when I click inside the modal.

to solve this in react, wrap your other dialog you want clickable in a portal

Can we please reopen this? Because there are still cases, there you have the same issue. For my case we show some text which might contain links (text created of wysiwyg editor). So if the user clicks on a link, which is an external domain a popup should show up. But that’s not possible, because of the current behaviour. As mentioned mousedown would work, but that’s more complicated then it could be.

At least this behaviour should be mentioned in the documentation.

What do you think?