Skip to content

feat(base-ui): port the Menu open/close + roving-focus path (Phase 3f stage 1) - #316

Merged
trueadm merged 2 commits into
mainfrom
feat/base-ui-menu
Jul 27, 2026
Merged

trueadm merged 2 commits into
mainfrom
feat/base-ui-menu

Conversation

@trueadm

@trueadm trueadm commented Jul 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Ports Base UI's Menu open/close + roving-focus path — Phase 3f stage 1 of 4.
  • @octanejs/base-ui/menu now exposes Menu.Root, Menu.Trigger, Menu.Portal, Menu.Positioner, Menu.Popup, Menu.createHandle and Menu.Handle, ported from @base-ui/react 1.6.0's menu/store, menu/root, menu/trigger, menu/portal, menu/positioner and menu/popup.
  • Subpath coverage 28/43 → 29/43.

Why

Phase 3e (useListNavigation, useTypeahead, both gridNavigation entry points, grid-utils, TimeoutManager, RequestQueue, getPseudoElementBounds, useMixedToggleClickHandler) merged in #308 with no tests, deliberately — a differential test needs a rendered component, and Menu is the first consumer. MenuRoot is where those hooks get wired to a real popup store, and Menu.Positioner supplies the CompositeList that feeds their element/label refs.

3e needed no repairs. It works as ported, which was the open question this stage existed to answer.

Changes

New — packages/base-ui/src/menu.ts (~2,000 loc)

MenuStore (parent-propagation observer, the full selector set, the setOpen-over-the-event-bus indirection that lets detached triggers and MenuHandle drive one root), MenuHandle/createMenuHandle, MenuRootContext/MenuSubmenuRootContext/MenuPortalContext/MenuPositionerContext, and the five components.

New — supporting

  • utils/findRootOwnerId.ts — verbatim; walks up to data-rootownerid so a mouseup landing in another popup of the same root is told apart from one outside the menu tree.
  • utils/MenubarContext.ts, utils/ContextMenuRootContext.ts — context-only, ahead of their components (stage 4). See "Scope calls" below.

Modified

  • utils/useOpenInteractionType.ts — adds the stateful useOpenInteractionType. Only the stateless useOpenMethodTriggerProps half had been ported, because Popover keeps openMethod in its store; Menu owns it locally.
  • utils/constants.ts — DROPDOWN_COLLISION_AVOIDANCE, TYPEAHEAD_RESET_MS, PATIENT_CLICK_THRESHOLD. popover.ts drops its local copy of the last one and imports it.
  • utils/createChangeEventDetails.ts — cancel-open / sibling-open reasons.
  • src/index.ts, status.json, docs/base-ui-migration-plan.md, docs/bindings-status.md (regenerated).

octane adaptations (all noted in the file header): createElement not JSX; forwardRef → ref-as-prop; per-call-site S(…) slots with subSlot for every composed hook; useIsoLayoutEffect → useLayoutEffect; fastComponent/fastComponentRef dropped. One event divergence: React's synthetic-only event.isPropagationStopped() in the popup's keyboard relay becomes a native event.cancelBubble read. Upstream's dev-only modal-on-a-nested-menu warning is dropped — this package carries no process.env.NODE_ENV branches.

Scope calls

Added beyond the stated stage-1 scope: two context-only modules (~80 loc). Menu branches on parent.type === 'menubar' | 'context-menu' in about a dozen places across Root, Trigger, Positioner and Popup. With the contexts present, every branch is transcribed verbatim rather than stubbed and re-threaded in stage 4. Nothing provides either context yet, so those branches are inert.

Left out of stage 1: menu/utils/stateAttributesMapping (itemMapping). It exists only to map CheckboxItem's checked state and depends on MenuCheckboxItemDataAttributes, a stage-2 file — porting it now means inventing that constant for code nothing calls. It is listed under stage 2 in the plan.

A rig limitation this surfaced

Two open-menu fixtures initially diverged: React's focus guards at tabindex="-1" data-tabindex="0", octane's at 0. Probed rather than normalised blind.

The rig mounts both runtimes into one jsdom document, and a Menu.Popup takes focus on open. Whichever popup is focused last makes the other runtime's FloatingPortal see a focusout whose relatedTarget is outside it; the portal's non-modal tab management answers with disableFocusInside(portalNode), which stamps tabindex="-1" data-tabindex="<prev>" on every tabbable in that portal — including the focus manager's own inside guards.

Both runtimes run identical code. The asymmetry is one document.activeElement for two apps, and which side loses flips with whatever the previous test left focused. Dialog and Popover dodge this by running a modal focus manager, which turns portal tab management off entirely; Menu's focus manager is modal only for ContextMenu, so it cannot.

parity.test.ts gains stepIgnoringGuardTabIndex, which strips only the guards' tabindex and byte-compares everything else — positioner styles, backdrop, roles, ids, aria/data attributes, portal structure. The reasoning is written out above the helper. Select and Combobox will hit the same thing.

Tests

5 differential fixtures (base-ui-diff.tsrx + parity.test.ts): closed Root+Trigger; open modal dropdown (portal, internal backdrop, positioner, popup); open non-modal; open with explicit side/align/sideOffset; open with a disabled root.

8 behavior tests (tests/menu.test.ts) for what the rig cannot see: click toggle open/closed, arrow-key open from the trigger (with a negative case so it can't pass on any keydown), Escape dismiss, the trigger↔popup ARIA contract (aria-haspopup, aria-expanded, aria-controls↔popup id, aria-labelledby, data-rootownerid, data-popup-open), aria-orientation reaching the popup from useListNavigation, and focus entering the popup on open then returning to the trigger on close.

Plus the detached-trigger / imperative-handle path (added in response to review): a Menu.Trigger rendered as a sibling of Menu.Root, associated only by a shared MenuHandle — opened through the detached trigger, closed via handle.close(), reopened via handle.open(triggerId) with the popup's aria-labelledby resolving to that trigger, and handle.open() throwing for an unregistered id. This is the only path that routes through MenuStore.setOpen → the floating root context's event bus (a normal trigger click goes through FloatingRootStore.setOpen → onOpenChange), so it is what pins the context actually being installed on the store. Negative control: deleting floatingRootContext from usePopupInteractionProps' state part fails six of the eight tests.

Not covered: item-level roving focus and typeahead matching need Menu.Item, which lands in stage 2. Stage 1's tests cover the layer directly below.

namespace-parts.test.ts gains a Menu entry listing the stage-1 surface, to be extended as each stage lands.

Validation

  • pnpm format:check
  • pnpm typecheck (repo-wide, exit 0)
  • pnpm test — 1406 files / 14,130 tests passed
  • targeted: pnpm --filter @octanejs/base-ui exec tsrx-tsc --noEmit -p tsconfig.json; pnpm vitest run --project base-ui (110 passed); --project base-ui-ssr (3 passed)

Risk / follow-ups

  • Stage 2 (~1,400 loc): Item, CheckboxItem(+Indicator), RadioGroup/RadioItem(+Indicator), Group/GroupLabel, LinkItem, Separator, Arrow, Backdrop, Viewport, and menu/utils/stateAttributesMapping. This is where item-level roving focus, typeahead matching and checkbox/radio semantics get tested.
  • Stage 3 (~340 loc): SubmenuRoot + SubmenuTrigger.
  • Stage 4 (~650 loc): Menubar and ContextMenu, which fill in the already-transcribed parent branches and provide the two context-only modules from this PR.
  • The menubar / context-menu / nested-context-menu code paths ship inert here — nothing constructs those parent values until stage 4, so they are untested by construction.
  • SSR/hydration coverage for an open menu is not added; open overlays remain uncovered there generally (tracked in status.json).

🤖 Generated with Claude Code

… stage 1)

`@octanejs/base-ui/menu` now exposes `Menu.Root`, `Menu.Trigger`, `Menu.Portal`,
`Menu.Positioner`, `Menu.Popup`, `Menu.createHandle` and `Menu.Handle`, ported
from `@base-ui/react` 1.6.0's `menu/store`, `menu/root`, `menu/trigger`,
`menu/portal`, `menu/positioner` and `menu/popup`.

This is the first CONSUMER of the Phase 3e list-navigation infrastructure, which
merged (#308) deliberately untested because a differential test needs a rendered
component. `MenuRoot` wires `useListNavigation` + `useTypeahead` to the popup
store and `Menu.Positioner` wraps the popup in the `CompositeList` that feeds
their element/label refs. 3e needed no repairs.

Supporting additions: `useOpenInteractionType` (only the stateless
`useOpenMethodTriggerProps` half had been ported, since Popover keeps
`openMethod` in its store), `DROPDOWN_COLLISION_AVOIDANCE` /
`TYPEAHEAD_RESET_MS` / `PATIENT_CLICK_THRESHOLD` in `utils/constants` (Popover's
local copy of the last one now imports it), `findRootOwnerId`, and the
`cancel-open` / `sibling-open` change reasons.

`MenubarContext` and `ContextMenuRootContext` land as context-only modules ahead
of their components (stage 4), so Menu's ~dozen `parent`-union branches are
transcribed verbatim rather than stubbed and re-threaded later. Nothing provides
them yet, so those branches are inert.

Tests: 5 differential fixtures (closed trigger; open modal, non-modal, explicit
placement, and disabled roots) plus 6 behavior tests for what the rig cannot
see — click toggle, arrow-key open, Escape dismiss, the ARIA contract,
`aria-orientation` reaching the popup from `useListNavigation`, and focus
entering the popup and returning to the trigger.

A `Menu.Popup` takes focus on open and the rig mounts both runtimes in one jsdom
document, so whichever popup is focused last makes the other runtime's portal
see an outward `focusout` and run `disableFocusInside`, rewriting that portal's
focus-guard `tabindex`. Both runtimes run identical code; the asymmetry is one
`document.activeElement` for two apps. `parity.test.ts` normalises the guards'
tabindex away for the open-menu fixtures and byte-compares everything else.

Items, submenus, `Viewport`/`Arrow`/`Backdrop`/`Separator`, `Menubar` and
`ContextMenu` land in stages 2-4.

Co-Authored-By: Claude Opus 5 <[email protected]>
@vercel

vercel Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

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

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
octane-website Skipped Skipped Jul 27, 2026 9:01pm
octane-website-mcp Skipped Skipped Jul 27, 2026 9:01pm

Request Review

@vercel
vercel Bot temporarily deployed to Preview – octane-website-mcp July 27, 2026 20:45 Inactive
@vercel
vercel Bot temporarily deployed to Preview – octane-website July 27, 2026 20:46 Inactive

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Disconnected floating root context
    • Created a real FloatingRootStore via createPopupFloatingRootContext in MenuStore's constructor and passed store.state.floatingRootContext to useSyncedFloatingRootContext in MenuRoot, matching the pattern used by Dialog/Popover/Tooltip stores.

Create PR

Or push these changes by commenting:

@cursor push fc1600af8a
Preview (fc1600af8a)
diff --git a/packages/base-ui/src/menu.ts b/packages/base-ui/src/menu.ts
--- a/packages/base-ui/src/menu.ts
+++ b/packages/base-ui/src/menu.ts
@@ -119,6 +119,7 @@ import type { ContextMenuRootContextValue } from './utils/ContextMenuRootContext
 import {
 	attachPreventUnmountOnClose,
 	createInitialPopupStoreState,
+	createPopupFloatingRootContext,
 	popupStoreSelectors,
 	setPopupOpenState,
 	useImplicitActiveTrigger,

@@ -119,6 +119,7 @@ import type { ContextMenuRootContextValue } from './utils/ContextMenuRootContext
 import {
 	attachPreventUnmountOnClose,
 	createInitialPopupStoreState,
+	createPopupFloatingRootContext,
 	popupStoreSelectors,
 	setPopupOpenState,
 	useImplicitActiveTrigger,
@@ -325,8 +326,12 @@ export class MenuStore<Payload> extends ReactStore<
 	private unsubscribeParentListener: (() => void) | null = null;
 
 	constructor(initialState?: Partial<MenuState<Payload>>) {
+		const initial = { ...createInitialMenuState<Payload>(), ...initialState };
+		const triggerElements = new PopupTriggerMap();
+		initial.floatingRootContext = createPopupFloatingRootContext(triggerElements);
+
 		super(
-			{ ...createInitialMenuState<Payload>(), ...initialState },
+			initial,
 			{
 				positionerRef: { current: null },
 				popupRef: { current: null },

@@ -325,8 +326,12 @@ export class MenuStore<Payload> extends ReactStore<
 	private unsubscribeParentListener: (() => void) | null = null;
 
 	constructor(initialState?: Partial<MenuState<Payload>>) {
+		const initial = { ...createInitialMenuState<Payload>(), ...initialState };
+		const triggerElements = new PopupTriggerMap();
+		initial.floatingRootContext = createPopupFloatingRootContext(triggerElements);
+
 		super(
-			{ ...createInitialMenuState<Payload>(), ...initialState },
+			initial,
 			{
 				positionerRef: { current: null },
 				popupRef: { current: null },
@@ -337,7 +342,7 @@ export class MenuStore<Payload> extends ReactStore<
 				triggerFocusTargetRef: { current: null },
 				beforeContentFocusGuardRef: { current: null },
 				onOpenChangeComplete: undefined,
-				triggerElements: new PopupTriggerMap(),
+				triggerElements,
 			} as MenuContext,
 			menuSelectors,
 		);

@@ -337,7 +342,7 @@ export class MenuStore<Payload> extends ReactStore<
 				triggerFocusTargetRef: { current: null },
 				beforeContentFocusGuardRef: { current: null },
 				onOpenChangeComplete: undefined,
-				triggerElements: new PopupTriggerMap(),
+				triggerElements,
 			} as MenuContext,
 			menuSelectors,
 		);
@@ -821,6 +826,7 @@ function MenuRoot<Payload>(props: any): any {
 	const floatingRootContext = useSyncedFloatingRootContext(
 		{
 			popupStore: store as any,
+			floatingRootContext: store.state.floatingRootContext,
 			floatingId,
 			nested: floatingParentNodeIdFromContext != null,
 			onOpenChange: setOpen,

@@ -821,6 +826,7 @@ function MenuRoot<Payload>(props: any): any {
 	const floatingRootContext = useSyncedFloatingRootContext(
 		{
 			popupStore: store as any,
+			floatingRootContext: store.state.floatingRootContext,
 			floatingId,
 			nested: floatingParentNodeIdFromContext != null,
 			onOpenChange: setOpen,

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit b4d2510. Configure here.

Comment thread packages/base-ui/src/menu.ts Resolved
Review flagged `MenuRoot`'s floating root context as never installed on
`MenuStore`. It is — `usePopupInteractionProps` syncs its whole state part, and
`floatingRootContext` is in it, exactly as upstream `MenuRoot.tsx` does it. The
real gap was coverage: nothing exercised the path that routes through
`MenuStore.setOpen` → the context's event bus. A normal trigger click goes
through `FloatingRootStore.setOpen` → `onOpenChange` instead, so it would not
have caught a dead bus.

Adds a `MenuWithHandle` fixture whose `Menu.Trigger` is a SIBLING of `Menu.Root`,
associated only by a shared `MenuHandle`, and tests that open through the
detached trigger, close through `handle.close()`, reopen through
`handle.open(triggerId)` with the popup's `aria-labelledby` resolving to that
trigger, and throw for an unregistered id.

Negative control: deleting `floatingRootContext` from that state part fails six
of the eight tests in `tests/menu.test.ts`.

Also documents the install site — Menu cannot build its context in the store
constructor the way Dialog/Popover do, because its store may be an external
`MenuHandle` store shared with triggers outside the Root, so the context must be
created by the Root that owns `setOpen` and published from there.

Co-Authored-By: Claude Opus 5 <[email protected]>
@vercel
vercel Bot temporarily deployed to Preview – octane-website July 27, 2026 21:01 Inactive
@vercel
vercel Bot temporarily deployed to Preview – octane-website-mcp July 27, 2026 21:01 Inactive
@trueadm
trueadm merged commit e1004af into main Jul 27, 2026
28 checks passed
@trueadm
trueadm deleted the feat/base-ui-menu branch July 27, 2026 21:23

This branch was previously deployed

2 inactive deployments
Preview – octane-website-mcp — 2856932c Deployed Jul 27, 2026 by vercel[bot]
Preview – octane-website — 2856932c Deployed Jul 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant