Package Exports
- react-spring
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-spring) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
npm install react-spring
Examples: Api demonstration | Native rendering | Single transition | Multiple item transition | Animated Todo MVC
Why 🤔
React-spring is a wrapper around a cooked down fork of Facebooks animated. It is trying to bridge Chenglou's React-motion and animated as both have their pros and cons, but definitively could benefit from one another:
React-motion
- Declarative api that doesn't involve manual management of handles
- Performance can suffer because components are re-rendered every frame
- Can't interpolate between raw state as it doesn't know colors, paths, gradients, etc.
Animated
- Interpolates most web privimites, units and patterns
- Efficiently changes styles in the dom instead of re-rendering a component with fresh props frame by frame
- Managing and orchestrating handles (starting/stopping/waiting/cleaning) can become a real chore
This lib inherits React-motions api while you can feed it everything animated can interpolate. It also has support for animateds efficient native rendering.
Default rendering 🐎
Like React-motion by default we'll render the receiving component every frame as it gives you more freedom to animate whatever you like. In many situations this will be ok.
import { Spring } from 'react-spring'
const App = ({ toggle }) => (
<Spring
// Default values, optional ...
from={{ opacity: 0 }}
// Will animate to ...
to={{
// Can be numbers, colors, paths, degrees, percentages, ...
color: toggle ? 'red' : '#00ff00',
start: toggle ? '#abc' : 'rgb(10,20,30)',
end: toggle ? 'seagreen' : 'rgba(0,0,0,0.5)',
stop: toggle ? '0%' : '50%',
scale: toggle ? 1 : 2,
rotate: toggle ? '0deg' : '45deg',
path: toggle
? 'M20,380 L380,380 L380,380 L200,20 L20,380 Z'
: 'M20,20 L20,380 L380,380 L380,20 L20,20 Z',
}}>
{({ color, scale, rotate, path, start, stop, end }) => (
<div style={{ background: `linear-gradient(to bottom, ${start} ${stop}, ${end} 100%)` }}>
<svg style={{ transform: `scale(${scale}) rotate(${rotate})` }}>
<g fill={color}>
<path d={path} />
</g>
</svg>
</div>
)}
</Spring>
)
Native rendering 🚀
If you need more performance then pass the native
flag. Now your component will only render once and all updates will be sent straight to the dom without any React reconciliation passes.
Just be aware of the following conditions:
- You can only animate native styles and props
- If you use transforms make sure it's an array
- Receiving components have to be "animated components"
- The values you receive are opaque objects, not regular values
import { Spring, animated } from 'react-spring'
const App = () => (
<Spring
native
from={{ fill: 'black' }}
to={{
fill: toggle ? '#247BA0' : '#70C1B3',
backgroundColor: toggle ? '#B2DBBF' : '#F3FFBD',
transform: [{ rotate: toggle ? '0deg' : '180deg' }, { scale: toggle ? 0.6 : 1.5 }],
path: toggle ? TRIANGLE : RECTANGLE,
}}>
{({ toggle, fill, backgroundColor, transform, path }) => (
<animated.div style={{ backgroundColor }}>
<animated.svg style={{ transform, fill }}>
<g onClick={toggle}>
<animated.path d={path} />
</g>
</animated.svg>
</animated.div>
)}
</Spring>
)
If you need to interpolate native styles, use animated.template
. For instance, given that you receive startColor and endColor as animatable values you could do it like so:
import { Spring, animated, template } from 'react-spring'
const Content = ({ startColor, endColor }) => {
const background = template`linear-gradient(bottom ${startColor} 0%, ${endColor} 100%)`
return (
<animated.div style={{ background }}>
...
</animated.div>
)
)
const App = () => (
<Spring
native
from={{ startColor: 'black', endColor: 'black' }}
to={{ startColor: '#70C1B3', endColor: 'seagreen' }}
children={Content} />
)
Transitions ☔️
Use SpringTransition
and pass in your keys
. from
denotes base styles, enter
styles are applied when objects appear, leave
styles are applied when objects disappear. You do not pass in components as children but rather functions that receive a style object. Keys and children have to match in their order! You can again use the native
flag for direct dom animation.
import React, { PureComponent } from 'react'
import { SpringTransition, animated } from 'react-spring'
export default class AppContent extends PureComponent {
state = { items: ['item1', 'item2', 'item3'] }
componentDidMount() {
setTimeout(() => this.setState({ items: ['item1', 'item2', 'item3', 'item4'] }), 2000)
setTimeout(() => this.setState({ items: ['item1', 'item3', 'item4'] }), 4000)
}
render() {
return (
<ul>
<SpringTransition
keys={this.state.items}
from={{ opacity: 0, color: 'black', height: 0 }}
enter={{ opacity: 1, color: 'red', height: 18 }}
leave={{ opacity: 0, color: 'blue', height: 0 }}>
{this.state.items.map(item => styles => <li style={styles}>{item}</li>)}
</SpringTransition>
</ul>
)
}
}