Repository navigation
[tabs] Fix activation direction on first render - #4532
Conversation
commit: |
Bundle size 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. |
LukasTy
left a comment
There was a problem hiding this comment.
Are the tests failing?
The existing tests are checking if the fundamental issue this PR solves is covered as it needs the first render to have up-to-date activationDirection.
|
@LukasTy yes, randomly on various PRs, but only on React 18 in Chromium |
7170b02 to
0eca620
Compare
|
Updated this PR after review feedback. The final change keeps the strict first-render activation-direction assertions for the direct tab-switch cases, but fixes the underlying |
LukasTy
left a comment
There was a problem hiding this comment.
LGTM, I hope this avoids the test flakiness. 🤞
Claude Opus 4.6 review
Overview
This PR refactors the activation direction tracking in TabsRoot from a multi-ref approach (previousValueRef, directionJustComputedRef, directionComputationIncompleteRef) to a single consolidated useState object. The test changes address Chromium flakiness by waiting for all panel transition renders to settle before asserting on subsequent clicks.
Code Quality — Source Changes
The core refactor in TabsRoot.tsx is well-motivated and clean. Collapsing three refs into one state object eliminates the tricky coordination between refs and render-time mutations. The new approach computes direction during render (via let variables) and syncs to state in a layout effect — a sound React pattern.
However, a few things to flag:
-
Layout effect runs on every render where direction changed — The
useIsoLayoutEffectat line 108 has[directionComputationIncomplete, tabActivationDirection, value]as deps. SincetabActivationDirectionanddirectionComputationIncompleteareletvariables recomputed each render, they will trigger the effect even when the state updater bails out (returnsprev). This is fine for correctness (the updater is idempotent), but worth noting it's a no-opsetStatecall on renders where nothing changed. The old ref-based approach avoided this. Acceptable trade-off for clarity though. -
onValueChangeno longer sets direction explicitly — In the old code, the click path setdirectionJustComputedRef = trueand calledsetTabActivationDirection. Now, it only callssetValue(newValue), relying on the next render'sif (activationDirectionState.previousValue !== value)block to compute direction. This is correct becausesetValuetriggers a re-render where the render-time computation runs. However, this means the direction is now computed twice on click: once inonValueChange(foreventDetails.activationDirection) and once during render. Minor redundancy, not a real issue. -
setActivationDirectionStatein the dependency array at line 124 — React state setters are stable, so listing it in deps is harmless but unnecessary. Same for the fallback effect's dep array at line ~270. Very minor nit.
Code Quality — Test Changes
-
waitForAnimationFrameuses rawrequestAnimationFrame— The project guidelines say to useuseAnimationFramefrom@base-ui/utils/useAnimationFrameinstead ofrequestAnimationFrame. However, this is test code (not component code), so a rawrequestAnimationFramein a test helper is reasonable. Still, double-check this is acceptable per project conventions. -
waitForSettledPanelTransitionswaits for 2 animation frames — The "two frames" pattern is a known technique but it's fragile if the implementation changes. A brief comment explaining why two frames are needed (e.g., "one frame for the layout effect setState, one for the resulting re-render") would help future maintainers. -
Dynamic tabs test assertion change is good — Switching from
nthCalledWith(1, ...)to.find(([state]) => state.value === 2)is more resilient. The old assertion assumed the newly-added panel's render call would be first, which is order-dependent. The new approach searches for the right call. Addingvalueto the render mock payload to enable this is a clean solution.
Potential Issues
-
Extra re-render in the click path — The old code did
setValue+setTabActivationDirectioninonValueChange, which React could batch. The new code doessetValueonly, then the layout effect callssetActivationDirectionState. This means a layout-effect-triggered state update → an additional synchronous re-render. For tabs this is negligible, but it's a behavioral change worth being aware of. -
directionComputationIncompleteis a local variable captured by closure — At line 110, the layout effect capturesdirectionComputationIncompletefrom the render scope. This works correctly because React runs layout effects synchronously after the render that created them, so the captured value is always fresh. Just noting this is correct but subtle.
Summary
The refactor simplifies tricky ref coordination into a more idiomatic React pattern. The test changes correctly address the flakiness by waiting for settled state rather than asserting on render call order. The trade-off is a potential extra re-render in the click path, which is acceptable for tabs.
Verdict: Looks good. Consider adding a brief comment on why two animation frames are needed in the test helper (point 5), and the rest is solid.
This fixes the activation-direction bug on the first render after a tab selection change and stabilizes the Chromium coverage that was catching it.
Tabs.Rootnow derives the effective activation direction from committed state plus the current value, then syncs that snapshot after commit, which keeps the first render aligned without relying on mutable ref reads in render.Changes
Tabs.Rootwith state-based previous-value tracking.