fix(accordion): prevent controlled→uncontrolled flip in single collapsible Accordion#3949
Open
chatman-media wants to merge 1 commit into
Open
Conversation
…sible Accordion When `<Accordion.Root type="single" collapsible>` is used with a controlled `value` prop set to `undefined` (representing "nothing open"), the component would fall into uncontrolled mode because `useControllableState` treats any `undefined` prop as uncontrolled. On collapse the parent often sets state back to `undefined`, which caused `useControllableState` to reuse its stale internal value (the previously opened item), making the item reappear open. Fix: detect whether the `value` prop was explicitly provided via `'value' in props` and, when it was, normalize `undefined` to `''` before passing to `useControllableState`. This keeps the component in controlled mode for the full lifetime of the mount, matching user expectations. Closes radix-ui#3478
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
When
<Accordion.Root type="single" collapsible>is used in controlled mode withvalueset toundefined(the natural way to represent "nothing open" in TypeScript), the component malfunctions:onValueChange('item-1')firesonValueChange('')fires; if the handler storesundefined(setValue(v || undefined)), the next render seesvalue={undefined}useControllableStatetreatsprop = undefinedas uncontrolled, so it falls back to its stale internal state ('item-1') — the item visually re-opensThis produces the "3 clicks to close" symptom reported in #3478.
Root cause
useControllableStateusesprop !== undefinedto decide whether the component is controlled. When a user passesvalue={undefined}(meaning "no item open"), the component silently becomes uncontrolled and the stale uncontrolled state resurrects the last-open item.Fix
In
AccordionImplSingle, detect whether thevalueprop was explicitly provided via'value' in props. When it was, normalizeundefined → ''before handing it touseControllableState. This keeps the component in controlled mode for its entire lifetime regardless of what the parent stores.The change is backward-compatible:
<Accordion>with novalueprop):'value' in propsisfalse→ behaviour unchangedvalue="item-1"):isValueControlledistrue,valueProp ?? ''is'item-1'→ behaviour unchangedvalue={undefined}): normalized to''→ stays controlled, empty value ✓Test evidence
A new test
given a controlled single collapsible Accordioncovers the exact failure scenario. It fails without the fix and passes after:Full test suite: 32 test files, 350 tests — all pass.
Closes #3478