Skip to content

[navigation menu] Fix keepMounted content sizing - #4817

Merged
atomiks merged 3 commits into
mui:masterfrom
atomiks:codex/navigation-menu-keep-mounted-sizing
May 18, 2026
Merged

atomiks merged 3 commits into
mui:masterfrom
atomiks:codex/navigation-menu-keep-mounted-sizing

Conversation

@atomiks

@atomiks atomiks commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

<NavigationMenu.Content keepMounted> could transition the size of the popup on open incorrectly + fail to transition the popup dimensions when hovering over nested inline submenus.

Root cause

Kept-mounted content can stay in the DOM while inactive, so it must not keep or regain ownership of the shared content ref used for popup sizing.

Before: https://stackblitz.com/edit/dwjmyhms
After: https://stackblitz.com/edit/dwjmyhms-hwgbd7w8

<NavigationMenu.Portal keepMounted> was one workaround before. Though that has a bug with the arrow transitioning (separate issue to fix)

Changes

  • Keep the shared current content ref owned only by active content, and clear it only when that same content closes.
  • Observe hidden changes and defer the first nested-content opening measurement so the popup uses the final nested content size.
  • Add regression coverage for kept nested content opening, hidden changes, close/reopen, and mixed kept/unmounted siblings.

@pkg-pr-new

pkg-pr-new Bot commented May 13, 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@4817
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4817
    

commit: 6b84254

@code-infra-dashboard

code-infra-dashboard Bot commented May 13, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+36B(+0.01%) 🔺+24B(+0.02%)

Details of bundle changes

Performance

Total duration: 1,406.35 ms +92.14 ms(+7.0%) | Renders: 50 (+0) | Paint: 2,124.47 ms +139.31 ms(+7.0%)

Test Duration Renders
Tabs mount (200 instances) 283.09 ms 🔺+48.33 ms(+20.6%) 4 (+0)
Tooltip mount (300 contained roots) 63.99 ms 🔺+15.90 ms(+33.1%) 1 (+0)
Checkbox mount (500 instances) 75.97 ms ▼-25.91 ms(-25.4%) 1 (+0)

9 tests within noise — details


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

@atomiks atomiks added the type: bug It doesn't behave as expected. label May 13, 2026 — with ChatGPT Codex Connector
@netlify

netlify Bot commented May 13, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 6b84254
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a0ac46a16d66c00088bf624
😎 Deploy Preview https://deploy-preview-4817--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.

@atomiks atomiks added the component: navigation menu Changes related to the navigation menu component. label May 13, 2026 — with ChatGPT Codex Connector
@atomiks
atomiks force-pushed the codex/navigation-menu-keep-mounted-sizing branch from aa9420c to 5523947 Compare May 13, 2026 08:43
@atomiks
atomiks force-pushed the codex/navigation-menu-keep-mounted-sizing branch from 5523947 to 245b179 Compare May 13, 2026 09:00
@atomiks
atomiks marked this pull request as ready for review May 13, 2026 09:26
@atomiks

atomiks commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

Code Review (GPT-5.5)

Approve ✅: the keepMounted sizing bug is fixed at the shared-content-ref ownership point, the hidden-attribute sizing path is covered, and the earlier ref-stability nit is resolved.

1. Bugs / Issues (None)

Root Cause & Patch Assessment

Reviewed as a bug-fix with secondary tests / react-ui scope against freshly fetched upstream/master (merge-base 646f5803). The patch addresses the root cause rather than only the visible animation symptom: inactive kept-mounted NavigationMenu.Content nodes can now mount in the viewport without taking ownership of currentContentRef, while active content still re-asserts ownership through the existing layout effect when the same DOM node re-enters.

The follow-up change to use useStableCallback keeps handleCurrentContentRef from changing identity on every open flip, so it preserves latest-state reads without churning the merged ref passed through CompositeRoot / useMergedRefsN.

The Trigger-side MutationObserver expansion is also targeted: kept nested submenu switches toggle hidden instead of inserting or removing nodes, and observing attributeFilter: ['hidden'] makes those visibility changes feed the existing sizing sync path without watching unrelated attributes.

Test Coverage Assessment

The regression coverage is proportional to the bug. The new tests cover kept nested default content during first portal entry, switching kept inline nested content, direct hidden attribute mutation, and the existing top-level keepMounted trigger-switch path remains covered.

Validation performed:

  • pnpm test:jsdom NavigationMenu --no-watch
  • pnpm test:chromium NavigationMenu --no-watch
  • pnpm exec eslint packages/react/src/navigation-menu/content/NavigationMenuContent.tsx --max-warnings 0
  • pnpm exec prettier --check packages/react/src/navigation-menu/content/NavigationMenuContent.tsx

I also verified the new kept-content regression expectations against the refreshed base implementation before the follow-up commit; the targeted cases failed there as expected, which confirms they gate the bug being fixed.

@atomiks
atomiks merged commit 8170336 into mui:master May 18, 2026
6 checks passed
@atomiks
atomiks deleted the codex/navigation-menu-keep-mounted-sizing branch May 18, 2026 08:01
mbrookes pushed a commit to mbrookes/base-ui that referenced this pull request May 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

1 participant