Repository navigation
feat(base-ui): port the Menu open/close + roving-focus path (Phase 3f stage 1) - #316
Merged
Merged
Conversation
… 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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
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.
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.
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]>
This was referenced Jul 27, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Menuopen/close + roving-focus path — Phase 3f stage 1 of 4.@octanejs/base-ui/menunow exposesMenu.Root,Menu.Trigger,Menu.Portal,Menu.Positioner,Menu.Popup,Menu.createHandleandMenu.Handle, ported from@base-ui/react1.6.0'smenu/store,menu/root,menu/trigger,menu/portal,menu/positionerandmenu/popup.Why
Phase 3e (
useListNavigation,useTypeahead, bothgridNavigationentry 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.MenuRootis where those hooks get wired to a real popup store, andMenu.Positionersupplies theCompositeListthat 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, thesetOpen-over-the-event-bus indirection that lets detached triggers andMenuHandledrive one root),MenuHandle/createMenuHandle,MenuRootContext/MenuSubmenuRootContext/MenuPortalContext/MenuPositionerContext, and the five components.New — supporting
utils/findRootOwnerId.ts— verbatim; walks up todata-rootowneridso 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 statefuluseOpenInteractionType. Only the statelessuseOpenMethodTriggerPropshalf had been ported, because Popover keepsopenMethodin its store; Menu owns it locally.utils/constants.ts—DROPDOWN_COLLISION_AVOIDANCE,TYPEAHEAD_RESET_MS,PATIENT_CLICK_THRESHOLD.popover.tsdrops its local copy of the last one and imports it.utils/createChangeEventDetails.ts—cancel-open/sibling-openreasons.src/index.ts,status.json,docs/base-ui-migration-plan.md,docs/bindings-status.md(regenerated).octane adaptations (all noted in the file header):
createElementnot JSX;forwardRef→ ref-as-prop; per-call-siteS(…)slots withsubSlotfor every composed hook;useIsoLayoutEffect→useLayoutEffect;fastComponent/fastComponentRefdropped. One event divergence: React's synthetic-onlyevent.isPropagationStopped()in the popup's keyboard relay becomes a nativeevent.cancelBubbleread. Upstream's dev-onlymodal-on-a-nested-menu warning is dropped — this package carries noprocess.env.NODE_ENVbranches.Scope calls
Added beyond the stated stage-1 scope: two context-only modules (~80 loc).
Menubranches onparent.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 mapCheckboxItem'scheckedstate and depends onMenuCheckboxItemDataAttributes, 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 at0. Probed rather than normalised blind.The rig mounts both runtimes into one jsdom document, and a
Menu.Popuptakes focus on open. Whichever popup is focused last makes the other runtime'sFloatingPortalsee afocusoutwhoserelatedTargetis outside it; the portal's non-modal tab management answers withdisableFocusInside(portalNode), which stampstabindex="-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.activeElementfor 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 forContextMenu, so it cannot.parity.test.tsgainsstepIgnoringGuardTabIndex, 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): closedRoot+Trigger; open modal dropdown (portal, internal backdrop, positioner, popup); open non-modal; open with explicitside/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-orientationreaching the popup fromuseListNavigation, 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.Triggerrendered as a sibling ofMenu.Root, associated only by a sharedMenuHandle— opened through the detached trigger, closed viahandle.close(), reopened viahandle.open(triggerId)with the popup'saria-labelledbyresolving to that trigger, andhandle.open()throwing for an unregistered id. This is the only path that routes throughMenuStore.setOpen→ the floating root context's event bus (a normal trigger click goes throughFloatingRootStore.setOpen→onOpenChange), so it is what pins the context actually being installed on the store. Negative control: deletingfloatingRootContextfromusePopupInteractionProps' 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.tsgains aMenuentry listing the stage-1 surface, to be extended as each stage lands.Validation
pnpm format:checkpnpm typecheck(repo-wide, exit 0)pnpm test— 1406 files / 14,130 tests passedpnpm --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
Item,CheckboxItem(+Indicator),RadioGroup/RadioItem(+Indicator),Group/GroupLabel,LinkItem,Separator,Arrow,Backdrop,Viewport, andmenu/utils/stateAttributesMapping. This is where item-level roving focus, typeahead matching and checkbox/radio semantics get tested.SubmenuRoot+SubmenuTrigger.MenubarandContextMenu, which fill in the already-transcribed parent branches and provide the two context-only modules from this PR.menubar/context-menu/nested-context-menucode paths ship inert here — nothing constructs those parent values until stage 4, so they are untested by construction.status.json).🤖 Generated with Claude Code