Top Related Projects
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Redefined chart library built with React and D3
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
A collection of composable React components for building interactive data visualizations
🐯 visx | visualization components
Data Visualization Components
Quick Overview
React-Move is a React animation library that provides a flexible and powerful way to create data-driven animations. It allows developers to animate any component or SVG element based on changing data or state, offering fine-grained control over transitions and easing functions.
Pros
- Highly customizable with support for custom easing functions and interpolators
- Seamless integration with React components and lifecycle methods
- Efficient performance through the use of RAF (RequestAnimationFrame)
- Supports both entering and exiting animations for elements
Cons
- Steeper learning curve compared to simpler animation libraries
- Documentation could be more comprehensive, especially for advanced use cases
- Limited built-in animation presets, requiring more manual setup
- May be overkill for simple animation needs
Code Examples
- Basic number animation:
import { NodeGroup } from 'react-move'
<NodeGroup
data={[1]}
keyAccessor={(d) => d}
start={() => ({
opacity: 0,
x: 0,
})}
enter={() => ({
opacity: [1],
x: [300],
timing: { duration: 2000 },
})}
>
{(nodes) => (
<div style={{ position: 'relative' }}>
{nodes.map(({ key, data, state: { x, opacity } }) => (
<div
key={key}
style={{
position: 'absolute',
transform: `translate(${x}px, 0)`,
opacity,
}}
>
{data}
</div>
))}
</div>
)}
</NodeGroup>
- Animating SVG elements:
import { NodeGroup } from 'react-move'
<NodeGroup
data={data}
keyAccessor={(d) => d.name}
start={(d) => ({
x: 0,
y: 0,
opacity: 0,
})}
enter={(d) => ({
x: [d.x],
y: [d.y],
opacity: [1],
timing: { duration: 1500, ease: easeCubicInOut },
})}
>
{(nodes) => (
<g>
{nodes.map(({ key, data, state: { x, y, opacity } }) => (
<circle
key={key}
cx={x}
cy={y}
r={5}
opacity={opacity}
fill={data.color}
/>
))}
</g>
)}
</NodeGroup>
- Animating with custom easing:
import { NodeGroup } from 'react-move'
import { easeElastic } from 'd3-ease'
<NodeGroup
data={[1]}
keyAccessor={(d) => d}
start={() => ({
scale: 0,
})}
enter={() => ({
scale: [1],
timing: { duration: 1000, ease: easeElastic },
})}
>
{(nodes) => (
<div>
{nodes.map(({ key, state: { scale } }) => (
<div
key={key}
style={{
transform: `scale(${scale})`,
width: 50,
height: 50,
background: 'steelblue',
}}
/>
))}
</div>
)}
</NodeGroup>
Getting Started
-
Install the package:
npm install react-move
-
Import and use in your React component:
import { NodeGroup } from 'react-move' function MyComponent() { return ( <NodeGroup data={[/* your data */]} keyAccessor={(d) => /* unique key */} start={(d) => ({ /* initial state */ })} enter={(d) => ({ /* enter animation */ })} update={(d) => ({ /*
Competitor Comparisons
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Pros of d3
- Extensive and powerful data visualization library with a wide range of chart types and customization options
- Large and active community, extensive documentation, and numerous examples available
- Flexible and can be used with various frameworks or vanilla JavaScript
Cons of d3
- Steeper learning curve due to its low-level API and complex concepts
- Requires more code to create basic visualizations compared to higher-level libraries
- Performance can be an issue with large datasets or complex animations
Code Comparison
d3:
const svg = d3.select("body").append("svg")
.attr("width", 400)
.attr("height", 300);
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", 5);
react-move:
<NodeGroup
data={data}
keyAccessor={d => d.id}
start={d => ({ x: d.x, y: d.y })}
enter={d => ({ x: [d.x], y: [d.y] })}
update={d => ({ x: [d.x], y: [d.y] })}
>
{nodes => (
<g>
{nodes.map(({ key, data, state }) => (
<circle key={key} cx={state.x} cy={state.y} r={5} />
))}
</g>
)}
</NodeGroup>
react-move provides a more React-friendly API for creating animations and transitions, while d3 offers lower-level control and a wider range of visualization options.
Redefined chart library built with React and D3
Pros of Recharts
- More comprehensive and feature-rich charting library with a wide variety of chart types
- Easier to use for beginners with less configuration required
- Better documentation and larger community support
Cons of Recharts
- Less flexible for custom animations and transitions
- Heavier package size, which may impact performance for larger applications
- Limited customization options for advanced use cases
Code Comparison
React-move example:
<NodeGroup
data={data}
keyAccessor={(d) => d.name}
start={(d) => ({ x: 0, y: 0 })}
enter={(d) => ({ x: [d.x], y: [d.y] })}
update={(d) => ({ x: [d.x], y: [d.y] })}
leave={() => ({ x: [0], y: [0] })}
>
{(nodes) => (
<g>
{nodes.map(({ key, data, state }) => (
<circle key={key} r={5} cx={state.x} cy={state.y} />
))}
</g>
)}
</NodeGroup>
Recharts example:
<LineChart width={600} height={300} data={data}>
<XAxis dataKey="name" />
<YAxis />
<CartesianGrid stroke="#eee" strokeDasharray="5 5" />
<Line type="monotone" dataKey="value" stroke="#8884d8" />
</LineChart>
React-move offers more granular control over animations and transitions, while Recharts provides a higher-level API for creating charts with less code. React-move is better suited for custom visualizations, whereas Recharts excels in quickly creating standard chart types.
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
Pros of nivo
- Offers a wider variety of chart types and visualizations
- Provides better out-of-the-box styling and theming options
- Has more comprehensive documentation and examples
Cons of nivo
- Larger bundle size due to more features and dependencies
- Steeper learning curve for complex customizations
- Less flexibility for low-level animation control
Code Comparison
nivo example:
import { ResponsiveLine } from '@nivo/line'
const MyChart = ({ data }) => (
<ResponsiveLine
data={data}
margin={{ top: 50, right: 110, bottom: 50, left: 60 }}
xScale={{ type: 'point' }}
yScale={{ type: 'linear', min: 'auto', max: 'auto', stacked: true, reverse: false }}
/>
)
react-move example:
import { NodeGroup } from 'react-move'
const MyChart = ({ data }) => (
<NodeGroup
data={data}
keyAccessor={(d) => d.name}
start={(d) => ({ x: 0, y: 0 })}
enter={(d) => ({ x: [d.x], y: [d.y] })}
update={(d) => ({ x: [d.x], y: [d.y] })}
>
{(nodes) => (
<g>{nodes.map(({ key, data, state }) => <circle key={key} cx={state.x} cy={state.y} r={5} />)}</g>
)}
</NodeGroup>
)
A collection of composable React components for building interactive data visualizations
Pros of Victory
- More comprehensive charting library with a wide range of chart types
- Extensive documentation and examples
- Active community and regular updates
Cons of Victory
- Steeper learning curve due to its extensive API
- Larger bundle size, which may impact performance for simpler projects
Code Comparison
Victory:
import { VictoryBar, VictoryChart, VictoryAxis } from 'victory';
<VictoryChart>
<VictoryAxis tickValues={[1, 2, 3, 4]} />
<VictoryAxis dependentAxis />
<VictoryBar data={data} x="quarter" y="earnings" />
</VictoryChart>
React-move:
import { NodeGroup } from 'react-move';
<NodeGroup
data={data}
keyAccessor={(d) => d.name}
start={(d) => ({ x: 0, y: 0 })}
enter={(d) => ({ x: [d.x], y: [d.y] })}
update={(d) => ({ x: [d.x], y: [d.y] })}
leave={() => ({ x: [0], y: [0] })}
>
{(nodes) => (
<g>{nodes.map(({ key, data, state }) => <circle key={key} {...state} />)}</g>
)}
</NodeGroup>
React-move focuses on animation and transitions, providing more flexibility for custom visualizations. Victory offers a higher-level abstraction for common chart types, making it easier to create standard charts quickly. The choice between the two depends on the specific project requirements and the level of customization needed.
🐯 visx | visualization components
Pros of visx
- More comprehensive library with a wider range of chart types and visualization components
- Modular architecture allows for fine-grained control and customization
- Actively maintained by Airbnb with frequent updates and improvements
Cons of visx
- Steeper learning curve due to its extensive API and modular nature
- Requires more setup and configuration compared to react-move's simpler approach
- Larger bundle size, which may impact performance in some applications
Code Comparison
react-move example:
<NodeGroup
data={data}
keyAccessor={(d) => d.name}
start={(d) => ({ x: 0, y: 0 })}
enter={(d) => ({ x: [d.x], y: [d.y] })}
update={(d) => ({ x: [d.x], y: [d.y] })}
leave={() => ({ x: [0], y: [0] })}
>
{(nodes) => (
<g>
{nodes.map(({ key, data, state }) => (
<circle key={key} r={5} cx={state.x} cy={state.y} />
))}
</g>
)}
</NodeGroup>
visx example:
<svg width={width} height={height}>
<Group top={margin.top} left={margin.left}>
<BarGroup
data={data}
keys={keys}
height={yMax}
x0={d => x0(d.date)}
x0Scale={x0Scale}
x1Scale={x1Scale}
yScale={yScale}
color={color}
/>
<AxisBottom scale={x0Scale} top={yMax} />
<AxisLeft scale={yScale} />
</Group>
</svg>
Data Visualization Components
Pros of react-vis
- More comprehensive charting library with a wider range of chart types
- Better documentation and examples
- Larger community and more frequent updates
Cons of react-vis
- Steeper learning curve due to more complex API
- Larger bundle size, which may impact performance
- Less flexibility for custom animations
Code Comparison
react-vis:
import {XYPlot, LineSeries} from 'react-vis';
<XYPlot width={300} height={300}>
<LineSeries data={[{x: 1, y: 10}, {x: 2, y: 5}, {x: 3, y: 15}]} />
</XYPlot>
react-move:
import { NodeGroup } from 'react-move';
<NodeGroup
data={[{x: 1, y: 10}, {x: 2, y: 5}, {x: 3, y: 15}]}
keyAccessor={(d) => d.x}
start={(d) => ({y: 0})}
enter={(d) => ({y: [d.y]})}
update={(d) => ({y: [d.y]})}
>
{(nodes) => (
<g>
{nodes.map(({key, data, state}) => (
<circle key={key} cx={data.x * 100} cy={300 - state.y} r={5} />
))}
</g>
)}
</NodeGroup>
react-vis offers a more declarative approach with built-in chart components, while react-move provides lower-level primitives for creating custom animations and visualizations. react-vis is better suited for quickly creating standard charts, whereas react-move offers more flexibility for complex, custom visualizations at the cost of more verbose code.
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-Move
Beautiful, data-driven animations for React. Just 3.5kb (gzipped)!
Documentation and Examples
Features
- Animate HTML, SVG & React-Native
- Fine-grained control of delay, duration and easing
- Animation lifecycle events: start, interrupt, end
- Custom tweening functions
- Awesome documentation and lots of examples
- Supports TypeScript
Installation
// React ^16.3.0
npm install react-move
// React ^0.14.9 || ^15.3.0 || ^16.0.0
npm install react-move@^5.0.0
Note: The API for React Move 5.x and 6.x is exactly the same. The 5.x version just includes react-lifecycles-compat to make the library work with earlier versions of React. This adds a little to the bundle so use 6.x if you're using React 16.3+.
Upgrading from React Move 2.x and 3.x
The API for React Move has been essentially stable since the 2.0 version. The 4.0 version of React Move introduced a change that broke the hard dependency on d3-interpolate and introduced the interpolation
prop. The current version of React Move will by default only do numeric interpolation and apply easing functions. If you only need to do numeric interpolation you don't need to do anything. Just upgrade and done.
To get the same interpolation found in React Move 2.x and 3.x which includes support for colors, paths and SVG transforms do this:
Install d3-interpolate:
npm install d3-interpolate
Then in your app:
import { NodeGroup } from 'react-move'
import { interpolate, interpolateTransformSvg } from 'd3-interpolate'
...
<NodeGroup
data={this.state.data}
keyAccessor={(d) => d.name}
start={(data, index) => ({
...
})}
enter={(data, index) => ([ // An array
...
])}
update={(data) => ({
...
})}
leave={() => ({
...
})}
interpolation ={(begValue, endValue, attr) => { // pass as prop
if (attr === 'transform') {
return interpolateTransformSvg(begValue, endValue)
}
return interpolate(begValue, endValue)
}}
>
...children
</NodeGroup>
Demos
- CodeSandbox - Animated Bars (@peterrcook)
- Blog Post by Peter Cook
- CodeSandbox - Collapsible Tree (@techniq)
- CodeSandbox - Draggable List
- CodeSandbox - Circle Inferno
- CodeSandbox - Animated Mount/Unmount
- Examples
Documentation
The docs below are for version 6.x.x of React-Move.
Older versions:
The API for NodeGroup
and Animate
have not changed except for the interpolation
xw prop, but if you want to refer back:
Getting Started
React Move exports just two components:
- NodeGroup - If you have an array of items that enter, update and leave
- Animate - If you have a singe item that enters, updates and leaves
< NodeGroup />
Component Props
Name | Type | Default | Description |
---|---|---|---|
data * | array | An array. The data prop is treated as immutable so the nodes will only update if prev.data !== next.data. | |
keyAccessor * | function | Function that returns a string key given the data and its index. Used to track which nodes are entering, updating and leaving. | |
interpolation | function | numeric | A function that returns an interpolator given the begin value, end value, attr and namespace. Defaults to numeric interpolation. See docs for more. |
start * | function | A function that returns the starting state. The function is passed the data and index and must return an object. | |
enter | function | () => {} | A function that returns an object or array of objects describing how the state should transform on enter. The function is passed the data and index. |
update | function | () => {} | A function that returns an object or array of objects describing how the state should transform on update. The function is passed the data and index. |
leave | function | () => {} | A function that returns an object or array of objects describing how the state should transform on leave. The function is passed the data and index. |
children * | function | A function that receives an array of nodes. |
< Animate />
Component Props
Name | Type | Default | Description |
---|---|---|---|
show | bool | true | Boolean value that determines if the child should be rendered or not. |
interpolation | function | numeric | A function that returns an interpolator given the begin value, end value, atrr and namespace. See docs for more. |
start | union: func object | An object or function that returns an obejct to be used as the starting state. | |
enter | union: func array object | An object, array of objects, or function that returns an object or array of objects describing how the state should transform on enter. | |
update | union: func array object | An object, array of objects, or function that returns an object or array of objects describing how the state should transform on update. Note: although not required, in most cases it make sense to specify an update prop to handle interrupted enter and leave transitions. | |
leave | union: func array object | An object, array of objects, or function that returns an object or array of objects describing how the state should transform on leave. | |
children * | function | A function that receives the state. |
Starting state
Before looking at the components it might be good to look at starting state. You are going to be asked to define starting states for each item in your NodeGroup
and Animate
components. This is a key concept and probably the most error prone for developers working with React Move. The starting state for each item is always an object with string or number leaves. The leaf keys are referred to as "attrs" as in "attribute." There are also "namespaces" which are a purely organizational concept.
Two rules to live by for starting states:
- Don't use the strings "timing" or "events" as an attr or namespace.
- There should never be an array anywhere in your object.
Example starting state:
// GOOD
{
attr1: 100,
attr2: 200,
attr3: '#dadada'
}
// BAD
{
attr1: [100], // NO ARRAYS
attr2: 200,
attr3: '#dadada'
}
A more concrete example might be:
{
opacity: 0.1,
x: 200,
y: 100,
color: '#dadada'
}
You can add "namespaces" to help organize your state:
{
attr1: 100,
attr2: 200,
attr3: '#ddaabb',
namespace1: {
attr1: 100,
attr2: 200
}
}
Or something like:
{
namespace1: {
attr1: 100,
attr2: 200
},
namespace2: {
attr1: 100,
attr2: 200
}
}
You might use namespaces like so:
{
inner: {
x: 100,
y: 150,
color: '#545454'
},
outer: {
x: 300,
y: 350,
color: '#3e3e3e'
}
}
Starting state in NodeGroup
In NodeGroup
you are working with an array of items and you pass a start prop (a function) that receives the data item and its index. The start prop will be called when that data item (identified by its key) enters. Note it could leave and come back and that prop will be called again. Immediately after the starting state is set your enter transition (optional) is called allowing you to transform that state.
<NodeGroup
data={data} // an array (required)
keyAccessor={item => item.name} // function to get the key of each object (required)
start={(item, index) => ({ // returns the starting state of node (required)
...
})}
>
{(nodes) => (
...
{nodes.map(({ key, data, state }) => {
...
})}
...
)}
</NodeGroup>
Starting state in Animate
In Animate
you are animating a single item and pass a start prop that is an object or a function. The start prop will be called when that the item enters. Note it could leave and come back by toggling the show prop. Immediately after the starting state is set your enter transition (optional) is called allowing you to transform that state.
<Animate
start={{ // object or function
...
}}
>
{state => (
...
)}
</Animate>
Transitioning state
You return a config object or an array of config objects in your enter, update and leave props functions for both NodeGroup
and Animate
. Instead of simply returning the next state these objects describe how to transform the state. Each config object can specify its own duration, delay, easing and events independently.
There are two special keys you can use: timing and events. Both are optional. Timing and events are covered in more detail below.
If you aren't transitioning anything then it wouldn't make sense to be using NodeGroup. That said, it's convenient to be able to set a key to value when a node enters, updates or leaves without transitioning. To support this you can return four different types of values to specify how you want to transform the state.
-
string or number
: Set the key to the value immediately with no transition. Ignores all timing values. -
array [value]
: Transition from the key's current value to the specified value. Value is a string or number. -
array [value, value]
: Transition from the first value to the second value. Each value is a string or number. -
function
: Function will be used as a custom tween function.
Example config object:
{
attr1: [200],
attr2: 300,
attr3: ['#dadada']
timing: { duration: 300, delay: 100 }
}
Using namespaces:
{
attr1: [100],
attr3: '#ddaabb',
namespace1: {
attr1: [300],
attr2: 200
},
timing: { duration: 300, delay: 100 }
}
To have different timing for some keys use an array of config objects:
[
{
attr1: [200, 500],
timing: { duration: 300, delay: 100 }
},
{
attr2: 300, // this item, not wrapped in an array, will be set immediately, so which object it's in doesn't matter
attr3: ['#dadada']
timing: { duration: 600 }
},
]
Example Transitions in NodeGroup
<NodeGroup
data={this.state.data}
keyAccessor={(d) => d.name}
start={(data, index) => ({
opacity: 1e-6,
x: 1e-6,
fill: 'green',
width: scale.bandwidth(),
})}
enter={(data, index) => ({
opacity: [0.5], // transition opacity on enter
x: [scale(data.name)], // transition x on on enter
timing: { duration: 1500 }, // timing for transitions
})}
update={(data) => ({
...
})}
leave={() => ({
...
})}
>
{(nodes) => (
...
)}
</NodeGroup>
Using an array of config objects:
import { easeQuadInOut } from 'd3-ease';
...
<NodeGroup
data={this.state.data}
keyAccessor={(d) => d.name}
start={(data, index) => ({
opacity: 1e-6,
x: 1e-6,
fill: 'green',
width: scale.bandwidth(),
})}
enter={(data, index) => ([ // An array
{
opacity: [0.5], // transition opacity on enter
timing: { duration: 1000 }, // timing for transition
},
{
x: [scale(data.name)], // transition x on on enter
timing: { delay: 750, duration: 1500, ease: easeQuadInOut }, // timing for transition
},
])}
update={(data) => ({
...
})}
leave={() => ({
...
})}
>
{(nodes) => (
...
)}
</NodeGroup>
Timing
If there's no timing key in your object you'll get the timing defaults. You can specify just the things you want to override on your timing key.
Here's the timing defaults...
const defaultTiming = {
delay: 0,
duration: 250,
ease: easeLinear
};
For the ease key, just provide the function. You can use any easing function, like those from d3-ease...
List of ease functions exported from d3-ease
Events
You can add events on your config objects. You can pass a function that will run when the transition starts, is interrupted (an update to the data occurs) or ends.
Using Events:
{
attr1: [100],
attr3: '#ddaabb',
namespace1: {
attr1: [300],
attr2: 200
},
timing: { duration: 300, delay: 100 },
events: {
start: () => {
..do stuff - use an arrow function to keep the context of the outer component
},
interrupt: () => {
..do stuff - use an arrow function to keep the context of the outer component
},
end: () => {
..do stuff - use an arrow function to keep the context of the outer component
},
}
}
Interpolation
You can wire your components in react-move
to handle different types of interpolation using the interpolation
prop in both NodeGroup
and Animate
. The code for interpolating strings or SVG paths can be bulky and, in many cases, it's not needed so by default components only handle numeric interpolation.
Your interpolation
prop is a function that should avoid a lot of logic and computation. It will get called at high frequency when transitions fire in your components. You get the begin and end values and what the attribute name (string) is. You will also get the namespace string (less common) if you are using them in your state. See the sections on starting states and transitions for more on attrs and namespaces.
Cadillac Interpolation - Depends on d3-interpolate
To wire up a full service interpolation that will interpolate colors, paths, numbers and SVG transforms you can use a setup like this:
npm install react-move d3-interpolate
Then in your app:
import { NodeGroup, Animate } from 'react-move'
import { interpolate, interpolateTransformSvg } from 'd3-interpolate'
...
<NodeGroup
data={this.state.data}
keyAccessor={(d) => d.name}
start={(data, index) => ({
...
})}
enter={(data, index) => ([ // An array
...
])}
update={(data) => ({
...
})}
leave={() => ({
...
})}
interpolation ={(begValue, endValue, attr, namespace) => { // pass as prop
if (attr === 'transform') {
return interpolateTransformSvg(begValue, endValue)
}
return interpolate(begValue, endValue)
}}
>
...children
</NodeGroup>
This setup mimics how d3.js
works for selecting interpolators and will not force you to think too much about the values your are using. For example, if you use colors (in any format) they will be recognized and interpolated correctly. The interpolate
function exported from d3-interpolate does a great job of guessing what you're trying to do and handles it for you but it also includes a lot of code (e.g. d3-color) that may not be needed for your project.
Numeric Interpolation Only - Default - No dependencies
To do numeric interpolation you don't need to do anything in your components. The default numeric interpolator looks like this:
// The default interpolator used in NodeGroup and Animate
const numeric = (beg, end) => {
const a = +beg;
const b = +end - a;
return function(t) {
return a + b * t;
};
};
React-Move vs React-Motion
-
React-move allows you to define your animations using durations, delays and ease functions. In react-motion you use spring configurations to define your animations.
-
React-move is designed to easily plugin interpolation for strings, numbers, colors, SVG paths and SVG transforms. With react-motion you can only interpolate numbers so you have to do a bit more work use colors, paths, etc.
-
In react-move you can define different animations for entering, updating and leaving with the ability to specify delay, duration and ease on each individual key. React-motion allows you to define a spring configuration for each key in the "style" object.
-
React-move has lifecycle events on its transitions. You can pass a function to be called on transition start, interrupt or end. React-motion has an "onRest" prop that fires a callback when the animation stops (just the
Motion
component notTransitionMotion
orStaggeredMotion
). -
React-move also allows you to pass your own custom tween functions. It's all springs in react-motion.
Contributing
We love contributions from the community! Read the contributing info here.
Run the repo locally
- Fork this repo
npm install
cd docs
npm install
npm start
Scripts
Run these from the root of the repo
npm run lint
Lints all files in src and docsnpm run test
Runs the test suite locallynpm run test:coverage
Get a coverage report in the consolenpm run test:coverage:html
Get an HTML coverage report in coverage folder
Go to live examples, code and docs!
Top Related Projects
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Redefined chart library built with React and D3
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
A collection of composable React components for building interactive data visualizations
🐯 visx | visualization components
Data Visualization Components
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