1
- import React , { useEffect , useMemo , useRef } from 'react' ;
1
+ import React , { useMemo , useRef } from 'react' ;
2
2
import { Menu , MenuItem , MenuSeparator } from './leafygreen' ;
3
+ import { css } from '@leafygreen-ui/emotion' ;
4
+ import { spacing } from '@leafygreen-ui/tokens' ;
3
5
4
6
import {
5
7
ContextMenuProvider as ContextMenuProviderBase ,
@@ -11,6 +13,19 @@ import {
11
13
12
14
export type { ContextMenuItem } from '@mongodb-js/compass-context-menu' ;
13
15
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
+
14
29
export function ContextMenuProvider ( {
15
30
children,
16
31
disabled,
@@ -27,32 +42,39 @@ export function ContextMenuProvider({
27
42
28
43
export function ContextMenu ( { menu } : ContextMenuWrapperProps ) {
29
44
const menuRef = useRef ( null ) ;
45
+ const anchorRef = useRef < HTMLDivElement > ( null ) ;
30
46
31
47
const { position, itemGroups } = menu ;
32
48
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
+ }
38
54
39
55
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
+ />
51
69
< Menu
70
+ data-testid = "context-menu"
71
+ refEl = { anchorRef }
52
72
ref = { menuRef }
53
73
open = { menu . isOpen }
54
74
setOpen = { menu . close }
55
75
justify = "start"
76
+ className = { menuStyles }
77
+ maxHeight = { Number . MAX_SAFE_INTEGER }
56
78
>
57
79
{ itemGroups . map ( ( items : ContextMenuItemGroup , groupIndex : number ) => {
58
80
return (
@@ -66,6 +88,7 @@ export function ContextMenu({ menu }: ContextMenuWrapperProps) {
66
88
key = { `menu-group-${ groupIndex } -item-${ itemIndex } ` }
67
89
data-text = { item . label }
68
90
data-testid = { `menu-group-${ groupIndex } -item-${ itemIndex } ` }
91
+ className = { itemStyles }
69
92
onClick = { ( evt : React . MouseEvent ) => {
70
93
item . onAction ?.( evt ) ;
71
94
menu . close ( ) ;
0 commit comments