Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate element height in React with ReactCSSTransitionGroup?

I'm trying to animate element height with ReactCSSTransitionGroup, so this is what I would want the animation looks like:

http://jsfiddle.net/cherrry/hgk4Lme9/

The problem is that I don't always know the element height, so I tried to hack the scrollHeight, clientHeight or something similar during componentDidMount and try to set node.style.height or add rules to stylesheet

http://jsfiddle.net/cherrry/dz8uod7u/

Leaving animation looks good, however when element enters, it flash a bit and the scaling animation looks strange

It should be because of asking node.scrollHeight caused the rendering occur immediately, so is there anyway to get the same information and inject css rules before animation start? Or should I think other way round?

I'm not very satisfied with the max-height solution, as the resulting animation speed will be very strange when max-height is not close to or smaller to height, and my components' height do vary a lot.

I could imagine the final solution could be a bit messy, but I think making it into a Mixin will be nice enough to reuse it anywhere

like image 303
Cherry Ng Avatar asked Mar 30 '15 01:03

Cherry Ng


People also ask

How do you animate height in CSS?

For animate the "height" of element with CSS Transitions you need use "max-height". If use the "height: auto", the effect not works. Is necessary some value for the CSS create a CSS animate, and you can use "max-height" with a great value for emulate this effect.


3 Answers

I had a same problem and ended up writing a standalone component for animating height.

You can see the demo here: https://stanko.github.io/react-animate-height/

It is much easier to use, and whole library is really small (~200 lines)

<AnimateHeight
  duration={ 500 }
  height={ 'auto' }
>
  <h1>Your content goes here</h1>
  <p>Put as many React or HTML components here.</p>
</AnimateHeight>

Sorry for the shameless self promotion, but I think it can save you a lot of time if you have more than one component to animate.

Cheers!

like image 109
Stanko Avatar answered Oct 20 '22 05:10

Stanko


After a bit more experiment, I've come up with a solution by using the low-level API ReactTransitionGroup instead of high-level ReactCSSTransitionGroup

Here's the JSFiddle with a working solution: http://jsfiddle.net/cherrry/0wgp34cr/

Before the animation, it's doing 3 things:

  1. get computed height, paddings and margins
  2. hide the element with display: none and add .anim-enter to set height to 0
  3. create css rule for .anim-enter-active

To start the animation, it's doing 2 things:

  1. unhide the element
  2. add .anim-enter-active to start the animation

Some numbers and class name in JSFiddle were hard-coded, but it should be easy enough to transform the "mixin" into a React class as a replacement of ReactCSSTransitionGroup

like image 39
Cherry Ng Avatar answered Oct 20 '22 06:10

Cherry Ng


A very easy and nice approach is by handling scaleY instead of height. The hover should set the attribute transform: scaleY(1) (originally => transform: scaleY(0) to hide it) and the animation should target transform attribute.

like image 41
Fran Goñi Avatar answered Oct 20 '22 05:10

Fran Goñi