Skip to content

Commit 6b6e398

Browse files
committed
fix: throw early on
1 parent c6d7b03 commit 6b6e398

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

packages/compass-context-menu/src/context-menu-provider.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export function ContextMenuProvider({
2424
// Check if there's already a parent context menu provider
2525
const parentContext = useContext(Context);
2626

27+
// Prevent accidental nested providers
28+
if (parentContext) {
29+
throw new Error(
30+
'Duplicated ContextMenuProvider found. Please remove the nested provider.'
31+
);
32+
}
33+
2734
const [menu, setMenu] = useState<ContextMenuState>({
2835
isOpen: false,
2936
itemGroups: [],
@@ -41,11 +48,6 @@ export function ContextMenuProvider({
4148
);
4249

4350
useEffect(() => {
44-
// If there's a parent provider, don't add event listeners
45-
if (parentContext) {
46-
return;
47-
}
48-
4951
function handleContextMenu(event: MouseEvent) {
5052
event.preventDefault();
5153

@@ -73,7 +75,7 @@ export function ContextMenuProvider({
7375
document.removeEventListener('contextmenu', handleContextMenu);
7476
window.removeEventListener('resize', handleClosingEvent);
7577
};
76-
}, [handleClosingEvent, parentContext]);
78+
}, [handleClosingEvent]);
7779

7880
const value = useMemo(
7981
() => ({
@@ -82,13 +84,6 @@ export function ContextMenuProvider({
8284
[close]
8385
);
8486

85-
// Prevent accidental nested providers
86-
if (parentContext) {
87-
throw new Error(
88-
'Duplicated ContextMenuProvider found. Please remove the nested provider.'
89-
);
90-
}
91-
9287
const Wrapper = wrapper ?? React.Fragment;
9388

9489
return (

0 commit comments

Comments
 (0)