ui: [bug]: Can't open DropdownMenu when using with AlertDialog together

Describe the bug

export default function Test() {
  const [open, setOpen] = useState(false);

  return (
    <div className="p-20">
      <DropdownMenu>
        <DropdownMenuTrigger>Open</DropdownMenuTrigger>
        <DropdownMenuContent>
          <DropdownMenuLabel>My Account</DropdownMenuLabel>
          <DropdownMenuSeparator />
          <DropdownMenuItem>Profile</DropdownMenuItem>
          <DropdownMenuItem>Billing</DropdownMenuItem>
          <DropdownMenuItem>Team</DropdownMenuItem>
          <DropdownMenuItem>Subscription</DropdownMenuItem>
          <Button onClick={() => setOpen(true)}>open dialog</Button> // open dialog 
        </DropdownMenuContent>
      </DropdownMenu>

      <AlertDialog open={open}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
            <AlertDialogDescription>
              This action cannot be undone. This will permanently delete your
              account and remove your data from our servers.
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel onClick={() => setOpen(false)}> // close dialog
              Cancel
            </AlertDialogCancel>
            <AlertDialogAction>Continue</AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </div>
  );
}

Can’t open DropdownMenu again after closing AlertDialog

Affected component/components

DropdownMenu, AlertDialog

How to reproduce


Codesandbox/StackBlitz link

https://codesandbox.io/p/sandbox/shadcnui-issue-44hxn3

Logs

No response

System Info

---

Before submitting

  • I’ve made research efforts and searched the documentation
  • I’ve searched for existing issues

About this issue

Most upvoted comments

I’ve tried all the possible solutions listed (and even noted listed) here, be it with

  • controlled state
  • with e.preventDefault() and e.stopPropagation()
  • overriding document.body.style.pointerEvents
  • and even shuffling around the dialog / dropdownmenu structure
<Dialog>
   <DropdownMenu>
     {/* ... */}
       <DropdownMenuItem>
             <DialogTrigger>open</DialogTrigger>
       </DropdownMenuItem>
      {/* .... */}
    </DropdownMenu>
    <DialogContent>
       ...
    </DialogContent>
</Dialog>

and

<DropdownMenu>
 {/* ... */}
   <DropdownMenuItem>
         <Dialog>
             <DialogTrigger>open</DialogTrigger>
             <DialogContent>...</DialogContent>
          </Dialog>
     </DropdownMenuItem>
 {/* ... */}
</DropdownMenu>

But none of the solutions work.

The only way I truly got it to work was by programatically reloading the page, i.e.

const onSubmit: MouseEventHandler<HTMLButtonElement> = async (e) => {
   try {
    // ...
   } catch (err) {
   // ...
   } finally {
    document.location.reload()
   }
}

This overrides the stuck pointer-events which was blocing the dialogs. Silly, but as far as I know this is the only solution that truly works everywhere.

This should work fine for you, at least it works for me. I have the same situation as you.

useEffect(() => {
    // Callback to handle DOM changes
    const handleDomChanges = (mutationsList: MutationRecord[]) => {
      for (const mutation of mutationsList) {
        if (
          mutation.type === "attributes" &&
          mutation.attributeName === "style"
        ) {
          const pointerEvents = document.body.style.pointerEvents;
          console.log("Pointer Events changed:", pointerEvents);

          // Reset pointer-events if set to 'none'
          if (pointerEvents === "none") {
            document.body.style.pointerEvents = "";
            console.log("Pointer Events reset to default");
          }
        }
      }
    };

    // Create a MutationObserver instance
    const observer = new MutationObserver(handleDomChanges);

    // Start observing the body element for style attribute changes
    observer.observe(document.body, {
      attributes: true, // Watch for attribute changes
      attributeFilter: ["style"], // Only observe changes to 'style'
    });

    // Cleanup the observer when the component unmounts
    return () => observer.disconnect();
  }, []); // Empty dependency array ensures it runs once

I’ve faced the same issue in a recent app, I recently added the alert-dialog component, it’s already using the v1.1.2 of @radix-ui/react-alert-dialog and the problem still occurs. I’ve noticed that when the dialog is closed it is adding a style="pointer-events: none;" property to thml body tag and it’s blocking the pointer event on the entire page.

image

I’m pretty sure that it’s being added after close the dialog because I’ve monitored on devtools elements tab, and if you remove that style prop manually (from devtools elements tab) the page backs to normal.

same scenario, solved with <AlertDialog open={open} onOpenChange={setOpen}>