ui: [bug]: Cannot interact with Sonner toasts opened from Dialogs (or other modal elements)
Describe the bug
If you create a sonner from within any modalic element (dialog, context menu, dropdown, popover), any interaction with the resulting sonner toast will result in the modal element closing as well.
This issue was previously described in https://github.com/shadcn-ui/ui/issues/2401. However, increasing the z-index of the sonner group does not resolve the issue, and registering custom onInteractOutside handlers for every modalic component is an unwieldy option. Ideally, one should be able to interact with the sonners while a dialog is open without closing the dialog.
Affected component/components
Sonner
How to reproduce
- Create a dialog
- Use a button in the dialog to create a sonner toast. Alternatively, enable the close button for the toast.
- Attempt to highlight text in the toast, or click the close button. The dialog will also close.
Codesandbox/StackBlitz link
Logs
No response
System Info
MacOS 14.4.1 (23E224), Arc Version 1.37.0
Before submitting
- I’ve made research efforts and searched the documentation
- I’ve searched for existing issues
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 16
when a dialog opens
pointer-events: none;is added to body andpointer-events: auto;is added to dialogAs a workaround I added
pointer-events: auto;to the toasterThis kinda worked for me. Instead of using
.toasteras the element in the querySelector, I used[data-sonner-toast]and it works well for meEDIT: Since there can be multiple toasts at once, when using
[data-sonner-toast], it will only work for the frontmost toast and clicking on the the others behind will still close the dialog. Here’s my workaround.Add
pointer-events: autosolve the problemOr even better
If you want to avoid casting you can check that
e.targetis an instance ofElementNOTE THAT THIS IS A WORKAROUND I AM CURRENTLY USING
The solution I found is this, there is already a discussion here about this
and add this to DialogContent