Skip to content

[navigation menu] Fix stale popup size on rapid trigger hover - #4646

Merged
atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/navigation-menu-fix-stale-hover-size
Apr 20, 2026
Merged

atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/navigation-menu-fix-stale-hover-size

Conversation

@atomiks

@atomiks atomiks commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #4619

Rapid trigger hovers could leave a queued popup size update behind from the previously active trigger. That extra size pass could reapply the old panel dimensions after the next trigger had already measured a smaller popup.

Changes

  • Cancel pending popup size work when a trigger stops being the active item.
  • Ignore deferred popup size writes once the trigger that scheduled them is no longer active.
  • Add a Chromium regression test that rapidly switches between differently sized popups and asserts the old width is not written again.

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

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

commit: b9b8b66

@code-infra-dashboard

code-infra-dashboard Bot commented Apr 18, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+58B(+0.01%) 🔺+13B(+0.01%)

Details of bundle changes

Performance

Total duration: 1,392.29 ms +0.12 ms(0.0%) | Renders: 53 (+0) | Paint: 2,171.18 ms +10.57 ms(+0.5%)

Test Duration Renders
Checkbox mount (500 instances) 91.71 ms 🔺+19.09 ms(+26.3%) 1 (+0)
Tabs mount (200 instances) 268.82 ms 🔺+45.61 ms(+20.4%) 4 (+0)
Scroll Area mount (300 instances) 90.62 ms 🔺+7.94 ms(+9.6%) 3 (+0)
Dialog mount (300 instances) 95.75 ms 🔺+4.27 ms(+4.7%) 2 (+0)
Slider mount (300 instances) 148.27 ms 🔺+6.04 ms(+4.2%) 3 (+0)

...and 7 more. View full report

Details of benchmark changes


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

@netlify

netlify Bot commented Apr 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit b9b8b66
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69e5ea2b13ee7800089eea9b
😎 Deploy Preview https://deploy-preview-4646--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.

@atomiks
atomiks force-pushed the codex/navigation-menu-fix-stale-hover-size branch from 624c50a to 08f3be0 Compare April 20, 2026 08:00
@atomiks
atomiks marked this pull request as ready for review April 20, 2026 08:04

@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.

PR #4646 Review: [navigation menu] Fix stale popup size on rapid trigger hover

Author: atomiks
Branch: codex/navigation-menu-fix-stale-hover-size -> master
Fixes: #4619


Summary

When rapidly hovering between NavigationMenu triggers with differently-sized popups, a queued sizeFrame callback from the previous trigger could fire after the new trigger became active, reapplying the old (stale) popup dimensions. This PR cancels pending size work when a trigger is deactivated and guards the deferred write against stale execution.


Changes Analysis

Source change (NavigationMenuTrigger.tsx, +13 -1)

  1. React.useEffect -> useIsoLayoutEffect with early return + cancellation (line ~148)

    • When isActiveItem becomes false, sizeFrame.cancel() and cancelAutoSizeReset() are called immediately.
    • When isActiveItem is true, the effect is a no-op (early return), which is correct — the active trigger should not cancel its own pending work.
    • Using useIsoLayoutEffect ensures cancellation happens synchronously before paint, preventing a single frame where the stale callback could fire. Good choice.
  2. Guard inside sizeFrame.request callback (line ~253)

    • if (!isActiveItemRef.current) return; prevents a queued animation frame from writing dimensions if the trigger has since become inactive.
    • Uses a ref (isActiveItemRef) which is correct for reading current state inside an async callback. Double-checked: isActiveItemRef is defined via useValueAsRef(isActiveItem) at line 126.
  3. Whitespace-only change (line ~502) — blank line addition, harmless.

Test change (NavigationMenuTrigger.test.tsx, +92 -1)

  • Chromium-only regression test (it.skipIf(isJSDOM)) — appropriate since this requires real layout measurements.
  • Sets up two triggers with different panel sizes (700px vs 500px), hovers Product first, then switches to Solutions.
  • Spies on popupRoot.style.setProperty and asserts that after the Solutions panel becomes visible, no 700px width is written after the 500px write. This directly tests the race condition.
  • Final assertion checks the popup width resets to auto after animations finish, validating the full lifecycle.
  • Test fixture and helper (TestNavigationMenuRapidHoverSizing, getPopupWidthCalls) are clean and well-scoped.

Potential Concerns

Area Assessment
Race condition coverage The two-pronged fix (cancel on deactivation + guard on callback) provides belt-and-suspenders protection. Solid.
Effect type change Switching from useEffect to useIsoLayoutEffect is intentional and correct — cancellation must happen before paint. No SSR concern since the effect body only runs when isActiveItem is false (cleanup path).
Bundle size +31B parsed, +7B gzipped. Negligible.
Performance regressions CI benchmark shows some component mount regressions (Slider +16%, Select +10%) but these are unrelated to navigation menu and likely noise — the PR doesn't touch those components.
Missing cleanup The useIsoLayoutEffect doesn't return a cleanup function, but that's fine — sizeFrame cleanup is already handled in the unmount effect at line ~358.
Edge cases The guard uses isActiveItemRef.current which reads the latest value at callback time. If React batches multiple state updates, the ref will still reflect the final state correctly.

Verdict

This is a clean, minimal, and well-targeted bug fix. The root cause is clearly identified (queued animation frame outliving trigger activation), the fix addresses it at two levels (proactive cancellation + defensive guard), and the regression test is thorough and Chromium-only where appropriate. No API changes, no unnecessary refactoring.

Confidence Rating: 4.5 / 5

Deducting half a point only because the race condition is timing-sensitive and the test relies on setProperty spy ordering, which could theoretically be flaky under extreme CI load — but the fix itself is sound and low-risk. Safe to merge.

@atomiks
atomiks force-pushed the codex/navigation-menu-fix-stale-hover-size branch from 08f3be0 to b9b8b66 Compare April 20, 2026 08:56
@atomiks
atomiks merged commit fc7f4f9 into mui:master Apr 20, 2026
7 checks passed
@atomiks
atomiks deleted the codex/navigation-menu-fix-stale-hover-size branch April 20, 2026 10:10
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.

[navigation menu] Popup dimensions become stale when rapidly hovering between menu triggers

2 participants