Skip to content

[tabs] Fix activation direction on first render - #4532

Merged
atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/tabs-unflake-activation-direction-test
Apr 6, 2026
Merged

atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/tabs-unflake-activation-direction-test

Conversation

@atomiks

@atomiks atomiks commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

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.Root now 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

  • Replace the render-time ref-driven activation-direction bookkeeping in Tabs.Root with state-based previous-value tracking.
  • Keep the strict first-render activation-direction assertions for direct tab switches.
  • Settle frame-based panel transition work between the two click assertions so delayed renders from the first interaction do not leak into the second assertion.
  • Narrow the dynamic add-and-select numeric test to the first render of the newly selected panel.
  • Preserve the existing final-render assertion for the out-of-order string-value dynamic case.

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

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

commit: 68300a2

@mui-bot

mui-bot commented Apr 6, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+81B(+0.02%) 🔺+29B(+0.02%)

Details of bundle changes


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

@netlify

netlify Bot commented Apr 6, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 68300a2
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69d37332ec1747000803ed8a
😎 Deploy Preview https://deploy-preview-4532--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 marked this pull request as ready for review April 6, 2026 02:24
@atomiks atomiks removed the type: bug It doesn't behave as expected. label Apr 6, 2026

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

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.

@atomiks

atomiks commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

@LukasTy yes, randomly on various PRs, but only on React 18 in Chromium

@atomiks
atomiks force-pushed the codex/tabs-unflake-activation-direction-test branch from 7170b02 to 0eca620 Compare April 6, 2026 07:26

atomiks commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

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 Tabs.Root logic instead of weakening the tests. Tabs.Root now derives the effective activation direction from committed state plus the current value during render, then syncs that snapshot after commit, which avoids reading mutable refs in render and keeps the first render after a selection change in sync. I also tightened the inter-click test cleanup to wait for frame-based panel transition work before clearing mocks, and narrowed the dynamic add-and-select numeric test to the first render of the newly selected panel.

@atomiks atomiks changed the title [tabs] Unflake activation direction Chromium test [tabs] Fix activation direction on first render Apr 6, 2026
@atomiks atomiks added type: bug It doesn't behave as expected. and removed test labels Apr 6, 2026

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

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:

  1. Layout effect runs on every render where direction changed — The useIsoLayoutEffect at line 108 has [directionComputationIncomplete, tabActivationDirection, value] as deps. Since tabActivationDirection and directionComputationIncomplete are let variables recomputed each render, they will trigger the effect even when the state updater bails out (returns prev). This is fine for correctness (the updater is idempotent), but worth noting it's a no-op setState call on renders where nothing changed. The old ref-based approach avoided this. Acceptable trade-off for clarity though.

  2. onValueChange no longer sets direction explicitly — In the old code, the click path set directionJustComputedRef = true and called setTabActivationDirection. Now, it only calls setValue(newValue), relying on the next render's if (activationDirectionState.previousValue !== value) block to compute direction. This is correct because setValue triggers a re-render where the render-time computation runs. However, this means the direction is now computed twice on click: once in onValueChange (for eventDetails.activationDirection) and once during render. Minor redundancy, not a real issue.

  3. setActivationDirectionState in 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

  1. waitForAnimationFrame uses raw requestAnimationFrame — The project guidelines say to use useAnimationFrame from @base-ui/utils/useAnimationFrame instead of requestAnimationFrame. However, this is test code (not component code), so a raw requestAnimationFrame in a test helper is reasonable. Still, double-check this is acceptable per project conventions.

  2. waitForSettledPanelTransitions waits 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.

  3. 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. Adding value to the render mock payload to enable this is a clean solution.

Potential Issues

  1. Extra re-render in the click path — The old code did setValue + setTabActivationDirection in onValueChange, which React could batch. The new code does setValue only, then the layout effect calls setActivationDirectionState. 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.

  2. directionComputationIncomplete is a local variable captured by closure — At line 110, the layout effect captures directionComputationIncomplete from 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.

@atomiks
atomiks merged commit e456b4b into mui:master Apr 6, 2026
7 checks passed
@atomiks
atomiks deleted the codex/tabs-unflake-activation-direction-test branch April 6, 2026 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: tabs Changes related to the tabs component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants