headlessui: [Bug]: Interacting with third-party components that use portals inside a Dialog closes the Dialog
What package within Headless UI are you using?
What version of that package are you using?
1.0.0
What browser are you using?
Firefox
Reproduction repository
https://codesandbox.io/s/pensive-fast-yxokp?file=/src/App.vue
Describe your issue
When any component inside a Dialog renders itself elsewhere in the DOM using a Portal/Teleport, clicking in it closes the entire Dialog. The example above uses vue-flatpickr-component
to create a date picker inside a modal, but because it renders the actual flatpickr instance somewhere else in the DOM, clicking any date closes the entire modal.
The docs on this say:
When a Dialog is rendered, clicking the
DialogOverlay
will close the Dialog.
But that isn’t exactly the case—it’s not clicking the DialogOverlay
specifically that closes the Dialog, but clicking anything that is not a child of the Dialog in the DOM.
I believe the relevant part of the code is here: https://github.com/tailwindlabs/headlessui/blob/main/packages/%40headlessui-vue/src/components/dialog/dialog.ts#L174-L184
What do you think about adding a way to disable that global mousedown
handler, or adding an option to actually only close the Dialog based on clicks directly on the DialogOverlay
component?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 20
- Comments: 30 (1 by maintainers)
Hey! Thank you for your bug report! Much appreciated! 🙏
This should be fixed #1268, and will be available in the next release.
You can already try it using:
npm install @headlessui/react@insiders
oryarn add @headlessui/react@insiders
.npm install @headlessui/vue@insiders
oryarn add @headlessui/vue@insiders
.I’ve updated your CodeSandbox with the latest insiders build: https://codesandbox.io/s/sweet-tree-egth6u
I opened a PR to fix the issue for the React-specific version in https://github.com/tailwindlabs/headlessui/pull/989. Not sure though whether this fix is also applicable in Vue since it relies on the event delegation system of React which also propagates events through portals.
I can confirm the issue. I use dialog with scrollable content that contains a selectbox. The selectbox options list is rendered at body level in a teleport and positioned with popper. Clicking on the options list will close the dialog.
same bug. What’s taking so long?
This could work. The issue is now with other way to close the dialog.
The quick solution would be to remove the
@close
handler on theDialog
component and adding@click
handler on theDialogOverlay
to emit theclose
event.You still need to handle other way of closing the dialog, like pressing the Escape key.
I don’t know if there are other things I am missing, here is an example.
is there a workaround for this? seeing the same issue with
@headlessui/react@1.2.0
After testing the solution I have proposed, there are still issues with
flatpickr
. Somehow, theyear
input is not focusable when it is inside theDialog
component.Hey there! 👋🏻
Small bump of the issue.
Any news about the fix?
i have the same issue. when i use some element over the dialog and click the element, dialog will be close
What is the fix/solution or status of this? I would really like to use headless UI for dialogs and other things but cannot if it breaks my existing usage of third party libraries such as flatpickr.
I am using CKeditor BalloonEditor the Modal also close after clicking editor toolbar. Plz fix soon. Thank you.
I have the same issue as described, clicking on the calendar provided by devextreme closes the Dialog. is there a workaround for this?
I have the same issue as described, clicking on the calendar provided by
flatpickr
closes the Dialog.