Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a react.js-friendly way to animate a list-reordering?

I have a list of items with scores, ordered by scores, rendered by react.js as a vertically-oriented list of rectangular items (highest scores at top). Hovers and other clicks on the individual items may show/hide extra info, changing their vertical height.

New information arrives that changes the scores, slightly, making some items rank higher and others lower after a re-sort. I'd like the items to simultaneously animate to their new positions, rather than appear in their new locations instantly.

Is there a recommended way to do this in React.js, perhaps with a popular add-on?

(In a similar past situation using D3, a technique I've used was roughly:

  1. Display the list with item DOM nodes in their natural order, with relative positioning. With relative positioning, other small changes – CSS or JS-triggered – to individual items' extent shift others as expected.
  2. Mutate all the DOM nodes, in a single step, to have their actual relative-coordinates as new absolute coordinates – a DOM change that causes no visual change.
  3. Re-order the item DOM nodes, within their parent, to the new sort order – another DOM change that causes no visual change.
  4. Animate all nodes' top-offsets to their new calculated values, based on the heights of all preceding items in the new ordering. This is the only visually-active step.
  5. Mutate all item DOM nodes back to non-offset relative-positioning. Again, this causes no visual change, but the now-relatively-positioned DOM nodes, in the natural ordering of the underlying list, will handle internal hover/expand/etc style changes with proper shifting.

Now I'm hoping for a similar effect in a React-ish way...)

like image 453
gojomo Avatar asked Nov 29 '14 03:11

gojomo


People also ask

Which react tool is used for animation?

React Spring is a modern animation library that is based on spring physics. It's highly flexible and covers most animations needed for a user interface.

What is react spring?

React spring is an animation library that makes animating UI elements simple. It is based on spring physics which helps it to achieve a natural look and feel. It is different from other animation libraries where someone has to deal with curves, easing, time durations, all of which in sync with each other.


2 Answers

I just released a module to tackle exactly this problem

https://github.com/joshwcomeau/react-flip-move

It does a few things differently than Magic Move / Shuffle:

  1. It uses the FLIP technique for hardware-accelerated 60FPS+ animations
  2. It offers options to "humanize" the shuffle by incrementally offsetting delay or duration
  3. It handles interruptions gracefully, no weird glitch effects
  4. Bunch of other neat stuff like start/finish callbacks

Check out the demos:

http://joshwcomeau.github.io/react-flip-move/examples/#/shuffle

like image 58
Joshua Comeau Avatar answered Oct 16 '22 18:10

Joshua Comeau


React Shuffle is solid and up to date. It's inspired by Ryan Florences Magic Move demo

https://github.com/FormidableLabs/react-shuffle

like image 9
Tim Arney Avatar answered Oct 16 '22 16:10

Tim Arney