Home

Awesome

React Flip Move

build status npm version npm monthly downloads

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.

Read more about how it works

demo

Current Status

React Flip Move is looking for maintainers!

In the meantime, we'll do our best to make sure React Flip Move continues to work with new versions of React, but otherwise it isn't being actively worked on.

Because it isn't under active development, you may be interested in checking out projects like react-flip-toolkit.

Demos

Installation

Flip Move can be installed with NPM or Yarn.

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 <script> tags:

<html>
  <body>
    <script src="https://unpkg.com/react-flip-move/dist/react-flip-move.js"></script>
    <script>
      // Will be available under the global 'FlipMove'.
    </script>
  </body>
</html>

Features

Flip Move was inspired by Ryan Florence's awesome <a href="https://github.com/ryanflorence/react-magic-move" target="_blank">Magic Move</a>, and offers:

Quickstart

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 = ({ articles }) => (
  {articles.map(article => (
    <Article key={article.id} {...article} />
  ))}
);

/**
 * AFTER:
 */
import FlipMove from 'react-flip-move';

const TopArticles = ({ articles }) => (
  <FlipMove>
    {articles.map(article => (
      <Article key={article.id} {...article} />
    ))}
  </FlipMove>
);

There are a number of options you can provide to customize Flip Move. There are also some gotchas to be aware of.

Usage with Functional Components

Functional components do not have a ref, which is needed by Flip Move to work. To make it work you need to wrap your functional component into React.forwardRef and pass it down to the first element which accepts refs, such as DOM elements or class components:

import React, { forwardRef } from 'react';
import FlipMove from 'react-flip-move';

const FunctionalArticle = forwardRef((props, ref) => (
  <div ref={ref}>
    {props.articleName}
  </div>
));

// you do not have to modify the parent component
// this will stay as described in the quickstart
const TopArticles = ({ articles }) => (
  <FlipMove>
    {articles.map(article => (
      <FunctionalArticle key={article.id} {...article} />
    ))}
  </FlipMove>
);

API Reference

View the full API reference documentation

Enter/Leave Animations

View the enter/leave docs

Compatibility

ChromeFirefoxSafariIEEdgeiOS Safari/ChromeAndroid Chrome
Supported✔ 10+✔ 4+✔ 6.1+✔ 10+✔ 6.1+

How It Works

Curious how this works, under the hood? Read the Medium post.


Wrapping Element

By default, Flip Move wraps the children you pass it in a <div>:

// JSX
<FlipMove>
  <div key="a">Hello</div>
  <div key="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 className="flip-wrapper" style={{ color: 'red' }}>
  <div key="a">Hello</div>
  <div key="b">World</div>
</FlipMove>

// HTML
<div class="flip-wrapper" style="color: red;">
  <div key="a">Hello</div>
  <div key="b">World</div>
</div>

You can supply a different element type with the typeName prop:

// JSX
<FlipMove typeName="ul">
  <li key="a">Hello</li>
  <li key="b">World</li>
</FlipMove>

// HTML
<ul>
  <li key="a">Hello</li>
  <li key="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 className="your-own-element">
  <FlipMove typeName={null}>
    <div key="a">Hello</div>
    <div key="b">World</div>
  </FlipMove>
</div>

// HTML
<div class="your-own-element">
  <div key="a">Hello</div>
  <div key="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:

// BAD - this will cause children to jump to a new position before exiting:
<div style={{ padding: 20 }}>
  <FlipMove typeName={null}>
    <div key="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 style={{ position: 'relative' }}>
  <FlipMove typeName={null}>
    <div key="a">Hello world</div>
  </FlipMove>
</div>

Gotchas

Known Issues

Note on will-change

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.

Applying 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

Contributions

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 npm install.

Development

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.

Flow support

Flip Move's sources are type-checked with Flow. If your project uses it too, you may want to install typings for our public API from flow-typed repo.

npm install --global flow-typed # if not already
flow-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 .flowconfig file:

[ignore]
.*/node_modules/react-flip-move/.*

License

MIT