npm install react-spring
- What is it?
- Why do we need yet another?
- What others say
- Used by
- API reference and examples
- Basic overview
- Interpolation
- Render props
- Native rendering
- React-native and other targets
- Funding
A set of spring-physics based primitives (as in building blocks) that should cover most of your UI related animation needs once plain CSS can't cope any longer. Forget easings, durations, timeouts and so on as you fluidly move data from one state to another. This isn't meant to solve each and every problem but rather to give you tools flexible enough to confidently cast ideas into moving interfaces.
react-spring is a cooked down fork of Christopher Chedeau's animated (which is used in react-native by default). It is trying to bridge it with Cheng Lou's react-motion. Although both are similarily spring-physics based they are still polar opposites.
Declarative | Primitives | Interpolations | Performance | |
---|---|---|---|---|
React-motion | ✅ | ✅ | ❌ | ❌ |
Animated | ❌ | ❌ | ✅ | ✅ |
React-spring | ✅ | ✅ | ✅ | ✅ |
react-spring builds upon animated's foundation, making it leaner and more flexible. It inherits react-motions declarative api and goes to great lengths to simplify it. It has lots of useful primitives, can interpolate mostly everything and last but not least, can animate by committing directly to the dom instead of re-rendering a component frame-by-frame.
For a more detailed explanation read Why React needed yet another animation library.
And many others ...
You'll find a full docs, live playgrounds, prop descriptions and so forth here:
A Spring
moves data from one state to another. from
is the optional initial state, to
is where the spring will shift values towards. You can update to
any time, mid-animation or not, and it will smoothly adapt.
A Transition
animates component lifecycles. It takes a list of items of any type, and their keys. Whenever items are added, removed, reordered or updated, it will help you to animate these changes.
It can also take a single item, which can be anything. You can use it to toggle between components.
It also comes in handy for single-component mount/unmount reveals. Unmounting elements will vanish from the dom once their animation concludes.
A Trail
animates the first item of a list of elements, the rest follow the spring of their previous sibling.
We don't handle just numbers, you can interpolate almost everything:
- colors (names, rgb, rgba, hsl, hsla)
- absolute lenghts (cm, mm, in, px, pt, pc)
- relative lengths (em, ex, ch, rem, vw, vh, vmin, vmax, %)
- angles (deg, rad, grad, turn)
- flex and grid units (fr, etc)
- all HTML attributes
- SVG paths (as long as the number of points matches, otherwise use custom interpolation)
- arrays
- string patterns (
transform
,border
,boxShadow
, etc) auto
is valid- non-animatable string values (
visibility
,pointerEvents
, etc) scrollTop
/scrollLeft
(native only, since these aren't actual dom properties)
<Spring
to={{
width: 'auto',
padding: 20,
width: '80%',
background: 'linear-gradient(to right, #009fff, #ec2f4b)',
transform: 'perspective(600px) translate3d(0px,0,0) scale(1) rotateX(0deg)',
boxShadow: '0px 10px 20px 0px rgba(0,0,0,0.4)',
borderBottom: '10px solid #2D3747',
shape: 'M20,20 L20,380 L380,380 L380,20 L20,20 Z',
textShadow: '0px 5px 15px rgba(255,255,255,0.5)' }}>
import { Spring, animated } from 'react-spring'
<Spring native from={{ opacity: 0 }} to={{ opacity: 1 }}>
{styles => <animated.div style={styles}>i will fade in</animated.div>}
</Spring>
Native rendering comes with a few caveats you should know about before using it, more about that here. Try going native in all situations where you can, the benefits are worth it!
If you like this project, consider helping out, all contributions are welcome as well as donations to opencollective or Patreon. You can make one off donations in crypto to 36fuguTPxGCNnYZSRdgdh6Ea94brCAjMbH (BTC).
This project exists thanks to all the people who contribute.
Thank you to all our backers! 🙏
Support this project by becoming a sponsor. Your logo will show up here with a link to your website.