Skip to content

[toolbar] Keep the tab stop on an item focused on mount - #6360

Open
jjenzz wants to merge 4 commits into
mui:masterfrom
jjenzz:codex/base-ui-issue-2ba7fa08-bca8-4f5b-a694-7ff72ce9d285
Open

jjenzz wants to merge 4 commits into
mui:masterfrom
jjenzz:codex/base-ui-issue-2ba7fa08-bca8-4f5b-a694-7ff72ce9d285

Conversation

@jjenzz

@jjenzz jjenzz commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fixes #6357

  • Cause: When an app focuses a toolbar item while the toolbar is still registering its items, in a mount effect or with autoFocus, the item reports its index before it has received it, so the index is -1. The toolbar then makes no item the tab stop: every item gets tabindex -1 and Tab and Shift+Tab skip the whole toolbar.
  • Fix and rationale: The composite root already keeps the list of item elements and owns the tab stop. Its focus handler now finds the focused element in that list and makes it the tab stop, so an item's stale index no longer matters. When the list resolves for the first time, an item that already has focus becomes the tab stop, which covers autoFocus before the list exists. Without mount focus the first enabled item keeps the tab stop. Tabs, Radio Group and the other composites share this code and the fix.
  • Considered: Ignore item focus while its index is still -1 — Keeps a tab stop, but on the first item. With autoFocus on Zoom in, or Zoom in focused on mount, Tab went back to Zoom out instead of the focused item in real Chrome.
  • Considered: Let every item guess its index from render order on first render — Fixes most cases but picks the wrong item when an item is inserted before the others: a newly added first item that focused itself lost the tab stop to Zoom in.
  • Considered: Document focusing in a 0 ms timeout as the workaround — Works for focus from an effect in real Chrome, but not for the autoFocus prop, and leaves each app to discover the timing rule.
  • Verification: Chromium, jsdom, ESLint, TypeScript, formatting. Regression fails before and passes after the fix.
BEFORE AFTER
before.mp4
after.mp4

@jjenzz jjenzz added component: toolbar Changes related to the toolbar component. type: bug It doesn't behave as expected. labels Nov 26, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

commit: 778cd66

@code-infra-dashboard

code-infra-dashboard Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+135B(+0.03%) 🔺+57B(+0.03%)

Details of bundle changes

Performance

No regressions in 36 benchmarks · precise to ±8.8% (±21.4% at worst) · ran 4m 28s · details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 778cd66
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6b098519c53c4919f061ac5d
😎 Deploy Preview https://deploy-preview-6360--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@jjenzz

This comment has been minimized.

Items that mount later with autoFocus now take the tab stop, and the
regression tests move to the CompositeRoot suite that owns the behavior.

Amp-Thread-ID: https://ampcode.com/threads/T-01a217e9-59d3-7464-931f-2caae17d6e59
Co-authored-by: Amp <amp@ampcode.com>
@jjenzz

jjenzz commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

PR review

Nothing blocks the merge, and I found no actionable findings. The change adds two ways to recover the tab stop. The root onFocus now looks the focused item up in elementsRef. onMapChange now prefers the focused element, both on the first map resolution and later when the highlighted element is gone. I read both paths against useCompositeItem and every consumer of useCompositeRoot/CompositeRoot: Toolbar, ToggleGroup, RadioGroup, Tabs, Menubar, NavigationMenu, MenuTrigger and useButton. Nothing regressed. I did not run any tests; I only read the code.

The earlier finding about an autoFocus item that mounts later is fixed in the current code. focusedElement is computed before the hasSetDefaultIndexRef branch, and the nextIndex === -1 && focusedIndex !== -1 branch moves the tab stop to the focused item. A test covers it in CompositeRoot.test.tsx.

What I checked and why each is safe:

  • Order of the two focus handlers. The item's onFocus runs first and may report its stale index (-1). The root's onFocus then runs with the index looked up from the list, so the root's value wins. Every consumer either uses internal state or, for Tabs, passes a plain state setter (setHighlightedTabIndex), so setting the index twice has no side effects.
  • Nested and portaled composites. Menubar menu popups and composites inside a Toolbar bubble focus events through the React tree. The target is not in the outer list, so findIndex returns -1 and nothing happens.
  • Focused item versus active item on first resolution. A focused element now wins over an item marked ACTIVE_COMPOSITE_ITEM. That matches what already happens when an item is focused after mount, so it adds no new behavior.
  • Shadow DOM. It goes through activeElement(ownerDocument(...)), as the repo guidelines require.
  • The as HTMLElement cast. TypeScript needs it for indexOf on Array<HTMLElement | null>.

Remaining risk:

  • An autoFocus item that mounts later relies on the item's onFocus reporting -1, which clears highlightedElementRef, before onMapChange runs.
  • The timing between the mount-effect focus and the passive-effect flush has only been checked by the jsdom/chromium runs recorded in the history, not by me.

Test audit

The Base UI test-audit files were all read: BASE-UI.md, SKILL.md, CAMPAIGN.md, references/audit.md, openclaw-testing.md, references/package-and-docker.md, writing-tests.md, boundary-scenarios.md and fixtures.md. writing-tests.md was only partly visible: I read it up to and including its "Flake triage" section, and the part after that was cut off in my output. As BASE-UI.md instructs, Base UI's AGENTS.md tooling replaced the OpenClaw commands, line budgets and CI.

No actionable test-audit findings.

Decision for each test (R = retain):

  • R — tab stop › keeps the tab stop on an item focused $name, row in a mount effect (CompositeRoot.test.tsx).
    • Contract: an item focused before it re-renders with its index keeps the single tab stop.
    • Regression it catches: removing the root onFocus list lookup leaves no tab stop.
    • Strength: Tab from "Before" must land on "Second", which also proves "First" is not a second tab stop.
  • R — same it.each, row with autoFocus.
    • Contract: the first map resolution prefers the focused element.
    • Regression it catches: dropping the focusedElement lookup in the first-resolution branch.
    • Shape: this is a separate path in the same contract, so the parameterized table is the right form and both rows use the parameter.
  • R — tab stop › moves the tab stop to an item that mounts later with autoFocus.
    • Contract: the later-resolution branch moves the tab stop to the focused item.
    • Starting state: it is asserted first ("First" has tabindex="0").
    • End state: both the new tab stop and the old item's tabindex="-1" are checked.
  • Owner placement: all three tests sit in the owner suite, CompositeRoot.test.tsx, as the previous round requested. ToolbarRoot.test.tsx is untouched in this diff.
  • Junk patterns: none. There are no mocks, no expected values generated by the code under test, no waitFor with negative assertions and no test-only seams. They use the user returned from render.

Verification limits:

  • I did not run the tests or break the source on purpose to see them fail. The recorded history says all three tab-stop tests fail against master's useCompositeRoot.ts, and the later-mount test fails on e51d98d8. I did not reproduce either claim.
  • Whether each it.each row fails when only its own code path is removed is inferred from reading the code, not run.

Verdict

Approve - no verified bugs, test gaps, simplifications or docs issues remain, and the previously reported issues are fixed in the current code.


🤖 Review generated with Claude Code

@jjenzz

jjenzz commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

Addressing #6360 (comment)

  • autoFocus item that mounts later: Fixed in 2be0156. When an item takes focus before it registers, its focus handler reports index -1, so the composite lost track of the tab stop. When the list resolves again, I now move the tab stop to the focused item if the previously highlighted one is gone. I added a regression test where the item mounts later.
  • Test placement: Fixed in the same commit. I moved both tab-stop rows from ToolbarRoot.test.tsx to CompositeRoot.test.tsx, since the behavior belongs to the shared composite root.

🤖 Comment posted by Claude Code

@jjenzz

jjenzz commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

PR review

No findings. Nothing here blocks the merge. The change touches the shared useCompositeRoot, which runs under Toolbar, Tabs, RadioGroup, ToggleGroup, Menubar and NavigationMenu. Each of its two fix paths is needed, and each has its own regression test. I didn't run any tests, per the instructions.

How I checked it:

  • Why both fix paths are needed.
    • Focused in a mount effect: CompositeList fills elementsRef in a layout effect before the mount effect calls focus(). When the focus lands, the item's own focus handler still holds a stale index of -1, so it reports -1. The root's onFocus handles the same event afterwards, finds the item in elementsRef, and sets the correct index (useCompositeRoot.ts:337-340).
    • autoFocus: React focuses the element during commit, before elementsRef is filled. The root's onFocus finds nothing there, so onMapChange picks up the focused element instead: on the first map change at :149, and on later ones at :127-128.
    • The tests: without the onFocus lookup, the mount-effect case would be left at index -1. Without the focused-element lookup in onMapChange, the autoFocus cases would be. That matches the earlier note that the tab-stop tests fail against the master version of useCompositeRoot.ts.
  • The earlier finding is fixed. An item that mounts later with autoFocus now gets the tab stop. Its focus handler resets the highlighted element to null (from elementsRef.current[-1]), so on the next map change the highlighted item reads as missing while the focused item is in the list.
  • The changed branch. The new nextIndex === -1 && focusedIndex !== -1 branch only wins when the highlighted element is gone and an item in the list has focus. In normal use the focused item is already the highlighted one, so the old replacement and fallback logic is unchanged for removals.
  • Callers. TabsList is the only caller that controls highlightedIndex. On focus it now gets its setHighlightedTabIndex call twice, the second time with the same or a corrected index. That state is internal, so users see no difference.
  • Repository rules.
    • The code uses the shadow-DOM-safe activeElement(ownerDocument(...)).
    • onMapChange runs only in a layout effect, so server rendering isn't affected.
    • The as HTMLElement cast is needed because activeElement returns Element.
  • Public API and docs. Nothing public changed, so the docs and pnpm docs:api aren't affected.

Remaining test gaps:

  • No test covers the controlled highlightedIndex path (Tabs).
  • No test covers a nested composite, where focus on an inner item must be ignored. From reading the code, findIndex returns -1 there and nothing changes.

Test audit

I read all nine bundle files through Kody: BASE-UI.md, SKILL.md, CAMPAIGN.md, references/audit.md, openclaw-testing.md, references/package-and-docker.md, writing-tests.md, boundary-scenarios.md and fixtures.md. BASE-UI.md replaces the OpenClaw-specific budgets, scripts and CI routing with the Base UI tooling.

No actionable test-audit findings.

  • Where the tests live: in CompositeRoot.test.tsx, next to the shared hook where the defect was. ToolbarRoot has no duplicate copy, so the earlier finding about test location stays fixed.
  • Authoring gate:
    1. Each test protects one visible contract: Tab from the button before the composite lands on the item that was focused programmatically.
    2. Each fails on a credible regression, one per fix path, as traced in the review above.
    3. No existing test covered focus taken before the item registers.
    4. No test-only exports or production seams were added.
  • Parametrized test: the it.each rows ($name) both use element, and each row exercises a different timing path: focus after elementsRef is filled, or before.
  • Starting state:
    • The Tab check first moves focus to "Before", so it can't pass just because the item kept focus.
    • The later-mount test checks that "First" holds tabindex="0" before setProps, then checks that "First" drops to -1.
  • Assertions: they check real DOM focus and tabindex, not mock calls or values the code under test computed. Titles match what the bodies assert, and nothing redundant needs removing.
  • Limits: I didn't run the tests or mutate the source to watch them fail. The failure claims come from reading the code, plus the earlier note that the tests fail against the master version of useCompositeRoot.ts. CI on 1beb2de5 decides the rest.

Verdict

Approve - the fix sits in the shared composite hook where the bug was, both timing paths have their own failing-before test, and no regressions turned up.


🤖 Review generated with Claude Code

@jjenzz
jjenzz marked this pull request as ready for review November 27, 2026 13:49

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: composite component: toolbar Changes related to the toolbar component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[toolbar] An item focused on mount leaves the toolbar without a tab stop

2 participants