Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React css transition does not work correctly

I've written a React app, using CSS transitions. But those transitions does not work correctly in some of the components. In my app, only the components who are moving upwards works well, those who are moving downwards moves instantly without animation. (I want them both moves with animation.)

Here is the CSS I used there:

div.canvas {     position: absolute;     top: 90px;     left: 60px;     width: 640px;     height: 480px;     border: 1px solid #999;     background: white;  }  div.canvas-rect {     position: relative;     margin-left: 20px;     margin-top: 10px;     height: 20px;     background: green;      transition: all 1s linear;     -moz-transition: all 1s linear; /* Firefox 4 */     -webkit-transition: all 1s linear; /* Safari 和 Chrome */     -o-transition: all 1s linear; /* Opera */  } 

UPDATED:

I also built a codepen.io project to show the problem. It has the complete code of this demo project.

I've tried to add a log entry to componentDidUpdate, componentDidMount and componentWillUnmount methods to show whether these component are re-created or updated, it shows that they are all updated (not re-created, or removed) every second.

like image 321
cmal Avatar asked Aug 14 '17 09:08

cmal


1 Answers

Well, after I started a bounty because I also have this problem I finally found what seems to be the problem.

When you are using absolute position (or relative, as in your case), if you re-render the whole list every time, React will re-order the elements in the DOM (as you said, the elements are not being recreated, just updated). But this creates the problem with the transitions... apparently, if you move an element while the transition is running then you end up cutting the animation.

So, for cases in which you want to use position absolute, the key concept is to render the containers of your elements once (in this case, just divs) and only change the inner contents based on the new order. If you need to add more elements, just add them at the end.

I modified your codepen so that it reflects what I am saying. My example is very dumb because I just created 4 ad-hoc divs, but it illustrates the idea: create as many containers as you need, but DO NOT use a map that recreates them every time, or your transitions will be cut.

https://codepen.io/damianmr/pen/boEmmy?editors=0110

const ArrList = ({   arr }) => {   return (     <div style={{position: 'relative'}}>       <div className={`element element-${arr[0]} index-${arr[0]}`}>{arr[0]}</div>       <div className={`element element-${arr[1]} index-${arr[1]}`}>{arr[1]}</div>         <div className={`element element-${arr[2]} index-${arr[2]}`}>{arr[2]}</div>         <div className={`element element-${arr[3]} index-${arr[3]}`}>{arr[3]}</div>       </div>   ); } 

So, the problem is basically how you create a static list of containers and how you iterate through that list so that the first container renders the first element of your data, the second container the second element, etc.

Hope that it helps, this problem was driving me crazy too! :)

like image 68
damianmr Avatar answered Sep 17 '22 03:09

damianmr