Skip to content

Commit 66beb5b

Browse files
committed
Use anchor ref and force a compact menu
1 parent 995fcc1 commit 66beb5b

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
screen,
66
cleanup,
77
waitFor,
8+
within,
89
} from '@mongodb-js/testing-library-compass';
910
import { expect } from 'chai';
1011
import { ContentWithFallback } from './content-with-fallback';
@@ -48,7 +49,7 @@ describe('ContentWithFallback', function () {
4849
expect(screen.getByText('I am ready!')).to.exist;
4950
});
5051

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

5455
render(
@@ -59,8 +60,10 @@ describe('ContentWithFallback', function () {
5960
);
6061

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

6669
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: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import React, { useEffect, useMemo, useRef } from 'react';
1+
import React, { useMemo, useRef } from 'react';
22
import { Menu, MenuItem, MenuSeparator } from './leafygreen';
3-
43
import { css } from '@leafygreen-ui/emotion';
54
import { spacing } from '@leafygreen-ui/tokens';
65

@@ -14,9 +13,11 @@ import {
1413

1514
export type { ContextMenuItem } from '@mongodb-js/compass-context-menu';
1615

16+
// TODO: Remove these once https://jira.mongodb.org/browse/LG-5013 is resolved
17+
1718
const menuStyles = css({
18-
paddingTop: spacing[100],
19-
paddingBottom: spacing[100],
19+
paddingTop: spacing[150],
20+
paddingBottom: spacing[150],
2021
});
2122

2223
const itemStyles = css({
@@ -39,33 +40,39 @@ export function ContextMenuProvider({
3940

4041
export function ContextMenu({ menu }: ContextMenuWrapperProps) {
4142
const menuRef = useRef(null);
43+
const anchorRef = useRef<HTMLDivElement>(null);
4244

4345
const { position, itemGroups } = menu;
4446

45-
useEffect(() => {
46-
if (!menu.isOpen) {
47-
menu.close();
48-
}
49-
}, [menu.isOpen]);
47+
// TODO: Remove when https://jira.mongodb.org/browse/LG-5342 is resolved
48+
if (anchorRef.current) {
49+
anchorRef.current.style.left = `${position.x}px`;
50+
anchorRef.current.style.top = `${position.y}px`;
51+
}
5052

5153
return (
52-
<div
53-
data-testid="context-menu"
54-
style={{
55-
position: 'absolute',
56-
left: position.x,
57-
top: position.y,
58-
// This is to ensure the menu gets positioned correctly as the left and top updates
59-
width: 1,
60-
height: 1,
61-
}}
62-
>
54+
<div data-testid="context-menu-container">
55+
<div
56+
data-testid="context-menu-anchor"
57+
ref={anchorRef}
58+
style={{
59+
position: 'absolute',
60+
left: position.x,
61+
top: position.y,
62+
// This is to ensure the menu gets positioned correctly as the left and top updates
63+
width: 1,
64+
height: 1,
65+
}}
66+
/>
6367
<Menu
68+
data-testid="context-menu"
69+
refEl={anchorRef}
6470
ref={menuRef}
6571
open={menu.isOpen}
6672
setOpen={menu.close}
6773
justify="start"
6874
className={menuStyles}
75+
maxHeight={Number.MAX_SAFE_INTEGER}
6976
>
7077
{itemGroups.map((items: ContextMenuItemGroup, groupIndex: number) => {
7178
return (

0 commit comments

Comments
 (0)