ui: [bug]:

Describe the bug

I originally planned to implement a dialog box that pops up after clicking the right-click menu. The components used are ContextMenu and Dialog. When the dialog box pops up after clicking the menu, do nothing and click the x button in the upper right corner of the dialog box. Then, you cannot right-click on the same element again. Finally, I found that there is a style attribute “pointer-events: none;” on the body element.

Affected component/components

ContextMenu/Dialog

How to reproduce

  1. right click on ContextMenuTrigger
  2. click ContextMenuItem and show a Dialog
  3. close the x button of the Dialog

The following is a full code example

import React, { useCallback, useState } from 'react';
import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuTrigger
} from '@/components/ui/context-menu';
import {
  Dialog,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogTitle
} from '@/components/ui/dialog';
import { Label } from '@/components/ui/label';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { enablePointerEvents } from '@/lib/pointer-events';

export function TreeView() {
  const [newDialogVisible, setNewDialogVisible] = useState(false);
  const [inputValue, setInputValue] = useState('');
  const onAdd = useCallback(() => {
    setNewDialogVisible(true);
  }, []);

  const handleNewDialogClose = () => {
    setNewDialogVisible(false);
    // shadcn bug
    enablePointerEvents();
  };

  const add = () => {
    // 
  };

  return (
    <div className="pl-2 pr-2 flex flex-1">
      <ContextMenu>
        <ContextMenuTrigger className="flex flex-1 text-sm">
          lorem
        </ContextMenuTrigger>
        <ContextMenuContent className="w-64">
          <ContextMenuItem inset onClick={onAdd}>
            New
          </ContextMenuItem>
        </ContextMenuContent>
      </ContextMenu>

      <Dialog open={newDialogVisible}>
        <DialogContent className="sm:max-w-[425px]" onClose={handleNewDialogClose}>
          <DialogHeader>
            <DialogTitle>New</DialogTitle>
          </DialogHeader>
          <Label className="flex items-center space-x-2 w-full">
            <span>Name</span>
            <Input
              value={inputValue}
              onChange={(event) => setInputValue(event.target.value.trim())}
              className="flex-1"
            />
          </Label>
          <DialogFooter>
            <Button type="submit" onClick={add}>Add</Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </div>
  );
}

To solve this problem, I wrote a method called enablePointerEvents.

// lib/pointer-events.ts
export function enablePointerEvents() {
  document.body.style.pointerEvents = '';
}

Codesandbox/StackBlitz link

No response

Logs


System Info

- system: mac os
- browser: chrome

Before submitting

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

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 19

Most upvoted comments

body{
  pointer-events: all !important;
}

works for me

i have same bug, i have Dialog inside Drodown, when i open dialog it add pointer-events: none; to body then dialog open store that styles and when that dialog closes it restores pointer-events: none; back to body so whole page is not clickable anymore 😕

using

"@radix-ui/react-dialog": "^1.1.11",
"@radix-ui/react-dropdown-menu": "^2.1.6",

Easyiest FIX for me is setting modal={false} to DropdownMenu component until they fix it enjoy

I also faced this issue in DropdownMenu. I just added modal={false}, and it fixed the issue

<DropdownMenu modal={false}>

This issue can also occur when using uncontrolled Dialog in DropdownMenuItem