Repository navigation
[tabs] Fix state edge cases - #4935
Conversation
commit: |
Bundle size
PerformanceTotal 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. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
flaviendelangle
left a comment
There was a problem hiding this comment.
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:
useControlledwarns whendefaultValuechanges 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-directiontest atTabsRoot.test.tsx:1944-1978was 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
tabActivationDirectionis reachable via<Tabs.Tab render={(_, state) => ...} />. Only the data-attribute is checked. - pr-test-analyzer:
initialDefaultValueRefis 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:196placesstateAttributesMappingbeforeprops; sibling parts (TabsRoot.tsx:362,TabsPanel.tsx:89,TabsIndicator.tsx:136) put it last. Cosmetic. - code-reviewer:
TabsIndicator.test.tsx:46-48'swaitFor(() => length > 1)only succeeds due to Strict Mode double-rendering. Would be more robust to assert onindicatorStates[0]directly. - comment-analyzer: Snapshot-ref comment at
TabsRoot.tsx:45-47lives 200+ lines from its use site at:303,:310. Consider co-locating withshouldHonorDisabledDefaultValueRefat line 255 where the policy is actually defined.
Strengths
- The
initialDefaultValueRefsnapshot correctly mirrorsuseControlled's read-once-at-mount semantics; safe under Strict Mode. - Removing
defaultValuePropfrom 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
useControlleddev warning and selection stability viatoErrorDev. - The TabsIndicator test asserts the user-visible
hiddenattribute alongside the state values — exactly the regression vector. - The test comment at
TabsRoot.test.tsx:674is exemplary — explains why, not what.
Recommended Action
- Fix JSDoc wording on
tabActivationDirectionto match codebase convention (Important). - Rewrite the snapshot-ref comment to remove the "read-once at mount" inaccuracy and add
useControlled-warning context (Important). - Extend the vertical-orientation
data-activation-directiontest for Tabs.Tab attribute symmetry (Suggestion). - Consider adding the symmetric
initialDefaultValueReftest case (Suggestion).
Verification baseline: tests pass, ESLint clean, TypeScript clean, pnpm docs:api no drift.
3f9c717 to
9b270d2
Compare
Fixes a few Tabs state edge cases where fallback state could drift from the selected tab or omit state data from customized parts.
Changes
defaultValuewhen deciding whether an explicitly selected disabled tab should stay selected.data-activation-directionandtabActivationDirectiononTabs.Tab.nullindicator position and size when the selected value has no matching tab.