-
Notifications
You must be signed in to change notification settings - Fork 229
fix(context-menu): position anchor in a way which escape containers COMPASS-9673 #7192
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR fixes a positioning issue in the context menu component when it's embedded within containers that have position: relative
. The fix changes the anchor positioning from absolute
to fixed
to ensure the context menu is positioned relative to the viewport rather than the containing element.
- Changes context menu anchor positioning from
absolute
tofixed
I think we discussed this on the standup, do you mind commiting this code also so that we have an environment that's a bit closer to mms? |
1bc2235
to
c436d82
Compare
} | ||
#container { | ||
--offset: 30px; | ||
position: absolute; |
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.
Using position absolute and having an offset in every direction ensures any position: relative;
contained will resolve its position relative to this with the offset, triggering the original issue.
@@ -62,18 +64,16 @@ export function ContextMenuProvider({ | |||
); | |||
|
|||
useEffect(() => { | |||
// Don't set up event listeners if we have a parent context | |||
if (parentContext || disabled) return; | |||
// We skip registering listeners when parentContext is known to avoid registering multiple (nested) listeners |
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.
Drive-by to have the comment say something about, why we're actually doing this.
@@ -86,7 +94,7 @@ export function ContextMenu({ | |||
data-testid="context-menu-anchor" | |||
ref={anchorRef} | |||
style={{ | |||
position: 'absolute', | |||
position: 'fixed', |
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.
The actual fix for the original bug.
@@ -17,6 +17,10 @@ export type { | |||
ContextMenuItemGroup, | |||
} from '@mongodb-js/compass-context-menu'; | |||
|
|||
const containerStyles = css({ |
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.
This container is not just a part of the provider from the compass-context-menu
package, because we need access to the css
helper, which would introduce a circular dependency.
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.
This probably should be display: contents
so that it doesn't create its own box and allows parent and children to keep their styles without the need to account for this extra element (we do something pretty similar here).
Also if this belongs to the context-menu rather than to compass-components, it should probably stay there, you can inline styles with a style property to avoid the dependency, although ContextMenuProviderBase
being configurable like that also looks good to me. I would rather pass the ref directly maybe, this seems to be a more common pattern that I see in these cases, but no strong preference for that, local state keeping the value around is also okay.
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.
This probably should be
display: contents
Great idea - thanks for pointing to that.
if this belongs to the context-menu rather than to compass-components, it should probably stay there
This was mostly to avoid inline styling, so I agree this could live in the context menu package, when it's just a display: contents
.
ceb1200
to
d886c3a
Compare
d886c3a
to
332bfe0
Compare
For some reason, this test is failing: compass/packages/compass-components/src/components/workspace-tabs/tab.spec.tsx Lines 94 to 98 in 8f183d0
It doesn't fail when I run it in isolation (using Also tried changing this to (without luck - still failing when all tests are ran):
|
Description
When embedded as the data-explorer, the right click menu is rendered inside a container with
position: relative;
.When the context menu anchor is positioned absolute, it will be relative to that container.
Merging this PR will:
I tested this locally by modifying the
compass-web
sandbox to reproduce the bug and update the code to fix it.I pushes a commit with the change I did to the sandbox and verified using the "compass-web" sandbox as well as "compass".
Checklist
Motivation and Context
Open Questions
Dependents
Types of changes