ui: I can't click anywhere after using alert dialog.

I wanted to implement alert-dialog into my dropdown menu but after I click to buttons I can’t click anymore I had to refresh site.

code:

"use client"

import { ColumnDef } from "@tanstack/react-table"
import { ArrowUpDown, Eye, EyeOff, MoreHorizontal } from "lucide-react"

import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"

import {
    AlertDialog,
    AlertDialogAction,
    AlertDialogCancel,
    AlertDialogContent,
    AlertDialogDescription,
    AlertDialogFooter,
    AlertDialogHeader,
    AlertDialogTitle,
    AlertDialogTrigger,
  } from "@/components/ui/alert-dialog"

import { useRouter, useSearchParams } from "next/navigation"
import { useState } from "react"

export type Category = {
  id: string
  name: string
  products: number
  visibility: "public" | "private"
}



export const columns: ColumnDef<Category>[] = [
    
  {
    id: "select",
    header: ({ table }) => (
      <Checkbox
        checked={table.getIsAllPageRowsSelected()}
        onCheckedChange={(value:any) => table.toggleAllPageRowsSelected(!!value)}
        aria-label="Select all"
      />
    ),
    cell: ({ row }) => (
      <Checkbox
        checked={row.getIsSelected()}
        onCheckedChange={(value:any) => row.toggleSelected(!!value)}
        aria-label="Select row"
      />
    ),
    enableSorting: false,
    enableHiding: false,
  },
  {
    accessorKey: "name",
    header: "Kategori",
    cell: ({ row }) => <div>{row.getValue("name")}</div>,
  },
  {
    accessorKey: "products",
    header: "Ürün Sayısı",
    cell: ({ row }) => <div>{row.getValue("products")}</div>,
  },
  {
    accessorKey: "visibility",
    header: "Görünürlük",
    cell: ({ row }) => (
      <div>
        {row.getValue("visibility") === "public" ? 
          <><Eye className="mr-2 inline flex-1 text-sm text-muted-foreground" size={20}/>Herkese açık</> : 
          <><EyeOff className="mr-2 inline flex-1 text-sm text-muted-foreground" size={20}/>Gizli</>
        }
      </div>
    ),
  },
  {
    id: "actions",
    enableHiding: false,
    cell: ({ row }) => {
      const category = row.original
      // eslint-disable-next-line react-hooks/rules-of-hooks
      const router = useRouter()
      const handleDelete = () => {
        // Perform delete action here
        // For example:
        // deleteCategory(category.id)
        // .then(() => router.push('/categories'))
      }
      // eslint-disable-next-line react-hooks/rules-of-hooks
      const [isAlertDialogOpen, setAlertDialogOpen] = useState(false)
      
      return (
        <>
            <DropdownMenu>
          <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">
            <DropdownMenuLabel>Eylemler</DropdownMenuLabel>
            <DropdownMenuSeparator />
            


            <DropdownMenuItem onClick={() => setAlertDialogOpen(true)}>Kategoriyi Sil</DropdownMenuItem>

            <DropdownMenuItem onClick={() => router.push(`/categories/edit/${category.id}`)}>Katogori Görünürlük</DropdownMenuItem>
            
            <DropdownMenuSeparator />
            <DropdownMenuItem onClick={() => router.push(`/categories/edit/${category.id}`)}>Kategoriyi Düzenle</DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
        
        <AlertDialog open={isAlertDialogOpen} onOpenChange={setAlertDialogOpen}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>Emin misiniz?</AlertDialogTitle>
            <AlertDialogDescription>
              Bu eylem geri alınamaz. Bu eylem kategoriyi kalıcı olarak silecek.
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
          <AlertDialogCancel onClick={() => setAlertDialogOpen(false)}>İptal</AlertDialogCancel>
            <AlertDialogAction>Devam et</AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>

        </AlertDialog>
        </>
      )
      
    },
  },


]

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 36

Commits related to this issue

Most upvoted comments

Setting <DropdownMenu modal={false}> solved the problem bcs i had two modal in place.

If it helps anyone, ran into the same problem and inspired by the comments above I solved it using this:

<AlertDialog
    open={deleteDialogOpen}
    onOpenChange={() => { setTimeout(() => (document.body.style.pointerEvents = ""), 100) }}
>

Seems to be working well so for now.

here’s what i found to solve:

  1. update @radix-ui/react-dropdown-menu to 2.1.1
  2. set modal={false} on DropdownMenu

hope it helps.

'use client'
import { DropdownMenuTrigger } from '@radix-ui/react-dropdown-menu'
import { SlidersHorizontal } from 'lucide-react'
import { Button } from '@/components/ui/button'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuItem,
} from '@/components/ui/dropdown-menu'
import { Dialog, DialogTrigger } from '@/components/ui/dialog'
import { AddGroupDialogContent } from '../../forms/components/add-group-dialog-content'
import { Sheet, SheetTrigger } from '@/components/ui/sheet'
import { AddItemSheetContent } from '../../forms/components/add-item-sheet-content'
import { EstimateType } from '@prisma/client'
import React from 'react'

export function AddItemsDropdown() {
  const [openSheet, setOpenSheet] = React.useState<boolean>(false)
  const [itemType, setItemType] = React.useState<EstimateType>('MATERIAL')
  return (
    <div>
      <Dialog>
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button variant='outline' size='sm' className='ml-auto hidden h-8 lg:flex'>
              <SlidersHorizontal className='mr-2 h-4 w-4' />
              Add Items
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align='end' className='w-[150px]'>
            <DropdownMenuLabel>Add Items</DropdownMenuLabel>
            <DialogTrigger className='w-full'>
              <DropdownMenuItem>{'Add Group'}</DropdownMenuItem>
            </DialogTrigger>
            <DropdownMenuItem
              onSelect={() => {
                setOpenSheet(true)
                document.body.style.pointerEvents = ""
              }}
            >
              {'Add Material'}
            </DropdownMenuItem>
            <DropdownMenuSeparator />
          </DropdownMenuContent>
        </DropdownMenu>
        <AddGroupDialogContent />
      </Dialog>
      <Sheet open={openSheet} onOpenChange={setOpenSheet}>
        <SheetTrigger>
          <Button>{'Open'}</Button>
        </SheetTrigger>
        <AddItemSheetContent type={itemType} />
      </Sheet>
    </div>
  )
}

In my case it appears that when i open my sheet within a DropdownMenuItem and closing the sheet afterwards the pointer event on the body remains none my fix was using document.body.style.pointerEvents = ""

if you are using any popover/alertdialog/modal inside dropdown-menu set the modal=false on DropdownMenu fixes this issue

<DropdownMenu modal={false}>

Huge shout-out to whoever posted this query! I’ve been struggling with the exact same issue for days, and somehow I stumbled upon this thread. Finally, the solution worked perfectly for me !

This solution Worked for me … <DropdownMenu modal={false}>

Thank you ! Really…

If it helps anyone, ran into the same problem and inspired by the comments above I solved it using this:

<AlertDialog
    open={deleteDialogOpen}
    onOpenChange={() => { setTimeout(() => (document.body.style.pointerEvents = ""), 100) }}
>

Seems to be working well so for now.

To add to prev answer: the consistent way is to modify the DialogPrimitive.Close component. You have to do the setTimeout. It’s not great, happy to make a PR to issue a fix but I’m not exactly sure what the better solution is @shadcn

<DialogPrimitive.Close
        className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground"
        onClick={() => {
          // yes, you have to set a timeout
          setTimeout(() => (document.body.style.pointerEvents = ""), 500);
        }}
      >
        <X className="h-4 w-4" />
        <span className="sr-only">Close</span>
      </DialogPrimitive.Close>

Thanks @Kiranism ! I was using a Dropdown with a Button trigger. DropdownItems was opening a modal. After closing that modal anything was not clickable anymore. Setting modal={false} solved the issue.

 <DropdownMenu>
  <DropdownMenuTrigger asChild={true}>
    <Button variant="ghost" className="h-8 w-8 p-0">
      <span className="sr-only">Open Menu</span>
      <span className="h-4 w-4">
        <MoreHorizontal />
      </span> </Button>
  </DropdownMenuTrigger>
  <DropdownMenuContent align="end">
    <DropdownMenuLabel>Actions</DropdownMenuLabel>
    <AlertDialog>
      <AlertDialogTrigger asChild>
        <DropdownMenuItem
          className="bg-green-500 hover:bg-green-700 hover:text-white"
          onSelect={(e) => e.preventDefault()}
        >
          <Eye className="w-4 h-4 mr-2" /> Show Detail
        </DropdownMenuItem>
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>
            Are you sure?
          </AlertDialogTitle>
          <AlertDialogDescription>
            Hahahahaha
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction>Continue</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
    <DropdownMenuItem className="bg-yellow-500 hover:bg-yellow-700 hover:text-white">
      <Pencil className="w-4 h-4 mr-2" /> Edit
    </DropdownMenuItem>
    <DropdownMenuItem className="bg-red-500 text-white hover:bg-red-700">
      <Trash className="w-4 h-4 mr-2" /> Delete
    </DropdownMenuItem>
  </DropdownMenuContent>
</DropdownMenu >

if you still have this problem, you can try using this code, i manage to open alert dialog inside the dropdown menu

This trick worked.

https://github.com/radix-ui/primitives/issues/1836#issuecomment-1674338372

you may also want to disable the modal of DropdownMenu by setting the value to false.

here’s what i found to solve:

  1. update @radix-ui/react-dropdown-menu to 2.1.1
  2. set modal={false} on DropdownMenu

hope it helps.

only second option worked on me, thanks @dalalRohit

Thanks @dalalRohit! Solved the modal and dropdown menu problem 😃 Another problem I was having was when using a Popover inside the modal inside the dropdownmenu, which I also managed to solve by putting modal={true} in the Popover.

Hi @wCatly and @UnknownBigBrain, did you try this?

“To activate the Dialog component from within a Context Menu or Dropdown Menu, you must encase the Context Menu or Dropdown Menu component in the Dialog component. For more information, refer to the linked issue here.”

From the documentation here: https://ui.shadcn.com/docs/components/dialog

here’s what i found to solve:

  1. update @radix-ui/react-dropdown-menu to 2.1.1
  2. set modal={false} on DropdownMenu

hope it helps.

THANKS!

Set modal on the dropdown menu to false <DropdownMenu modal={false}> works for me.

When using Shadcn components like Popover, DropdownMenu, or Select inside a modal/dialog, you need to set modal={true} to prevent the portal from creating a new stacking context that can cause z-index and focus trapping issues.

Example: <Popover modal />

Hope this helps

Here’s my solution.

  1. Wrap the DropdownMenu with AlertDialog
  2. Wrap the DropdownMenuItem with AlertDialogTrigger
  3. Add the class name w-full to AlertDialogTrigger, and remove any prop asChild

The code is here:

<AlertDialog>
  <DropdownMenu>
    <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">
      <DropdownMenuLabel>Actions</DropdownMenuLabel>
      <DropdownMenuItem>Publish</DropdownMenuItem>
      <DropdownMenuItem>Unpublish</DropdownMenuItem>
      <AlertDialogTrigger className="w-full">
        <DropdownMenuItem>Delete</DropdownMenuItem>
      </AlertDialogTrigger>
    </DropdownMenuContent>
  </DropdownMenu>
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
      <AlertDialogDescription>
        This action cannot be undone. This will permanently delete the item.
      </AlertDialogDescription>
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel>Cancel</AlertDialogCancel>
      <AlertDialogAction>Continue</AlertDialogAction>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>

My case is a bit different. I use an ‘AlertDialog’ at the same level as the dialog, but it will display a warning when the user accidentally exits the form. When ‘leave’ is selected, the dialog will close, but buttons and other elements outside of it are no longer interactable. This is because the body still has style=“pointer-events: none;” instead of being removed as it normally should. image I had to use a Dialog instead of an AlertDialog to fix the current issue.

Here’s a workaround for Dialog and AlertDialog in the same level.

// Example: Creating api key in one dialog and showing a copy api key button in another dialog
const [isCreateOpen, setIsCreateOpen] = useState(false)
const [isCopyOpen, setIsCopyOpen] = useState(false)

const onSubmit = (values: ValuesType) => {
    ...
   setIsCopyOpen(true)
   setIsCreateOpen(false)
};

return (
    <>
      {!isCopyOpen && (
        <Dialog open={isCreateOpen} onOpenChange={setIsCreateOpen}>
          <DialogTrigger>
            Create API Key
          </DialogTrigger>
          <DialogContent>
           <Form {...form}>
              <form onSubmit={form.handleSubmit(onSubmit)}>
                  ...
                 <Button>Create</Button>
              </form>
           </Form>
          </DialogContent>
        </Dialog>
      )}

      <AlertDialog open={isCopyOpen} onOpenChange={setIsCopyOpen}>
        <AlertDialogContent>
            ...
        </AlertDialogContent>
      </AlertDialog>
    </>
  );

Thanks for the updates @braian-bernatto

In my case l’ve got that error because on my dialog component I’ve imported by error this line of code import { DialogDescription } from '@radix-ui/react-dialog' instead of import { DialogDescription } from './ui/dialog'. Once I’ve changed it back it works as it should.