-
Notifications
You must be signed in to change notification settings - Fork 247
chore(context-menu): use anchor ref and compact menu #7073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||||||
| import React, { useEffect, useMemo, useRef } from 'react'; | ||||||||||||||||||||||
| import React, { useMemo, useRef } from 'react'; | ||||||||||||||||||||||
| import { Menu, MenuItem, MenuSeparator } from './leafygreen'; | ||||||||||||||||||||||
| import { css } from '@leafygreen-ui/emotion'; | ||||||||||||||||||||||
| import { spacing } from '@leafygreen-ui/tokens'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import { | ||||||||||||||||||||||
| ContextMenuProvider as ContextMenuProviderBase, | ||||||||||||||||||||||
|
|
@@ -11,6 +13,19 @@ import { | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| export type { ContextMenuItem } from '@mongodb-js/compass-context-menu'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // TODO: Remove these once https://jira.mongodb.org/browse/LG-5013 is resolved | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const menuStyles = css({ | ||||||||||||||||||||||
| paddingTop: spacing[150], | ||||||||||||||||||||||
| paddingBottom: spacing[150], | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const itemStyles = css({ | ||||||||||||||||||||||
| paddingTop: 0, | ||||||||||||||||||||||
| paddingBottom: 0, | ||||||||||||||||||||||
| fontSize: '.8em', | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export function ContextMenuProvider({ | ||||||||||||||||||||||
| children, | ||||||||||||||||||||||
| }: { | ||||||||||||||||||||||
|
|
@@ -25,32 +40,39 @@ export function ContextMenuProvider({ | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| export function ContextMenu({ menu }: ContextMenuWrapperProps) { | ||||||||||||||||||||||
| const menuRef = useRef(null); | ||||||||||||||||||||||
| const anchorRef = useRef<HTMLDivElement>(null); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const { position, itemGroups } = menu; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||
| if (!menu.isOpen) { | ||||||||||||||||||||||
| menu.close(); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }, [menu.isOpen]); | ||||||||||||||||||||||
| // TODO: Remove when https://jira.mongodb.org/browse/LG-5342 is resolved | ||||||||||||||||||||||
| if (anchorRef.current) { | ||||||||||||||||||||||
| anchorRef.current.style.left = `${position.x}px`; | ||||||||||||||||||||||
| anchorRef.current.style.top = `${position.y}px`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+48
to
+51
|
||||||||||||||||||||||
| if (anchorRef.current) { | |
| anchorRef.current.style.left = `${position.x}px`; | |
| anchorRef.current.style.top = `${position.y}px`; | |
| } | |
| React.useLayoutEffect(() => { | |
| if (anchorRef.current) { | |
| anchorRef.current.style.left = `${position.x}px`; | |
| anchorRef.current.style.top = `${position.y}px`; | |
| } | |
| }, [position]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, we have to do this outside of a useEffect or useLayoutEffect for it to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to address this when that ticket is resolved, we can create a ticket marked as
Blockedand link the tickets as depends upon. Then when it's resolved that ticket will get set to open and we get reminded of it.Fine as is, I figured I'd mention in case you haven't done that flow before.