-
Notifications
You must be signed in to change notification settings - Fork 963
Dialog.Trigger does not work if trigger is Dropdown.Item #1836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Wrapping <Dialog.Root>
<DropdownMenu.Root>
<DropdownMenu.Trigger>Dropdown Menu</DropdownMenu.Trigger>
<DropdownMenu.Portal>
<DropdownMenu.Content>
<Dialog.Trigger>
<DropdownMenu.Item>"Test"</DropdownMenu.Item>
</Dialog.Trigger>
</DropdownMenu.Content>
</DropdownMenu.Portal>
</DropdownMenu.Root>
<Dialog.Portal>
<Dialog.Overlay className="DialogOverlay" />
<Dialog.Content className="DialogContent">
This is a modal.
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root> |
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 |
But if you mean open multiple dialogs at the same time, then I don't know. |
Hi @monokizsolt, here some examples that can help |
Perfect, thank you! |
Thank you ! A clean and nice solution ! |
Thank you! |
For anyone who are still having issue with this, I fixed it by using 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! |
@Kynno Could you bring us more details? maybe an example |
@AlamMena, here is the correct way to handle composition of Dialog and DropdownMenu: https://codesandbox.io/s/dropdownmenu-dialog-items-r9sq1q |
@benoitgrelard Yes, I saw that, but I noticed that the dropmenu doesn't close after the dialog opens. |
It depends on which approach, the sandbox shows 2 different approaches. The first one keeps it open (easier to implement), the second closes it (a bit more work to manage focus correctly). |
I had an issue where the dialog would open, but then I could not click anything after it was closed. And it would trigger a repetitive This simple solution fixed it. Thanks! |
@Kynno i tried the solution in the sandbox and it didnt work either , i also tried modal={false} and it did not help, super lost. Are you able to share what you did that got it working ? |
I prevented the default event in the onSelect event of DropdownMenuItem, so the Dialog can be opened now. The code is as follows: <DropdownMenu>
<DropdownMenuTrigger> Dropdown Menu </DropdownMenuTrigger>
<DropdownMenuContent>
<Dialog>
<DialogTrigger asChild>
<DropdownMenuItem onSelect={(e) => e.preventDefault()}>
Test
</DropdownMenuItem>
</DialogTrigger>
<DialogContent>
This is a modal.
</DialogContent>
</Dialog>
</DropdownMenuContent>
</DropdownMenu> |
If anyone has come here from shadcn/ui dialog notes and wondering how to show different dialogs for different menu items
|
But the expanded menu after opening Dialog does not hide, is there any solution for this? |
Did you find the answer to this? same thing happening here |
Brilliant! thank you |
I am using a form, and inside it, the inputs are behaving unexpectedly. When I select one input and change its value, the input content gets automatically selected when I move the cursor |
same issue but mine is with a popover, i have a form (in a modal dialog) and when user opens the date-picker which uses the popover, the popover closes when i hover on it even before selecting a date |
copy.mp4this above behavior |
solved this by creating a replica popover component without the portal as both the dialog and popover from shadcn were using the body element |
This solution is great, but it does not close the dropdown element, as clicking on the Dropdown Item would normally do. |
@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: function ItemTableRow({ item, onItemUpdated }: { item: Item; onItemUpdated: () => void }) {
const [isMenuOpen, setIsMenuOpen] = useState(false);
return (
<DropdownMenu open={isMenuOpen} onOpenChange={(isOpen) => setIsMenuOpen(isOpen)}>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 w-8 p-0">
<span className="sr-only">Open menu</span>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{/* EditItemDialog is just an abstraction around Dialog - onClose is a custom thing */}
<EditItemDialog
item={item}
onItemSaved={onItemUpdated}
onClose={() => setIsMenuOpen(false)}
>
<DropdownMenuItem
onSelect={(e) => {
e.preventDefault();
}}
>
<Edit className="h-4 w-4 mr-2" />
Edit
</DropdownMenuItem>
</EditItemDialog>
</DropdownMenuContent>
</DropdownMenu>
);
} it's a bit wonky, but it works |
Has anyone tried to combine the Dialog and Drawer components to create a responsive dialog, while still nesting them in a dropdown item? I can't use state because I'm using the dropdown item in the table. What I do is to create three abstractions for the wrapper, the trigger and the content, that just return either
|
YOU DID IT!!!! |
Thanks, but I have to say this is not really ideal. What I've done is imperative and not congruent with React's declarative paradigm. It would be more ideal to instead wrap the dropdown menu inside of one big dialog component with multiple triggers, and change the content of the dialog depending on which item was selected (which you can track with a state). But since I already had the dialogs built out, this was a quick workaround. |
This solution won't apply the correct aria-labels either to the dropdown menu item. |
Hey! Good day, what if the dialog is inside a looping function. I am trying to display a table, and each corresponding row have a edit button. What arises with using this hook is that multiple dialogs are being rendered, suppose I have 3 rows of data, and I click edit on one of them, 3 dialogs will also be rendered. Sorry for disturbing, I am just trying to learn. |
@Kendaichi My guess would be that you're reusing the same function Table() {
return {rows.map(r => <tr><Edit/></tr>)}
}
function Edit() {
const dialog = useDialog();
return <>
<button {...dialog.triggerProps}>Edit</button>
<Dialog {...dialog.dialogProps}>
<DialogContent> Edit </DialogContent>
</Dialog>
</>
} |
Similar to some previous answers, but using a more generic hook. // @/hooks/use-show-hide.js
import { useState } from 'react';
export const useShowHide = (defaultVisible = false) => {
const [visible, setVisible] = useState(defaultVisible);
const show = () => setVisible(true);
const hide = () => setVisible(false);
const toggle = (nextVisible) => {
if (typeof nextVisible !== 'undefined') {
setVisible(nextVisible);
} else {
setVisible((previousVisible) => !previousVisible);
}
};
return { hide, show, toggle, visible };
};
// dropdown-menu.jsx
import { useParams } from 'react-router-dom';
import { deletePost } from '@/api/post';
import { ConfirmDialog } from '@/components/confirm-dialog';
import { useShowHide } from '@/hooks/use-show-hide';
export function DropdownMenu() {
const { id } = useParams();
const deleteDialog = useShowHide();
const handleDelete = () =>
deletePost(id)
.catch(() => {
alert('Post deletion has failed.');
})
.then(() => {
deleteDialog.hide();
});
return (
<>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<MenuButton />
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>View details</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
className="!text-destructive"
onSelect={deleteDialog.show}
>
Delete
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<Dialog onOpenChange={deleteDialog.toggle} open={deleteDialog.visible}>
<DialogContent>
<ConfirmDialog
onSubmit={handleDelete}
title="Are you sure to delete this post?"
/>
</DialogContent>
</Dialog>
</>
);
} |
- also tried using the shadcnvue dropdown menu but decided against it due to an existing bug when nesting drawers/dialogs, radix-ui/primitives#1836
Thanks a a lot man it solved my issue |
I'm not a huge fan of highly nested templates, and wanted a way to remotely trigger the modal., so I created a component to remotely trigger the dialog to open. This method allows me to host ConfirmDialog outside of DropdownMenu.
|
Thank you so much. This is right solution. <DropdownMenu modal={false}><DialogComponentWithDropDownItemAsTrigger /></DropdownMenu> |
For those who use Vue here is a workaround with "as-child" and "@click.stop"
` |
Bro! You're a lifesaver. I spent 2-3 hours trying to figure this out. |
I resolved by wrapping the Dropdown with AlertDialog and putting the dialog trigger inside of DropDownMenuItem
|
This worked for me , if you want to open multiple dialogs within a dropdown menu while closing the menu after selecting a dialog
|
… causing improper opening/closing behaviour & erratic highlighting, import use-dialog from radix-ui/primitives#1836 (comment) for fix
Hello, I found a solution in the ShadCN example from the playground. https://ui.shadcn.com/examples/playground Screen.Recording.2025-01-08.184030.mp4 |
This example also shows the issue / bug:
So still the most simple solution for me is to prevent the "scroll locking" via As you can also see in the playground: when you open the dropdown - you can not scroll the page anymore - in my use case this feature was not needed - as the dropdown can stay open when I scroll (it will be repositioned correctly when you scroll) |
Perhaps this issue should be locked, as the comments have largely devolved into reposts of previously suggested solutions |
doesn't work for me |
- click Plus can open AddGroup Dialog - right click on each group button can open context menu - context menu can set default chosen, edit/delete and manage all the groups, so the context menu can open two different dialogs, one for EditGroup, one for ManageGroup - ManageGroup can open AddGroup and EditGroup as well - based on issue radix-ui/primitives#1836 to call multi dialog on shadcn-ui
approached this task as an opportunity to learn something new, so I decided to give ShadCN UI a try. Unfortunately, I ran into a few tricky bugs while implementing the features. I also considered using Tanstack Table and Tanstack Query, but the scale of the project was too small for that to be necessary. During the task, I encountered several bugs in ShadCN UI, such as: shadcn-ui/ui#468 radix-ui/primitives#1836 (comment) I was able to fix some of these issues, but there's still one remaining problem: after submitting the form, clicking anything becomes impossible. This seems to be related to focus management when dealing with multiple popovers. Since resolving third-party issues is outside the scope of this task, I’m leaving it as is for now. Apologies for the inconvenience! 😅
You can resolve this issue by changing the uncontrolled dropdown menu to a controlled dropdown menu and closing the menu item when it is clicked. |
thank you so much. you save my day |
this works for me. thanks |
Woks for me |
Adding modal={false} worked for me I want to open dialog from each menu item. It solves the issue blocked aria as well.
|
Bug report
Current Behavior
A dropdown menu consists of several
Dropdown.Item
components. If I pass one of these Item components to aDialog.Trigger
, it does not open the dialog on click. If I pass plain text to theDialog.Trigger
, then it works.Expected behavior
I expect
Dropdown.Item
to be a validDialog.Trigger
.Reproducible example
https://codesandbox.io/s/shy-hill-5ymyp0?file=/src/App.js
Your environment
See sandbox for package versions.
The text was updated successfully, but these errors were encountered: