Skip to content

[tabs] Fix state edge cases - #4935

Merged
atomiks merged 2 commits into
mui:masterfrom
atomiks:codex/tabs-state-edge-cases
May 29, 2026
Merged

atomiks merged 2 commits into
mui:masterfrom
atomiks:codex/tabs-state-edge-cases

Conversation

@atomiks

@atomiks atomiks commented May 28, 2026

Copy link
Copy Markdown
Contributor

Fixes a few Tabs state edge cases where fallback state could drift from the selected tab or omit state data from customized parts.

Changes

  • Preserve the initial uncontrolled defaultValue when deciding whether an explicitly selected disabled tab should stay selected.
  • Expose data-activation-direction and tabActivationDirection on Tabs.Tab.
  • Report null indicator position and size when the selected value has no matching tab.
  • Add regression coverage for these cases plus nearby keyboard and rendered-element behavior.

@atomiks atomiks added component: tabs Changes related to the tabs component. type: bug It doesn't behave as expected. labels May 28, 2026
@pkg-pr-new

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

commit: 1886f0d

@code-infra-dashboard

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

Copy link
Copy Markdown

Bundle size

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

Details of bundle changes

Performance

Total duration: 1,186.40 ms -43.34 ms(-3.5%) | Renders: 50 (+0) | Paint: 1,795.27 ms -88.06 ms(-4.7%)

No significant changes — details


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

@netlify

netlify Bot commented May 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 1886f0d
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a194d43f8ae9300082ab6cd
😎 Deploy Preview https://deploy-preview-4935--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 marked this pull request as ready for review May 28, 2026 12:51

@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 Review Summary: #4935 [tabs] Fix state edge cases

Critical Issues (0 found)

None. All three fixes are correct and ready to merge from a correctness perspective.

Important Issues (2 found)

1. JSDoc Wording Inconsistency

comment-analyzer + code-reviewer: packages/react/src/tabs/tab/TabsTab.tsx:256 and docs/src/app/(docs)/react/components/tabs/types.md:261 use "The direction of the activation." while every other tabActivationDirection doc in the codebase (e.g., TabsRoot.tsx:449) uses "The direction used for tab activation." — divergent JSDoc for the same underlying value across Root.State vs Tab.State is a maintainability hazard. Mirror the existing wording.

2. Misleading Snapshot-Ref Comment

comment-analyzer: packages/react/src/tabs/root/TabsRoot.tsx:45-47 — "defaultValue is read-once at mount" is factually inaccurate (the prop is destructured every render at line 32; only useControlled's state-init is once). Also doesn't mention that useControlled already warns on the scenario this guards. Suggested rewrite:

useControlled warns when defaultValue changes after mount, but the disabled-default-honor policy in the fallback effect below still needs a stable reference — snapshot the initial value so the policy can't shift post-mount even if a consumer ignores the warning.

Suggestions (nice to have)

Tests

  • pr-test-analyzer: Vertical-orientation data-activation-direction test at TabsRoot.test.tsx:1944-1978 was NOT extended like the horizontal one — only horizontal asserts per-tab attribute. A regression breaking vertical-only exposure would slip through.
  • pr-test-analyzer: No test confirms tabActivationDirection is reachable via <Tabs.Tab render={(_, state) => ...} />. Only the data-attribute is checked.
  • pr-test-analyzer: initialDefaultValueRef is not tested for the symmetric case — defaultValue=0 (enabled, selected), then changed to defaultValue=1 (where tab 1 is now disabled). The snapshot's value depends on both directions of the policy.
  • pr-test-analyzer: The new cancellation test (TabsRoot.test.tsx:650-677) passes the spec but is loose — 'none' reason fires for both click and focus activation, so the .some() assertion is too permissive.

Style

  • code-reviewer: TabsTab.tsx:196 places stateAttributesMapping before props; sibling parts (TabsRoot.tsx:362, TabsPanel.tsx:89, TabsIndicator.tsx:136) put it last. Cosmetic.
  • code-reviewer: TabsIndicator.test.tsx:46-48's waitFor(() => length > 1) only succeeds due to Strict Mode double-rendering. Would be more robust to assert on indicatorStates[0] directly.
  • comment-analyzer: Snapshot-ref comment at TabsRoot.tsx:45-47 lives 200+ lines from its use site at :303,:310. Consider co-locating with shouldHonorDisabledDefaultValueRef at line 255 where the policy is actually defined.

Strengths

  • The initialDefaultValueRef snapshot correctly mirrors useControlled's read-once-at-mount semantics; safe under Strict Mode.
  • Removing defaultValueProp from effect deps is sound — all internal reads now go through the constant ref.
  • TabsIndicator fix is minimally invasive (one hoisted line) and downstream null-aware logic does the right thing.
  • TabsTab wiring is consistent with sibling components already using tabsStateAttributesMapping.
  • The "continues honoring an initially disabled explicit defaultValue" test smartly asserts both the useControlled dev warning and selection stability via toErrorDev.
  • The TabsIndicator test asserts the user-visible hidden attribute alongside the state values — exactly the regression vector.
  • The test comment at TabsRoot.test.tsx:674 is exemplary — explains why, not what.

Recommended Action

  1. Fix JSDoc wording on tabActivationDirection to match codebase convention (Important).
  2. Rewrite the snapshot-ref comment to remove the "read-once at mount" inaccuracy and add useControlled-warning context (Important).
  3. Extend the vertical-orientation data-activation-direction test for Tabs.Tab attribute symmetry (Suggestion).
  4. Consider adding the symmetric initialDefaultValueRef test case (Suggestion).

Verification baseline: tests pass, ESLint clean, TypeScript clean, pnpm docs:api no drift.

@atomiks
atomiks force-pushed the codex/tabs-state-edge-cases branch from 3f9c717 to 9b270d2 Compare May 29, 2026 08:23
@atomiks
atomiks merged commit b3e1405 into mui:master May 29, 2026
6 checks passed
@atomiks
atomiks deleted the codex/tabs-state-edge-cases branch May 29, 2026 08:31
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.

2 participants