-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Collapsible] Add support for disabling the transition, and fix state machine #7364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
fe4e2a0
cdbcf0a
c06133c
f08519a
258bdf9
12ee027
0a49421
063b41f
4b341c5
237ea07
20017ed
5a0528f
82f3e5a
e14e13a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@shopify/polaris': minor | ||
| --- | ||
|
|
||
| Deprecated Collapsible preventMeasuringOnChildrenUpdate. | ||
| Fixed bug where Collapsible would get stuck in animating state. | ||
| Add support for intentionally disabling the transition in Collapsible. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,9 +18,11 @@ export interface CollapsibleProps { | |
| expandOnPrint?: boolean; | ||
| /** Toggle whether the collapsible is expanded or not. */ | ||
| open: boolean; | ||
| /** Assign transition properties to the collapsible */ | ||
| transition?: Transition; | ||
| /** Prevents component from re-measuring when child is updated **/ | ||
| /** Override transition properties. When set to false, disables transition completely. Defaults to true, which uses | ||
| * default transition properties. | ||
| */ | ||
| transition?: boolean | Transition; | ||
| /** @deprecated Re-measuring is no longer necessary on children update **/ | ||
| preventMeasuringOnChildrenUpdate?: boolean; | ||
| /** The content to display inside the collapsible. */ | ||
| children?: React.ReactNode; | ||
|
|
@@ -32,8 +34,8 @@ export function Collapsible({ | |
| id, | ||
| expandOnPrint, | ||
| open, | ||
| transition, | ||
| preventMeasuringOnChildrenUpdate, | ||
| transition = true, | ||
| preventMeasuringOnChildrenUpdate: _preventMeasuringOnChildrenUpdate, | ||
kyledurand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| children, | ||
| }: CollapsibleProps) { | ||
| const [height, setHeight] = useState(0); | ||
|
|
@@ -51,11 +53,14 @@ export function Collapsible({ | |
| expandOnPrint && styles.expandOnPrint, | ||
| ); | ||
|
|
||
| const transitionDisabled = transition === false; | ||
|
||
|
|
||
| const transitionStyles = typeof transition === 'object' && { | ||
| transitionDuration: transition.duration, | ||
| transitionTimingFunction: transition.timingFunction, | ||
| }; | ||
| const collapsibleStyles = { | ||
| ...(transition && { | ||
| transitionDuration: `${transition.duration}`, | ||
| transitionTimingFunction: `${transition.timingFunction}`, | ||
| }), | ||
| ...transitionStyles, | ||
| ...{ | ||
| maxHeight: isFullyOpen ? 'none' : `${height}px`, | ||
| overflow: isFullyOpen ? 'visible' : 'hidden', | ||
|
|
@@ -72,15 +77,27 @@ export function Collapsible({ | |
| [open], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (isFullyClosed || preventMeasuringOnChildrenUpdate) return; | ||
| setAnimationState('measuring'); | ||
| }, [children, isFullyClosed, preventMeasuringOnChildrenUpdate]); | ||
| const startAnimation = useCallback(() => { | ||
| if (transitionDisabled) { | ||
| setIsOpen(open); | ||
| setAnimationState('idle'); | ||
|
|
||
| if (open && collapsibleContainer.current) { | ||
| setHeight(collapsibleContainer.current.scrollHeight); | ||
| } else { | ||
| setHeight(0); | ||
| } | ||
| } else { | ||
| setAnimationState('measuring'); | ||
| } | ||
| }, [open, transitionDisabled]); | ||
|
|
||
| useEffect(() => { | ||
| if (open !== isOpen) { | ||
| setAnimationState('measuring'); | ||
| startAnimation(); | ||
| } | ||
| // startAnimation should only be fired if the open state changes. | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [open, isOpen]); | ||
kyledurand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| useEffect(() => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.