primitives: Dialog.Trigger does not work if trigger is Dropdown.Item
Bug report
Current Behavior
A dropdown menu consists of several Dropdown.Item components. If I pass one of these Item components to a Dialog.Trigger, it does not open the dialog on click. If I pass plain text to the Dialog.Trigger, then it works.
Expected behavior
I expect Dropdown.Item to be a valid Dialog.Trigger.
Reproducible example
https://codesandbox.io/s/shy-hill-5ymyp0?file=/src/App.js
Your environment
See sandbox for package versions.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 10
- Comments: 52 (4 by maintainers)
I prevented the default event in the onSelect event of DropdownMenuItem, so the Dialog can be opened now. The code is as follows:
If anyone has come here from shadcn/ui dialog notes and wondering how to show different dialogs for different menu items
Hi @monokizsolt, here some examples that can help
https://codesandbox.io/embed/r9sq1q
For anyone who are still having issue with this, I fixed it by using
modal={false}on myDropdownMenu.Rootcomponent.I tried the solution in the sandbox link but I was unable to use it in my code, not sure why. However this modal setting did the trick for me.
Hope it helps!
Hello,
Is there a way to have multiple Dropdown.Item components each with their own Dialog? With your solution only 1 dialog can be opened.
Thanks
Wrapping
DropdownwithDialogfixes this:Not working for me. Dialog not opening. Using preventDefault works but doesn’t close the dropdown.
If you put the “modal” property to false, your DropdownMenuItem with the modal/dialog children will work normally
Fixing the problem with the DropdownMenu overlay not closing after click
But if you mean open multiple dialogs at the same time, then I don’t know.
This is working for me.
Using https://codesandbox.io/embed/r9sq1q as a starting point, I adapted it for typescript and shadcn/ui.
Same here.
This solution is great, but it does not close the dropdown element, as clicking on the Dropdown Item would normally do. Is there any way to keep that functionality while also allowing to open the Dialog?
@juanrgon that’s clean ☺️ but just a heads up that you’re breaking a11y there so if you’re worried about that, i think the most relevant part is to make sure you return focus to the trigger when the dialog closes.
this is why radix usually recommends using the parts they provide instead of doing your own thing, because we can’t know for sure what a11y support we’re losing by breaking away from them.
Sorry for intruding in a closed issue, but I don’t really get WHY I should do this (nesting dialogs inside a dropdown)… why not doing what we normally do in react, that is defining whatever number of dialogs we want in another place and then controlling them using state vars? Maybe I’m missing something, but if it’s only to spare some state I think it isn’ worth it. Just my 2c 😉
Yep this fixed it for me, this should get more attention!
Brilliant! thank you
@Malin88 I have been using this so far, works for me. Found this here from shadcn/ui issues Example:
But the expanded menu after opening Dialog does not hide, is there any solution for this?
@benoitgrelard Yes, I saw that, but I noticed that the dropmenu doesn’t close after the dialog opens.
Thank you ! A clean and nice solution !
In case this helps anyone, I’ve made my own hook for Dialogs in my app:
I now use this hook to launch multiple dialogs from anywhere:
Since switching to this pattern, this issue is not really relevant to me anymore
Você é muito fera, muito obrigado amigo!!
This probably needs to be truly an actually fixed, since this thread has been proposing half-working workarounds for a year now.
@piotrkulpinski i solved this by making both the Dialog and DropdownMenu controlled. then when the dialog closes, i manually close the dropdown menu
here’s an example:
it’s a bit wonky, but it works
solved this by creating a replica popover component without the portal as both the dialog and popover from shadcn were using the body element
@AlamMena, here is the correct way to handle composition of Dialog and DropdownMenu: https://codesandbox.io/s/dropdownmenu-dialog-items-r9sq1q
If anyone has come here from shadcn/ui dialog notes and wondering how to show different dialogs for different menu items, and close the menu after closing the dialog you can use this simple example demo:
( <DropdownMenu modal={false} open={opendropdown1} onOpenChange={(v) => { closeDropdown1(v); }} > <DropdownMenuTrigger> <Button>Dropdown Menu</Button> </DropdownMenuTrigger> <DropdownMenuContent className={shadow-light100_dark100 bg-tertiary-light-dark border-none p-2 `} > <Dialog onOpenChange={closeDropdown1}> <DialogTrigger asChild> <DropdownMenuItem onSelect={(e) => e.preventDefault()}> Test option one </DropdownMenuItem> </DialogTrigger>) `
If the whole Dialogs are placed in the DropdownMenu, the Dialog will disappear after the Dropdown is closed, so a better way is to put the dialog at a higher level instead of using
preventDefaultand other workarounds.@pfurini for many reasons:
a blog post i wrote on this topic might shed some light but it’s an oldie https://jjenzz.com/avoid-global-state-colocate/
If you are interested, I make a small wrapper to add dialog as a dropdown menu item.
https://codesandbox.io/p/sandbox/dropdown-dialog-item-ts-wfvn7k?file=%2Fsrc%2FDropdownDialog.tsx%3A28%2C46
By default, the dropdown menu is closed when Dialog or AlertDialog is closed. If you want the focus to return to the Dialog/AlertDialog dropdown item you can use the
returnFocusprop in theDialogTriggerItem/AlertDialogTriggerItemPS: I used Radix UI utility packages that are not exposed for public use, so it is at your own risk to install these libraries.
Packages
Anatomy
Still same issue
That’s smart ! it fixed my problem
The escape key still causes buggy behavior here even when I use the above solutions. Has anyone found a fix for that?
Thank you!