Imran
Posted
Animate Children Height Dynamically with react-spring
Our goal
To animate a container that collapses its children smoothly.
Use cases:
Accordian component
What wont work
When:
animate with percentage based height value
apply scaleY to the animated div
positioned component
display "none"
Lets start with fixed height
FixedHeight component has a parent container that hides its child item of height: 200px.
When the children component height is unknown
Setting the height to percentage based value don't produce the result we look for since the browser will just ignore and default to the children's height value. This leads to the parent element stays static in size while the child element disappear due to the rest of animation properties took effect except for height.
So, our solution would simply by measuring the child element DOM size with browser DOMRect API.
What made it works
By measuring the child item DOM size in its parent element with Javascript. At the time of writing, I could not find simpler solution using CSS height values alone. If you do, please reach me out on twitter or join my livestream.
Discussion
Although above solution works, we do need to measure if the child element updates its size.
Our options:
browser native Resize Observer API
react-use-measurelibrary
