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
- right click on
ContextMenuTrigger
- click
ContextMenuItem and show a Dialog
- 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
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
- right click on
ContextMenuTrigger - click
ContextMenuItemand show aDialog - close the
xbutton of theDialog
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
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 restorespointer-events: none;back to body so whole page is not clickable anymore 😕using
Easyiest FIX for me is setting
modal={false}toDropdownMenucomponent until they fix it enjoyI also faced this issue in
DropdownMenu. I just addedmodal={false}, and it fixed the issueThis issue can also occur when using uncontrolled Dialog in DropdownMenuItem