Repository navigation
Dialoge is automatically getting hidden while using inside use of Dropdown Item
#3361
Replies: 2 comments
|
The Keep the dialog as a controlled sibling of the menu instead: const [passwordOpen, setPasswordOpen] = React.useState(false)
return (
<>
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon">
<DotsHorizontalIcon />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onSelect={() => setPasswordOpen(true)}>
Change password
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<Dialog open={passwordOpen} onOpenChange={setPasswordOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Change password</DialogTitle>
</DialogHeader>
{/* form */}
</DialogContent>
</Dialog>
</>
)This is also the pattern used by shadcn's official Dropdown Menu with Dialog example. |
|
The issue is caused by the Dialog being mounted inside The cleanest solution is to move the Dialog outside the DropdownMenu and control its open state from the menu item. For example: const [dialogOpen, setDialogOpen] = React.useState(false)
return (
<>
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild>
<Button variant="ghost">Open menu</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem
onSelect={() => setDialogOpen(true)}
>
Change password
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Change password</DialogTitle>
<DialogDescription>
Update the user's password here.
</DialogDescription>
</DialogHeader>
</DialogContent>
</Dialog>
</>
)This way, selecting the menu item closes the dropdown normally, but the Dialog remains mounted independently and opens through its controlled state. You can use the same pattern for Delete, Edit, Confirmation, and other dialogs triggered from row actions. It also avoids relying on nested overlay components sharing the same lifecycle. |
Uh oh!
There was an error while loading. Please reload this page.
Inside of a shadcn table I'm using Row Actions for do some action on row level. I have the component as below. I'm trying to use a
Dialogemodel component for action button to change password. But the Modal is visible for split seconds it's getting hide. Please help !All reactions