Package Exports
- react-spring
- react-spring/dist/native.cjs.js
- react-spring/dist/universal
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
Table of Contents
- 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
What is it?
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.
Why do we need yet another?
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.
What others say
Used by
And many others ...
API Reference and examples
You'll find a full docs, live playgrounds, prop descriptions and so forth here:
http://react-spring.surge.sh/
Basic overview
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.
Interpolation
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)' }}>
Native rendering
![]() |
![]() |
---|---|
Most libs animate by having React recalculate the component-tree on every frame. Here it attempts to animate a component consisting of ~300 sub-components, plowing through the frame budget and causing jank. | React-spring with the native property renders the component only once, from then on the animation will be applied directly to the dom in a requestAnimationFrame-loop, similar to how gsap and d3 do it. |
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!
Funding
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).
Contributors
This project exists thanks to all the people who contribute.
Backers
Thank you to all our backers! 🙏
Gold sponsors
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website.