Skip to content

Commit 5deaca4

Browse files
authored
fix: scrolling the context menu shouldn't close it COMPASS-9584 (#7172)
1 parent e7e965d commit 5deaca4

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useMemo, useRef } from 'react';
22
import { Menu, MenuItem, MenuSeparator } from './leafygreen';
3-
import { css } from '@leafygreen-ui/emotion';
3+
import { css, cx } from '@leafygreen-ui/emotion';
44
import { spacing } from '@leafygreen-ui/tokens';
55

66
import {
@@ -9,6 +9,7 @@ import {
99
type ContextMenuItem,
1010
type ContextMenuItemGroup,
1111
type ContextMenuWrapperProps,
12+
contextMenuClassName,
1213
} from '@mongodb-js/compass-context-menu';
1314

1415
export type {
@@ -76,7 +77,7 @@ export function ContextMenu({ menu }: ContextMenuWrapperProps) {
7677
open={menu.isOpen}
7778
setOpen={menu.close}
7879
justify="start"
79-
className={menuStyles}
80+
className={cx(menuStyles, contextMenuClassName)}
8081
maxHeight={Number.MAX_SAFE_INTEGER}
8182
>
8283
{itemGroups.map((items: ContextMenuItemGroup, groupIndex: number) => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const contextMenuClassName = 'compass-context-menu';

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getContextMenuContent,
1313
type EnhancedMouseEvent,
1414
} from './context-menu-content';
15+
import { contextMenuClassName } from './consts';
1516

1617
export const ContextMenuContext = createContext<ContextMenuContextType | null>(
1718
null
@@ -76,7 +77,16 @@ export function ContextMenuProvider({
7677

7778
document.addEventListener('contextmenu', handleContextMenu);
7879
window.addEventListener('resize', handleClosingEvent);
79-
window.addEventListener('scroll', handleClosingEvent, { capture: true });
80+
window.addEventListener(
81+
'scroll',
82+
(e) => {
83+
const isCompassContextMenu =
84+
e.target instanceof HTMLElement &&
85+
e.target.classList.contains(contextMenuClassName);
86+
if (!isCompassContextMenu) handleClosingEvent(e);
87+
},
88+
{ capture: true }
89+
);
8090

8191
return () => {
8292
document.removeEventListener('contextmenu', handleContextMenu);

packages/compass-context-menu/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export type {
55
ContextMenuItemGroup,
66
ContextMenuWrapperProps,
77
} from './types';
8+
export { contextMenuClassName } from './consts';

0 commit comments

Comments
 (0)