Skip to content

fix(ContextMenu): position menu relative to context (scroll-follow + auto-flip) - #3866

Merged
cixzhang merged 1 commit into
mainfrom
fix/contextmenu-context-anchor
Jul 12, 2026
Merged

cixzhang merged 1 commit into
mainfrom
fix/contextmenu-context-anchor

Conversation

@cixzhang

Copy link
Copy Markdown
Contributor

Summary

Fixes ContextMenu positioning so the menu is anchored to the context (its trigger) rather than the page/viewport. The menu still opens under the cursor, but now:

  • follows the content on scroll — if the underlying container scrolls while the menu is open, the menu tracks the item it belongs to instead of staying pinned to a fixed screen point, and
  • auto-flips at the viewport edges — near an edge (including after scrolling), the browser repositions the menu so it stays on screen.

Resolves #3465.

Approach

The previous implementation used useLayer({ mode: 'fixed' }) and placed the menu at absolute viewport coordinates captured from the pointer — so it neither scroll-followed nor flipped.

This PR keeps the cursor position but expresses it relative to the trigger's context using CSS anchor positioning:

  1. A zero-size anchor element (<span>, aria-hidden) is rendered inside the trigger and positioned (via left/top) at the cursor point in the trigger's local coordinate space (offset from the trigger's top-left). Because the anchor lives inside the (scrollable) trigger, it moves with the content.
  2. useLayer is switched to mode: 'context', so the menu is anchored to that point with position-area + position-try-fallbacks: flip-block, flip-inline, …. A zero-size point anchor makes the flip mirror cleanly around the cursor.
  3. positionRef is retained, but now stores the cursor point in the trigger's local space rather than viewport coordinates.

No changes to useLayer were needed — context mode already supported everything. This is scoped to positioning only; the trigger wrapper and triggerXstyle (the separate display:contents migration, #3629) are left untouched.

Handlers

  • Right-click: viewport cursor → local offset via the trigger's getBoundingClientRect().
  • Keyboard (Shift+F10 / Menu key): coords (0,0) → anchor to the trigger's bottom-left (local 0, height), preserving the existing menus-8 behavior.
  • Touch long-press: touch point → local offset.

Testing

  • pnpm -F @astryxdesign/core typecheck — clean
  • pnpm -F @astryxdesign/core ContextMenu suite — 31 passing (3 new: cursor-anchor exists with an anchor-name, anchor placed at the pointer's trigger-relative offset, keyboard invocation anchors to the trigger bottom-left)
  • Table + Layer suites — 356 passing (no regressions)
  • eslint on the changed files — clean

The scroll-follow + auto-flip behavior is best validated interactively in a deployed build (jsdom does not implement CSS anchor positioning). Draft pending that check.

…llow + auto-flip

Anchor the right-click menu to a zero-size point placed at the cursor
*inside the trigger* and switch useLayer to context mode. The menu still
appears under the cursor, but is now positioned relative to the trigger's
context via CSS anchor positioning instead of fixed viewport coordinates:
it follows the content on scroll and the browser auto-flips it against the
viewport edges (position-area + flip fallbacks).

positionRef is retained but now stores the cursor point in the trigger's
local coordinate space rather than viewport coordinates.

Resolves #3465.
@vercel

vercel Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview, Comment Jul 12, 2026 6:39am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 12, 2026
github-actions Bot added a commit that referenced this pull request Jul 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.6KB 0B

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@cixzhang
cixzhang marked this pull request as ready for review July 12, 2026 19:24
@cixzhang
cixzhang requested a review from imdreamrunner as a code owner July 12, 2026 19:24

@thedjpetersen thedjpetersen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

This branch was successfully deployed

1 active deployment
Preview — d13f102f Deployed Jul 12, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ContextMenu: anchor the menu to the right-click target (scroll-follow + auto-flip) instead of fixed positioning

3 participants