ui: Unable to use Dialog inside a DropdownMenu ( with DialogTrigger or without it )
I’m having an issue with triggering a dropdown menu correctly. The dropdown menu contains a dialog, and I’m trying to trigger the dropdown using a DropdownMenuTrigger or a DialogTrigger, but neither is working as expected.
Minimal Reproducible Example:
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button className="bg-gray-800 text-white mb-4 h-12 w-full text-md pr-4 text-left">
Create Campaign
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="center" className="w-[260px] bg-white p-4">
<Dialog>
<DialogTrigger asChild>
<DropdownMenuItem className="text-md pt-2">
trigger dialog from DropdownMenuItem
</DropdownMenuItem>
</DialogTrigger>
<DialogContent className="text-black max-w-[60%] bg-white p-6 h-[600px]">
<div className="flex gap-10 items-start">
x
</div>
</DialogContent>
</Dialog>
<DropdownMenuSeparator className={"bg-gray-300"}/>
<DropdownMenuItem className="text-md pt-3">
option 2
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 29
If you want to have a DialogBox pop up from a dropdown menu (for example, a dropdown menu to edit a user, which would pop up a DialogBox modal to edit it), you’ll have to do it like this:
the key is the preventDefault() call.
@Char99s, @shiftcontrol-dan, Using “preventDefault” works by keeping the dialog open. But it remains open even when the dialog is closed. Is there way to automatically close the dropdown menu when the dialog is closed?
`<DropdownMenu modal={false}> <DropdownMenuTrigger className="hover:bg-violet-200 h-8 w-8 flex justify-center items-center rounded-full"> <IoEllipsisVerticalOutline /> </DropdownMenuTrigger> <DropdownMenuContent className="shadow-md bg-white rounded-sm border p-2"> <AlertDialog> <AlertDialogTrigger asChild> <DropdownMenuItem onSelect={(e) => e.preventDefault()} className=“flex items-center py-1 px-2 rounded-sm hover:!bg-violet-200 cursor-pointer”> <MdDriveFileRenameOutline className="mr-3" /> Rename </DropdownMenuItem> </AlertDialogTrigger> <AlertDialogContent> <AlertDialogHeader> <AlertDialogTitle>Rename</AlertDialogTitle> <AlertDialogDescription> Please enter a new name for the item: <input type=“text” value={docName} onChange={(e) => { setDocName(e.target.value); }} className=“py-1 px-2 w-full border-2 outline-violet-800 my-2” /> </AlertDialogDescription> </AlertDialogHeader> <AlertDialogFooter> <AlertDialogCancel onClick={() => { setDocName(Name); }} className=“hover:text-gray-800 hover:border-violet-500 text-violet-800”> Cancel </AlertDialogCancel> <AlertDialogAction className={ docName == “” ? "bg-gray-200 text-gray-700 hover:bg-gray-200 hover:text-gray-700 cursor-not-allowed " : “” }> Save </AlertDialogAction> </AlertDialogFooter> </AlertDialogContent> </AlertDialog>
Suggestion: Use modal={false} for DropdownMenu to prevent portal rendering issues
The recommended and straightforward solution is to disable modal rendering by setting modal={false} on the DropdownMenu component.
Example Implementation:
Benefits:
Avoids potential z-index/portal-related rendering issues
Maintains all functionality while simplifying DOM structure
Particularly useful in complex layout scenario
ShadCn has provided also some guidance in the Dialog Page - here.
In cases of multiple different dialogs, you would need to tell react which dialog to render
And your
DeleteDialogshould only contain the<DialogContent/>as you’ve already the entire component wrapped in a dialog.I tried a dozen different things, the only way to make it work was to render the dialog outside of the dropdown and handle control manually.
I correctly set up the dialog rendering based on the state and a switch statement. However, I kept running into an issue where, after closing one dialog, I couldn’t click anywhere else on the page.
It turns out this behavior is related to the following issue: https://github.com/shadcn-ui/ui/issues/1859.
The solution is to update
DialogContentlike this:Hope this helps anyone else running into the same issue!
The above worked for me, but only after adding
DialogTriggers.I like to extract the open-state handling into a useDialog hook and pass the props returned from that hook to the dialog.
useDialog hook
Example Dialog Component
Usage of Dialog and Hook
Well I tried an easier approach, which can make the button looks like dropdown menu item, it will be easy if you want to build up some indepedent components without changing the dropdown menu’s structure.
thank you so much, that actually worked, I had to assign a separate state to the dropdown menu and add set false on both states when closing the modal.
So I have tinkered with the code. trying every possible solutions posted here, but I still can’t get the modal nor dropdown menu to work.
I am using react-router + vite framework. The issue was i was using the fetcher from useFetcher hook in the alertdialog component i made thats imported into dropdown menu.
removing all the fetchers, made it work.
Wrap your
DropdownMenuin aDialogcomponent, and theDialogTriggershould be a child of theDropdownMenuItemwith theasChildprop.The above solution leaves the Dropdown menu open, even after the dialog has been closed. Placing the Dropdown within the Dialog is a better solution. This works well for me: