Repository navigation
[navigation menu] Fix stale popup size on rapid trigger hover - #4646
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,392.29 ms +0.12 ms(0.0%) | Renders: 53 (+0) | Paint: 2,171.18 ms +10.57 ms(+0.5%)
...and 7 more. View full report Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
624c50a to
08f3be0
Compare
flaviendelangle
left a comment
There was a problem hiding this comment.
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)
-
React.useEffect->useIsoLayoutEffectwith early return + cancellation (line ~148)- When
isActiveItembecomesfalse,sizeFrame.cancel()andcancelAutoSizeReset()are called immediately. - When
isActiveItemistrue, the effect is a no-op (early return), which is correct — the active trigger should not cancel its own pending work. - Using
useIsoLayoutEffectensures cancellation happens synchronously before paint, preventing a single frame where the stale callback could fire. Good choice.
- When
-
Guard inside
sizeFrame.requestcallback (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:isActiveItemRefis defined viauseValueAsRef(isActiveItem)at line 126.
-
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.setPropertyand asserts that after the Solutions panel becomes visible, no700pxwidth is written after the500pxwrite. This directly tests the race condition. - Final assertion checks the popup width resets to
autoafter 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.
08f3be0 to
b9b8b66
Compare
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