1- import React , { useEffect , useMemo , useRef } from 'react' ;
1+ import React , { useMemo , useRef } from 'react' ;
22import { Menu , MenuItem , MenuSeparator } from './leafygreen' ;
3+ import { css } from '@leafygreen-ui/emotion' ;
4+ import { spacing } from '@leafygreen-ui/tokens' ;
35
46import {
57 ContextMenuProvider as ContextMenuProviderBase ,
@@ -11,6 +13,19 @@ import {
1113
1214export type { ContextMenuItem } from '@mongodb-js/compass-context-menu' ;
1315
16+ // TODO: Remove these once https://jira.mongodb.org/browse/LG-5013 is resolved
17+
18+ const menuStyles = css ( {
19+ paddingTop : spacing [ 150 ] ,
20+ paddingBottom : spacing [ 150 ] ,
21+ } ) ;
22+
23+ const itemStyles = css ( {
24+ paddingTop : 0 ,
25+ paddingBottom : 0 ,
26+ fontSize : '.8em' ,
27+ } ) ;
28+
1429export function ContextMenuProvider ( {
1530 children,
1631 disabled,
@@ -27,32 +42,39 @@ export function ContextMenuProvider({
2742
2843export function ContextMenu ( { menu } : ContextMenuWrapperProps ) {
2944 const menuRef = useRef ( null ) ;
45+ const anchorRef = useRef < HTMLDivElement > ( null ) ;
3046
3147 const { position, itemGroups } = menu ;
3248
33- useEffect ( ( ) => {
34- if ( ! menu . isOpen ) {
35- menu . close ( ) ;
36- }
37- } , [ menu . isOpen ] ) ;
49+ // TODO: Remove when https://jira.mongodb.org/browse/LG-5342 is resolved
50+ if ( anchorRef . current ) {
51+ anchorRef . current . style . left = ` ${ position . x } px` ;
52+ anchorRef . current . style . top = ` ${ position . y } px` ;
53+ }
3854
3955 return (
40- < div
41- data-testid = "context-menu"
42- style = { {
43- position : 'absolute' ,
44- left : position . x ,
45- top : position . y ,
46- // This is to ensure the menu gets positioned correctly as the left and top updates
47- width : 1 ,
48- height : 1 ,
49- } }
50- >
56+ < div data-testid = "context-menu-container" >
57+ < div
58+ data-testid = "context-menu-anchor"
59+ ref = { anchorRef }
60+ style = { {
61+ position : 'absolute' ,
62+ left : position . x ,
63+ top : position . y ,
64+ // This is to ensure the menu gets positioned correctly as the left and top updates
65+ width : 1 ,
66+ height : 1 ,
67+ } }
68+ />
5169 < Menu
70+ data-testid = "context-menu"
71+ refEl = { anchorRef }
5272 ref = { menuRef }
5373 open = { menu . isOpen }
5474 setOpen = { menu . close }
5575 justify = "start"
76+ className = { menuStyles }
77+ maxHeight = { Number . MAX_SAFE_INTEGER }
5678 >
5779 { itemGroups . map ( ( items : ContextMenuItemGroup , groupIndex : number ) => {
5880 return (
@@ -66,6 +88,7 @@ export function ContextMenu({ menu }: ContextMenuWrapperProps) {
6688 key = { `menu-group-${ groupIndex } -item-${ itemIndex } ` }
6789 data-text = { item . label }
6890 data-testid = { `menu-group-${ groupIndex } -item-${ itemIndex } ` }
91+ className = { itemStyles }
6992 onClick = { ( evt : React . MouseEvent ) => {
7093 item . onAction ?.( evt ) ;
7194 menu . close ( ) ;
0 commit comments