Skip to content

Commit fcf802c

Browse files
fix(context-menu): nested menus
Add props to stop propagation of mouse events so nested menus don't all open at once.
1 parent 741e2ed commit fcf802c

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/components/context-menu/context-menu.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export interface ContextMenuProps<T extends HTMLElement = HTMLDivElement> {
1616
menuProps?: Omit<MenuProps, 'children'> & { children?: React.ReactNode };
1717
portalProps?: Omit<PortalProps, 'children'> & { children?: React.ReactNode };
1818
menuButtonProps?: MenuButtonProps;
19+
stopPropagation?: boolean;
20+
stopImmediatePropagation?: boolean;
1921
}
2022

2123
export const ContextMenu = typedMemo(<T extends HTMLElement = HTMLElement>(props: ContextMenuProps<T>) => {
@@ -36,6 +38,12 @@ export const ContextMenu = typedMemo(<T extends HTMLElement = HTMLElement>(props
3638
// `contains()` requires the arg to be Node. Technically it can be any EventTarget, but it won't cause an error.
3739
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions */
3840
if (targetRef.current?.contains(e.target as Node) || e.target === targetRef.current) {
41+
if (props.stopImmediatePropagation) {
42+
e.stopImmediatePropagation();
43+
}
44+
if (props.stopPropagation) {
45+
e.stopPropagation();
46+
}
3947
// clear pending delayed open
4048
window.clearTimeout(timeoutRef.current);
4149
e.preventDefault();
@@ -54,7 +62,7 @@ export const ContextMenu = typedMemo(<T extends HTMLElement = HTMLElement>(props
5462
}
5563
lastPositionRef.current = [e.pageX, e.pageY];
5664
},
57-
[onClose, onOpen]
65+
[onClose, onOpen, props.stopImmediatePropagation, props.stopPropagation]
5866
);
5967

6068
useEffect(

0 commit comments

Comments
 (0)