Skip to content

Commit 3ea3b2a

Browse files
committed
Wrap menu providers children in a container
1 parent c55143d commit 3ea3b2a

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useRef } from 'react';
1+
import React, { useMemo, useRef, useState } from 'react';
22
import { Menu, MenuItem, MenuSeparator } from './leafygreen';
33
import { css, cx } from '@leafygreen-ui/emotion';
44
import { spacing } from '@leafygreen-ui/tokens';
@@ -17,6 +17,10 @@ export type {
1717
ContextMenuItemGroup,
1818
} from '@mongodb-js/compass-context-menu';
1919

20+
const containerStyles = css({
21+
height: '100%',
22+
});
23+
2024
// TODO: Remove these once https://jira.mongodb.org/browse/LG-5013 is resolved
2125

2226
const menuStyles = css({
@@ -44,8 +48,10 @@ export function ContextMenuProvider({
4448
item: ContextMenuItem
4549
) => void;
4650
}) {
51+
const [container, setContainer] = useState<HTMLElement | null>(null);
4752
return (
4853
<ContextMenuProviderBase
54+
container={container}
4955
disabled={disabled}
5056
menuWrapper={(props) => (
5157
<ContextMenu
@@ -55,7 +61,9 @@ export function ContextMenuProvider({
5561
)}
5662
onContextMenuOpen={onContextMenuOpen}
5763
>
58-
{children}
64+
<div ref={setContainer} className={containerStyles}>
65+
{children}
66+
</div>
5967
</ContextMenuProviderBase>
6068
);
6169
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ export const ContextMenuContext = createContext<ContextMenuContextType | null>(
2525

2626
export function ContextMenuProvider({
2727
disabled = false,
28+
container,
2829
children,
2930
menuWrapper: Wrapper,
3031
onContextMenuOpen,
3132
}: {
3233
disabled?: boolean;
34+
container: HTMLElement | null;
3335
children: React.ReactNode;
3436
menuWrapper: React.ComponentType<{
3537
menu: ContextMenuState & { close: () => void };
@@ -62,8 +64,8 @@ export function ContextMenuProvider({
6264
);
6365

6466
useEffect(() => {
65-
// Don't set up event listeners if we have a parent context
66-
if (parentContext || disabled) return;
67+
// We skip registering listeners when parentContext is known to avoid registering multiple (nested) listeners
68+
if (parentContext || disabled || !container) return;
6769

6870
function handleContextMenu(event: MouseEvent) {
6971
event.preventDefault();
@@ -86,7 +88,7 @@ export function ContextMenuProvider({
8688
});
8789
}
8890

89-
document.addEventListener('contextmenu', handleContextMenu);
91+
container.addEventListener('contextmenu', handleContextMenu);
9092
window.addEventListener('resize', handleClosingEvent);
9193
window.addEventListener(
9294
'scroll',
@@ -100,13 +102,19 @@ export function ContextMenuProvider({
100102
);
101103

102104
return () => {
103-
document.removeEventListener('contextmenu', handleContextMenu);
105+
container.removeEventListener('contextmenu', handleContextMenu);
104106
window.removeEventListener('resize', handleClosingEvent);
105107
window.removeEventListener('scroll', handleClosingEvent, {
106108
capture: true,
107109
});
108110
};
109-
}, [disabled, handleClosingEvent, onContextMenuOpenRef, parentContext]);
111+
}, [
112+
disabled,
113+
container,
114+
handleClosingEvent,
115+
onContextMenuOpenRef,
116+
parentContext,
117+
]);
110118

111119
const value = useMemo(
112120
() => ({

0 commit comments

Comments
 (0)