react-flip-move
Effortless animation between DOM changes (eg. list reordering) using the FLIP technique.
Top Related Projects
Open source, production-ready animation and gesture library for React
An easy way to perform animations when a React component enters or leaves the DOM
A spring that solves your animation problems.
✌️ A spring physics based React animation library
🇨🇭 A React renderer for Three.js
🎊 A collection of animations for inline style libraries
Quick Overview
React Flip Move is a lightweight animation library for React that provides smooth, natural-looking transitions when elements are added, removed, or reordered in a list. It uses the FLIP (First, Last, Invert, Play) animation technique to achieve high-performance animations with minimal configuration.
Pros
- Easy to implement with minimal setup required
- Highly customizable with various animation options and callbacks
- Performant, using CSS transforms for smooth animations
- Works well with dynamic lists and grid layouts
Cons
- Limited to list-based animations; not suitable for complex, multi-step animations
- Requires wrapping child components, which may affect existing component structure
- May have compatibility issues with certain React patterns (e.g., render props)
- Documentation could be more comprehensive for advanced use cases
Code Examples
- Basic usage with a list of items:
import FlipMove from 'react-flip-move';
function List({ items }) {
return (
<FlipMove>
{items.map(item => (
<div key={item.id}>{item.name}</div>
))}
</FlipMove>
);
}
- Custom animation settings:
<FlipMove
staggerDurationBy={30}
duration={500}
easing="ease-out"
>
{/* List items */}
</FlipMove>
- Using enter/leave animations:
<FlipMove
enterAnimation="fade"
leaveAnimation="accordionVertical"
>
{/* List items */}
</FlipMove>
- Applying custom styles to the wrapper:
<FlipMove
typeName="ul"
className="my-list"
style={{ padding: 0 }}
>
{/* List items */}
</FlipMove>
Getting Started
-
Install the package:
npm install react-flip-move
-
Import and use in your React component:
import React from 'react'; import FlipMove from 'react-flip-move'; function MyList({ items }) { return ( <FlipMove> {items.map(item => ( <div key={item.id}>{item.content}</div> ))} </FlipMove> ); } export default MyList;
-
Customize animations as needed using props like
duration
,easing
, andenterAnimation
/leaveAnimation
.
Competitor Comparisons
Open source, production-ready animation and gesture library for React
Pros of Motion
- More comprehensive animation library with support for gestures, variants, and complex animations
- Better performance optimization with GPU acceleration and automatic batching
- Larger community and more frequent updates
Cons of Motion
- Steeper learning curve due to more features and complexity
- Larger bundle size, which may impact load times for smaller projects
- Potentially overkill for simple list animations
Code Comparison
React Flip Move:
import FlipMove from 'react-flip-move';
<FlipMove>
{items.map(item => (
<div key={item.id}>{item.content}</div>
))}
</FlipMove>
Motion:
import { motion } from 'framer-motion';
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
{items.map(item => (
<motion.div key={item.id} layout>{item.content}</motion.div>
))}
</motion.div>
Summary
Motion offers a more feature-rich animation solution with better performance, while React Flip Move is simpler and more focused on list animations. Motion's extensive capabilities come at the cost of increased complexity and bundle size. For basic list animations, React Flip Move may be sufficient, but for more complex projects, Motion's versatility could be advantageous.
An easy way to perform animations when a React component enters or leaves the DOM
Pros of react-transition-group
- More flexible and general-purpose, suitable for a wide range of transition effects
- Officially maintained by the React team, ensuring long-term support and compatibility
- Provides fine-grained control over enter, exit, and appear transitions
Cons of react-transition-group
- Requires more manual setup and configuration compared to react-flip-move
- May have a steeper learning curve for beginners
- Less optimized for list animations specifically
Code Comparison
react-transition-group:
<CSSTransition
in={inProp}
timeout={200}
classNames="fade"
unmountOnExit
>
<div>Transitioning content</div>
</CSSTransition>
react-flip-move:
<FlipMove enterAnimation="fade" leaveAnimation="fade">
{items.map(item => (
<div key={item.id}>{item.content}</div>
))}
</FlipMove>
react-transition-group offers more granular control over transitions but requires more setup, while react-flip-move provides a simpler API specifically tailored for list animations. The choice between the two depends on the specific requirements of your project and the complexity of the animations you need to implement.
A spring that solves your animation problems.
Pros of react-motion
- More flexible and powerful for complex animations
- Provides physics-based animations for more natural movement
- Supports spring animations and custom easing functions
Cons of react-motion
- Steeper learning curve and more complex API
- Requires more code to set up basic animations
- May have a slight performance overhead for simple transitions
Code Comparison
react-motion:
<Motion defaultStyle={{x: 0}} style={{x: spring(10)}}>
{({x}) => <div style={{transform: `translateX(${x}px)`}} />}
</Motion>
react-flip-move:
<FlipMove>
{items.map(item => (
<div key={item.id}>{item.content}</div>
))}
</FlipMove>
react-motion offers more control over animation behavior but requires more setup, while react-flip-move provides simpler implementation for list animations with less customization options. react-motion is better suited for complex, physics-based animations, whereas react-flip-move excels in straightforward list transitions with minimal configuration.
✌️ A spring physics based React animation library
Pros of react-spring
- More versatile, offering a wide range of animation types beyond list reordering
- Provides a physics-based animation system for more natural-looking transitions
- Supports complex, multi-stage animations and chained effects
Cons of react-spring
- Steeper learning curve due to its more comprehensive API
- May be overkill for simple list animations, where react-flip-move excels
- Requires more setup and configuration for basic use cases
Code Comparison
react-flip-move:
<FlipMove>
{items.map(item => (
<div key={item.id}>{item.content}</div>
))}
</FlipMove>
react-spring:
const transitions = useTransition(items, {
from: { opacity: 0, transform: 'translate3d(0,-40px,0)' },
enter: { opacity: 1, transform: 'translate3d(0,0px,0)' },
leave: { opacity: 0, transform: 'translate3d(0,-40px,0)' },
})
return transitions((style, item) => (
<animated.div style={style}>{item.content}</animated.div>
))
react-spring offers more flexibility but requires more code for similar functionality. react-flip-move provides a simpler API for list animations but is less versatile for other animation types.
🇨🇭 A React renderer for Three.js
Pros of react-three-fiber
- Provides a powerful 3D rendering engine for React applications
- Offers a declarative approach to creating complex 3D scenes
- Integrates seamlessly with the React ecosystem and component lifecycle
Cons of react-three-fiber
- Steeper learning curve due to 3D graphics concepts
- Higher performance overhead compared to 2D animations
- May be overkill for simple UI transitions
Code Comparison
react-flip-move:
<FlipMove>
{items.map(item => (
<div key={item.id}>{item.content}</div>
))}
</FlipMove>
react-three-fiber:
<Canvas>
<mesh>
<boxBufferGeometry args={[1, 1, 1]} />
<meshStandardMaterial color="hotpink" />
</mesh>
</Canvas>
While react-flip-move focuses on smooth transitions for list items, react-three-fiber enables the creation of complex 3D scenes within React applications. react-flip-move is more suitable for simple UI animations, whereas react-three-fiber is designed for building immersive 3D experiences. The code comparison illustrates the difference in complexity and purpose between the two libraries.
🎊 A collection of animations for inline style libraries
Pros of react-animations
- Offers a wide range of pre-defined animations, making it easier to implement various effects quickly
- Integrates well with CSS-in-JS libraries like styled-components or Emotion
- Provides more flexibility in terms of animation customization
Cons of react-animations
- Requires additional setup and configuration compared to react-flip-move
- May have a steeper learning curve for developers new to CSS animations
- Performance might be slightly lower for complex animations compared to react-flip-move's optimized approach
Code Comparison
react-animations:
import { fadeIn } from 'react-animations';
import styled, { keyframes } from 'styled-components';
const fadeInAnimation = keyframes`${fadeIn}`;
const FadeInDiv = styled.div`animation: 1s ${fadeInAnimation};`;
react-flip-move:
import FlipMove from 'react-flip-move';
<FlipMove enterAnimation="fade" leaveAnimation="fade">
{items.map(item => (
<div key={item.id}>{item.content}</div>
))}
</FlipMove>
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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.
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 Magic Move, and offers:
-
Exclusive use of hardware-accelerated CSS properties (
transform: translate
) instead of positioning properties (top
,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
onStart
/onFinish
callbacks. -
Compatible with Preact (should work with other React-like libraries as well).
-
Tiny! Gzipped size is <5kb! â¡
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
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.
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:
- 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: absolute
to 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 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
-
Does not work with stateless functional components without a React.forwardRef, read more about here. 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
key
property. Even if Flip Move is only given a single child, it needs to have a uniquekey
prop for Flip Move to track it. -
Flip Move clones the direct children passed to it and overwrites the
ref
prop. As a result, you won't be able to set aref
on the top-most elements passed to FlipMove. To work around this limitation, you can wrap each child you pass to<FlipMove>
in a<div>
. -
Elements whose positions have not changed between states will not be animated. This means that no
onStart
oronFinish
callbacks 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
key
for both versions, and Flip Move will treat them as the same item. -
If you have a vertical list with numerous elements, exceeding viewport, and you are experiencing automatic scrolling issues when reordering an item (i.e. the browser scrolls to the moved item's position), you can add
style={{ overflowAnchor: 'none' }}
to the container element (e.g.<ul>
) to prevent this issue.
Known Issues
-
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
transition
ortransform
CSS properties on its direct children.
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
Top Related Projects
Open source, production-ready animation and gesture library for React
An easy way to perform animations when a React component enters or leaves the DOM
A spring that solves your animation problems.
✌️ A spring physics based React animation library
🇨🇭 A React renderer for Three.js
🎊 A collection of animations for inline style libraries
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot