-
|
I'm trying to make a very simple DropdownMenu that invokes a Dialog if a certain option is used (Delete plan in my case) <div className="flex gap-1 w-4">
<AlertDialog>
<DropdownMenu>
<DropdownMenuTrigger
render={
<Button variant="default" className="h-8 w-8 p-0">
<FaWrench className="h-4 w-4" />
</Button>
}
/>
<DropdownMenuContent align="end">
<DropdownMenuGroup>
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem>View details</DropdownMenuItem>
<DropdownMenuItem>Edit plan</DropdownMenuItem>
<DropdownMenuItem>Duplicate plan</DropdownMenuItem>
<DropdownMenuSeparator />
<AlertDialogTrigger
render={<DropdownMenuItem variant="destructive">Delete plan</DropdownMenuItem>}
nativeButton={false}
/>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone. This plan will be permanently deleted.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction>Continue</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>It works fine for the most part, but the issue is that the I have tried to apply solutions from discussion #2307; however, none of them worked. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
This is a common focus-management conflict when nesting menu -> dialog triggers.
Typical fix pattern:
So instead of trigger-inside-item composition, wire it as:
That usually resolves the autofocus race. If this solves it for your setup, feel free to mark accepted. |
Beta Was this translation helpful? Give feedback.
-
|
The existing explanation nails the root cause. Here's what's specifically happening with your setup and the working code. Why the autofocus happens When The fix: remove the trigger from the menu entirely Drive the dialog with state instead of trigger composition: import { useState } from "react";
export function PlanActions() {
const [deleteOpen, setDeleteOpen] = useState(false);
return (
<div className="flex gap-1 w-4">
<AlertDialog open={deleteOpen} onOpenChange={setDeleteOpen}>
<DropdownMenu>
<DropdownMenuTrigger
render={
<Button variant="default" className="h-8 w-8 p-0">
<FaWrench className="h-4 w-4" />
</Button>
}
/>
<DropdownMenuContent align="end">
<DropdownMenuGroup>
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem>View details</DropdownMenuItem>
<DropdownMenuItem>Edit plan</DropdownMenuItem>
<DropdownMenuItem>Duplicate plan</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
variant="destructive"
onClick={() => setDeleteOpen(true)}
>
Delete plan
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone. This plan will be permanently deleted.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction>Continue</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
);
}The two key changes from your original:
With no |
Beta Was this translation helpful? Give feedback.

This is a common focus-management conflict when nesting menu -> dialog triggers.
DropdownMenutries to restore focus to the trigger when it closes, whileAlertDialogtries to move focus into the dialog at open. If both happen in the same tick, autofocus can look broken.Typical fix pattern:
preventDefault()and control dialog open state manually.requestAnimationFrame/setTimeout(0)).So instead of trigger-inside-item composition, wire it as:
openDialog(true)Al…