Skip to content

Commit f4fbe29

Browse files
authored
chore(context-menu): use anchor ref and compact menu (#7073)
* Use compact styling * Use anchor ref and force a compact menu
1 parent b4604a6 commit f4fbe29

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

packages/compass-components/src/components/content-with-fallback.spec.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('ContentWithFallback', function () {
4848
expect(screen.getByText('I am ready!')).to.exist;
4949
});
5050

51-
it('should render nothing when content is not ready on the first render', function () {
51+
it('should render only the context menu when content is not ready on the first render', function () {
5252
const container = document.createElement('div');
5353

5454
render(
@@ -59,8 +59,10 @@ describe('ContentWithFallback', function () {
5959
);
6060

6161
expect(container.children.length).to.equal(1);
62-
const [anchorElement] = container.children;
63-
expect(anchorElement.getAttribute('data-testid')).to.equal('context-menu');
62+
const [contextMenuContainer] = container.children;
63+
expect(contextMenuContainer.getAttribute('data-testid')).to.equal(
64+
'context-menu-container'
65+
);
6466
});
6567

6668
it('should render fallback when the timeout passes', async function () {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('useContextMenuItems', function () {
4646

4747
// Should only find one context menu (from the parent provider)
4848
expect(
49-
container.querySelectorAll('[data-testid="context-menu"]')
49+
container.querySelectorAll('[data-testid="context-menu-container"]')
5050
).to.have.length(1);
5151
// Should still render the trigger
5252
expect(screen.getByTestId(menuTestTriggerId)).to.exist;

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

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import React, { useEffect, useMemo, useRef } from 'react';
1+
import React, { useMemo, useRef } from 'react';
22
import { Menu, MenuItem, MenuSeparator } from './leafygreen';
3+
import { css } from '@leafygreen-ui/emotion';
4+
import { spacing } from '@leafygreen-ui/tokens';
35

46
import {
57
ContextMenuProvider as ContextMenuProviderBase,
@@ -11,6 +13,19 @@ import {
1113

1214
export 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+
1429
export function ContextMenuProvider({
1530
children,
1631
disabled,
@@ -27,32 +42,39 @@ export function ContextMenuProvider({
2742

2843
export 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

Comments
 (0)