React Flip Move
This module was built to tackle the common but arduous problem of animating a list of items when the list's order changes.
CSS transitions only work for CSS properties. If your list is shuffled, the items have rearranged themselves, but without the use of CSS. The DOM nodes don't know that their on-screen location has changed; from their perspective, they've been removed and inserted elsewhere in the document.
Flip Move uses the FLIP technique to work out what such a transition would look like, and fakes it using 60+ FPS hardware-accelerated CSS transforms.
yarn add react-flip-move# Or, if not using yarn:npm i -S react-flip-move
A UMD build is made available for those not using JS package managers:
To use a UMD build, you can use
Flip Move was inspired by Ryan Florence's awesome Magic Move, and offers:
Exclusive use of hardware-accelerated CSS properties (
transform: translate) instead of positioning properties (
left). Read why this matters.
Full support for enter/exit animations, including some spiffy presets, that all leverage hardware-accelerated CSS properties.
Ability to 'humanize' transitions by staggering the delay and/or duration of subsequent elements.
Ability to provide
Compatible with Preact (should work with other React-like libraries as well).
Tiny! Gzipped size is <5kb! ⚡
Flip Move aims to be a "plug and play" solution, without needing a lot of tinkering. In the ideal case, you can wrap the children you already have with
<FlipMove>, and get animation for free:
/*** BEFORE:*/const TopArticles = articlesarticles;/*** AFTER:*/import FlipMove from 'react-flip-move';const TopArticles = articles<FlipMove>articles</FlipMove>;
View the full API reference documentation
View the enter/leave docs
|Chrome||Firefox||Safari||IE||Edge||iOS Safari/Chrome||Android Chrome|
|Supported||✔ 10+||✔ 4+||✔ 6.1+||✔ 10+||✔||✔ 6.1+||✔|
How It Works
Curious how this works, under the hood? Read the Medium post.
By default, Flip Move wraps the children you pass it in a
// JSX<FlipMove><div ="a">Hello</div><div ="b">World</div></FlipMove>// HTML<div><div>Hello</div><div>World</div></div>
Any unrecognized props to
<FlipMove> will be delegated to this wrapper element:
// JSX<FlipMove ="flip-wrapper" =><div ="a">Hello</div><div ="b">World</div></FlipMove>// HTML<div ="flip-wrapper" ="color: red;"><div ="a">Hello</div><div ="b">World</div></div>
You can supply a different element type with the
// JSX<FlipMove ="ul"><li ="a">Hello</li><li ="b">World</li></FlipMove>// HTML<ul><li ="a">Hello</li><li ="b">World</li></ul>
Finally, if you're using React 16 or higher, and Flip Move 2.10 or higher, you can use the new "wrapperless" mode. This takes advantage of a React Fiber feature, which allows us to omit this wrapping element:
// JSX<div ="your-own-element"><FlipMove =><div ="a">Hello</div><div ="b">World</div></FlipMove></div>// HTML<div ="your-own-element"><div ="a">Hello</div><div ="b">World</div></div>
Wrapperless mode is nice, because it makes Flip Move more "invisible", and makes it easier to integrate with parent-child CSS properties like flexbox. However, there are some things to note:
- This is a new feature in FlipMove, and isn't as battle-tested as the traditional method. Please test thoroughly before using in production, and report any bugs!
- Flip Move does some positioning magic for enter/exit animations - specifically, it temporarily applies
position: absoluteto its children. For this to work correctly, you'll need to make sure that
<FlipMove>is within a container that has a non-static position (eg.
position: relative), and no padding:
// BAD - this will cause children to jump to a new position before exiting:<div =><FlipMove =><div ="a">Hello world</div></FlipMove></div>// GOOD - a non-static position and a tight-fitting wrapper means children will// stay in place while exiting:<div =><FlipMove =><div ="a">Hello world</div></FlipMove></div>
Does not work with stateless functional components. This is because Flip Move uses refs to identify and apply styles to children, and stateless functional components cannot be given refs. Make sure the children you pass to
<FlipMove>are either native DOM elements (like
<div>), or class components.
All children need a unique
keyproperty. Even if Flip Move is only given a single child, it needs to have a unique
keyprop for Flip Move to track it.
Flip Move clones the direct children passed to it and overwrites the
refprop. As a result, you won't be able to set a
refon the top-most elements passed to FlipMove. To work around this limitation, you can wrap each child you pass to
Elements whose positions have not changed between states will not be animated. This means that no
onFinishcallbacks will be executed for those elements.
Sometimes you'll want to update or change an item without triggering a Flip Move animation. For example, with optimistic updating, you may render a temporary version before replacing it with the server-validated one. In this case, use the same
keyfor both versions, and Flip Move will treat them as the same item.
Interrupted enter/leave animations can be funky. This has gotten better recently thanks to our great contributors, but extremely fast adding/removing of items can cause weird visual glitches, or cause state to become inconsistent. Experiment with your usecase!
Existing transition/transform properties will be overridden. I am hoping to change this in a future version, but at present, Flip Move does not take into account existing
transformCSS properties on its direct children.
To fully benefit from hardware acceleration, each item being translated should have its own compositing layer. This can be accomplished with the CSS will-change property.
will-change too willy-nilly, though, can have an adverse effect on mobile browsers, so I have opted to not use it at all.
In my personal experimentations on modern versions of Chrome, Safari, Firefox and IE, this property offers little to no gain (in Chrome's timeline I saw a savings of ~0.5ms on a 24-item shuffle).
YMMV: Feel free to experiment with the property in your CSS. Flip Move will respect the wishes of your stylesheet :)
Further reading: CSS will-change Property
Contributors welcome! Please discuss new features with me ahead of time, and submit PRs for bug fixes with tests (Testing stack is Mocha/Chai/Sinon, tested in-browser by Karma).
There is a shared prepush hook which launches eslint, flow checks, and tests. It sets itself up automatically during
This project uses React Storybook in development. The developer experience is absolutely lovely, and it makes testing new features like enter/leave presets super straightforward.
After installing dependencies, launch the Storybook dev server with
npm run storybook.
This project adheres to the formatting established by airbnb's style guide. When contributing, you can make use of the autoformatter prettier to apply these rules by running the eslint script
npm run lint:fix. If there are conflicts, the linter triggered by the prepush hook will inform you of those as well. To check your code by hand, run
npm run lint.
npm install --global flow-typed # if not alreadyflow-typed install react-flip-move@<version>
If you're getting some flow errors coming from
node_modules/react-flip-move/src path, you should add this to your