Skip to content

[navigation menu] Fix reverse shift-tab submenu re-entry - #4464

Merged
atomiks merged 3 commits into
mui:masterfrom
atomiks:codex/navigation-menu-shift-tab-reentry
Mar 27, 2026
Merged

atomiks merged 3 commits into
mui:masterfrom
atomiks:codex/navigation-menu-shift-tab-reentry

Conversation

@atomiks

@atomiks atomiks commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #4463

This fixes reverse tab re-entry in Navigation Menu submenus so focus returns to the last submenu item instead of falling back to the trigger or disappearing in inline nested submenus.

Changes

  • Update the afterOutside focus guard to re-enter the submenu through the inside guard and synchronously clear inline viewport inert state before redirecting focus.
  • Add regression coverage for reverse tab re-entry in both portaled and inline nested submenu flows.

@atomiks atomiks added type: bug It doesn't behave as expected. component: navigation menu Changes related to the navigation menu component. accessibility a11y labels Mar 27, 2026 — with ChatGPT Codex Connector
@pkg-pr-new

pkg-pr-new Bot commented Mar 27, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4464
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4464
    

commit: 02aaf4c

@netlify

netlify Bot commented Mar 27, 2026

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 2798e54
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69c65188aeb7b80009fd566f
😎 Deploy Preview https://deploy-preview-4464--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@mui-bot

mui-bot commented Mar 27, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+1B(0.00%) 🔺+2B(0.00%)

Details of bundle changes


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

@atomiks
atomiks marked this pull request as ready for review March 27, 2026 09:51
@netlify

netlify Bot commented Mar 27, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 02aaf4c
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69c6726417d71c00084cf095
😎 Deploy Preview https://deploy-preview-4464--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@flaviendelangle flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

afterOutside focus guard fix

The root cause is that when focus leaves the viewport, the viewport's onBlur handler sets viewportInert = true, making the viewport inert on inline (non-portaled) menus. When the user Shift+Tabs back, isTabbable() returned false and focus fell back to the trigger.

The flushSync approach is the right call here — it ensures React commits the inert removal to the DOM before the .focus() call, which is necessary since we're inside an event handler that would otherwise batch the state update. The scope of the re-render is small (just the viewport's inert attribute), so the perf cost is negligible.

Minor observations

  1. Asymmetry with beforeOutside guard: The beforeOutside guard simply calls beforeInsideRef.current?.focus() without clearing inert state. I believe this works because Shift+Tabbing backward hits beforeOutside before leaving the viewport scope, so the viewport isn't inert yet. Worth confirming this reasoning holds in all cases — if the beforeOutside case can also encounter an inert viewport, it would need the same flushSync treatment.

  2. Fallback behavior change: The old code gated on isTabbable(afterInsideRef.current) before focusing it. The new code uses afterInsideRef.current || triggerElement. Since the flushSync ensures the viewport is no longer inert, afterInsideRef.current should always be focusable when it exists, so dropping the isTabbable gate is safe. But if afterInsideRef.current exists yet is somehow not focusable for another reason, focus would silently land on a non-tabbable element instead of falling back to the trigger. Seems unlikely but is a subtle behavioral change.

Verdict

Overall looks good to merge.

atomiks commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

Codex Review (GPT-5.4)

I reviewed the full PR diff against a freshly fetched upstream/master base. This is a narrow bug-fix change with secondary tags tests, a11y, and react-ui, focused on the NavigationMenuTrigger focus-guard change plus the new reverse-tabbing regression coverage.

1. Bugs / Issues (None)

I did not find any concrete correctness, accessibility, or regression issues in the current patch.

2. Root Cause & Patch Assessment

Area Assessment Notes
Portaled reverse-tab re-entry Handled Re-entering through afterInsideRef instead of gating on isTabbable(...) fixes the fallback-to-trigger behavior on the top-level path.
Inline reverse-tab re-entry Handled Clearing viewportInert synchronously before redirecting focus closes the inline nested regression where focus could disappear instead of landing back in the submenu.
Blast radius Low The runtime change is tightly scoped to the afterOutsideRef focus-guard path in packages/react/src/navigation-menu/trigger/NavigationMenuTrigger.tsx.

6. Test Coverage Assessment

The added tests meaningfully improve coverage for the boundary that was previously missing: reverse-tabbing after focus has already left the submenu. I also reran targeted validation on the current PR head with pnpm test:jsdom NavigationMenuRoot --no-watch, pnpm test:chromium NavigationMenuRoot --no-watch, and pnpm typescript, and the latest lint/prettier follow-up for the trigger file is also reflected in the branch.

Merge Confidence Score

Overall merge confidence is 4/5. The patch is small, the root cause looks correctly addressed for both portaled and inline submenu flows, and the targeted browser/jsdom/type validation is strong for this bug class. I would be comfortable merging this as-is.

@atomiks
atomiks merged commit 66c97c3 into mui:master Mar 27, 2026
7 checks passed
@atomiks
atomiks deleted the codex/navigation-menu-shift-tab-reentry branch August 11, 2026 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accessibility a11y component: navigation menu Changes related to the navigation menu component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[a11y] Navigation Menu: reverse tab (Shift+Tab) skips submenu items / goes to trigger

3 participants