Gathering detailed insights and metrics for react-motion
Gathering detailed insights and metrics for react-motion
Gathering detailed insights and metrics for react-motion
Gathering detailed insights and metrics for react-motion
A spring that solves your animation problems.
npm install react-motion
Typescript
Module System
Node Version
NPM Version
91
Supply Chain
98.8
Quality
77.2
Maintenance
100
Vulnerability
100
License
JavaScript (99.54%)
HTML (0.46%)
Total Downloads
178,788,442
Last Day
8,312
Last Week
384,033
Last Month
1,604,316
Last Year
22,563,824
21,720 Stars
809 Commits
1,154 Forks
276 Watching
25 Branches
72 Contributors
Minified
Minified + Gzipped
Latest Version
0.5.2
Package Id
react-motion@0.5.2
Size
78.16 kB
NPM Version
5.4.2
Node Version
8.4.0
Publised On
02 Oct 2017
Cumulative downloads
Total Downloads
Last day
-22.3%
8,312
Compared to previous day
Last week
-7.8%
384,033
Compared to previous week
Last month
-11.9%
1,604,316
Compared to previous month
Last year
-18.2%
22,563,824
Compared to previous year
3
1
33
1import {Motion, spring} from 'react-motion'; 2// In your render... 3<Motion defaultStyle={{x: 0}} style={{x: spring(10)}}> 4 {value => <div>{value.x}</div>} 5</Motion>
Animate a counter from 0
to 10
. For more advanced usage, see below.
Npm: npm install --save react-motion
Bower: do not install with bower install react-motion
, it won't work. Use bower install --save https://unpkg.com/react-motion/bower.zip
. Or in bower.json
:
1{ 2 "dependencies": { 3 "react-motion": "https://unpkg.com/react-motion/bower.zip" 4 } 5}
then include as
1<script src="bower_components/react-motion/build/react-motion.js"></script>
1<script src="https://unpkg.com/react-motion/build/react-motion.js"></script> 2(Module exposed as `ReactMotion`)
Works with React-Native v0.18+.
1git clone https://github.com/chenglou/react-motion.git 2cd react-motion 3npm install
npm start
.npm run build-demos
and open the static demos/demo_name/index.html
file directly. Don't forget to use production mode when testing your animation's performance!To build the repo yourself: npm run prepublish
.
For 95% of use-cases of animating components, we don't have to resort to using hard-coded easing curves and duration. Set up a stiffness and damping for your UI element, and let the magic of physics take care of the rest. This way, you don't have to worry about petty situations such as interrupted animation behavior. It also greatly simplifies the API.
This library also provides an alternative, more powerful API for React's TransitionGroup
.
Exports:
spring
Motion
StaggeredMotion
TransitionMotion
presets
Here's the well-annotated public Flow type definition file (you don't have to use Flow with React-motion, but the types help document the API below).
P.S. using TypeScript? Here are the React-motion TypeScript definitions!
Used in conjunction with the components below. Specifies the how to animate to the destination value, e.g. spring(10, {stiffness: 120, damping: 17})
means "animate to value 10, with a spring of stiffness 120 and damping 17".
val
: the value.
config
: optional, for further adjustments. Possible fields:
stiffness
: optional, defaults to 170
.damping
: optional, defaults to 26
.precision
: optional, defaults to 0.01
. Specifies both the rounding of the interpolated value and the speed (internal).It's normal not to feel how stiffness and damping affect your spring; use Spring Parameters Chooser to get a feeling. Usually, you'd just use the list of tasteful stiffness/damping presets below.
{stiffness, damping}
Commonly used spring configurations used like so: spring(10, presets.wobbly)
or spring(20, {...presets.gentle, precision: 0.1})
. See here.
1<Motion defaultStyle={{x: 0}} style={{x: spring(10)}}> 2 {interpolatingStyle => <div style={interpolatingStyle} />} 3</Motion>
Required. The Style
type is an object that maps to either a number
or an OpaqueConfig
returned by spring()
above. Must keep the same keys throughout component's existence. The meaning of the values:
OpaqueConfig
returned from spring(x)
: interpolate to x
.number
x
: jump to x
, do not interpolate.Optional. The PlainStyle
type maps to number
s. Defaults to an object with the same keys as style
above, whose values are the initial numbers you're interpolating on. Note that during subsequent renders, this prop is ignored. The values will interpolate from the current ones to the destination ones (specified by style
).
Required function.
interpolatedStyle
: the interpolated style object passed back to you. E.g. if you gave style={{x: spring(10), y: spring(20)}}
, you'll receive as interpolatedStyle
, at a certain time, {x: 5.2, y: 12.1}
, which you can then apply on your div
or something else.
Return: must return one React element to render.
Optional. The callback that fires when the animation comes to a rest.
Animates a collection of (fixed length) items whose values depend on each other, creating a natural, springy, "staggering" effect like so. This is preferred over hard-coding a delay for an array of Motions
to achieve a similar (but less natural-looking) effect.
1<StaggeredMotion 2 defaultStyles={[{h: 0}, {h: 0}, {h: 0}]} 3 styles={prevInterpolatedStyles => prevInterpolatedStyles.map((_, i) => { 4 return i === 0 5 ? {h: spring(100)} 6 : {h: spring(prevInterpolatedStyles[i - 1].h)} 7 })}> 8 {interpolatingStyles => 9 <div> 10 {interpolatingStyles.map((style, i) => 11 <div key={i} style={{border: '1px solid', height: style.h}} />) 12 } 13 </div> 14 } 15</StaggeredMotion>
Aka "the current spring's destination value is the interpolating value of the previous spring". Imagine a spring dragging another. Physics, it works!
Required function. Don't forget the "s"!
previousInterpolatedStyles
: the previously interpolating (array of) styles (undefined
at first render, unless defaultStyles
is provided).
Return: must return an array of Style
s containing the destination values, e.g. [{x: spring(10)}, {x: spring(20)}]
.
Optional. Similar to Motion
's defaultStyle
, but an array of them.
Required function. Similar to Motion
's children
, but accepts the array of interpolated styles instead, e.g. [{x: 5}, {x: 6.4}, {x: 8.1}]
(No onRest
for StaggeredMotion because we haven't found a good semantics for it yet. Voice your support in the issues section.)
Helps you to do mounting and unmounting animation.
You have items a
, b
, c
, with their respective style configuration, given to TransitionMotion
's styles
. In its children
function, you're passed the three interpolated styles as params; you map over them and produce three components. All is good.
During next render, you give only a
and b
, indicating that you want c
gone, but that you'd like to animate it reaching value 0
, before killing it for good.
Fortunately, TransitionMotion
has kept c
around and still passes it into the children
function param. So when you're mapping over these three interpolated styles, you're still producing three components. It'll keep interpolating, while checking c
's current value at every frame. Once c
reaches the specified 0
, TransitionMotion
will remove it for good (from the interpolated styles passed to your children
function).
This time, when mapping through the two remaining interpolated styles, you'll produce only two components. c
is gone for real.
1import createReactClass from 'create-react-class'; 2 3const Demo = createReactClass({ 4 getInitialState() { 5 return { 6 items: [{key: 'a', size: 10}, {key: 'b', size: 20}, {key: 'c', size: 30}], 7 }; 8 }, 9 componentDidMount() { 10 this.setState({ 11 items: [{key: 'a', size: 10}, {key: 'b', size: 20}], // remove c. 12 }); 13 }, 14 willLeave() { 15 // triggered when c's gone. Keeping c until its width/height reach 0. 16 return {width: spring(0), height: spring(0)}; 17 }, 18 render() { 19 return ( 20 <TransitionMotion 21 willLeave={this.willLeave} 22 styles={this.state.items.map(item => ({ 23 key: item.key, 24 style: {width: item.size, height: item.size}, 25 }))}> 26 {interpolatedStyles => 27 // first render: a, b, c. Second: still a, b, c! Only last one's a, b. 28 <div> 29 {interpolatedStyles.map(config => { 30 return <div key={config.key} style={{...config.style, border: '1px solid'}} /> 31 })} 32 </div> 33 } 34 </TransitionMotion> 35 ); 36 }, 37});
First, two type definitions to ease the comprehension.
TransitionStyle
: an object of the format {key: string, data?: any, style: Style}
.
key
: required. The ID that TransitionMotion
uses to track which configuration is which across renders, even when things are reordered. Typically reused as the component key
when you map over the interpolated styles.
data
: optional. Anything you'd like to carry along. This is so that when the previous section example's c
disappears, you still get to access c
's related data, such as the text to display along with it.
style
: required. The actual starting style configuration, similar to what you provide for Motion
's style
. Maps keys to either a number or an OpaqueConfig
returned by spring()
.
TransitionPlainStyle
: similar to above, except the style
field's value is of type PlainStyle
, aka an object that maps to numbers.
Required. Accepts either:
an array of TransitionStyle
configs, e.g. [{key: 'a', style: {x: spring(0)}}, {key: 'b', style: {x: spring(10)}}]
.
a function similar to StaggeredMotion
, taking the previously interpolating styles (undefined
at first call, unless defaultStyles
is provided), and returning the previously mentioned array of configs. You can do staggered mounting animation with this.
Optional. Similar to the other components' defaultStyle
/defaultStyles
.
Required function. Similar to other two components' children
. Receive back an array similar to what you provided for defaultStyles
, only that each style
object's number value represent the currently interpolating value.
Optional. Defaults to () => null
. The magic sauce property.
styleThatLeft
: the e.g. {key: ..., data: ..., style: ...}
object from the styles
array, identified by key
, that was present during a previous render, and that is now absent, thus triggering the call to willLeave
. Note that the style property is exactly what you passed in styles
, and is not interpolated. For example, if you passed a spring for x
you will receive an object like {x: {stiffness, damping, val, precision}}
.
Return: null
to indicate you want the TransitionStyle
gone immediately. A Style
object to indicate you want to reach transition to the specified value(s) before killing the TransitionStyle
.
{key: string, data?: any}
) => voidOptional. Defaults to () => {}
.
styleThatLeft
: the {key:..., data:...}
that was removed after the finished transition.Optional. Defaults to styleThatEntered => stripStyle(styleThatEntered.style)
. Where stripStyle
turns {x: spring(10), y: spring(20)}
into {x: 10, y: 20}
.
styleThatEntered
: similar to willLeave
's, except the TransitionStyle
represents the object whose key
value was absent during the last render
, and that is now present.
Return: a defaultStyle
-like PlainStyle
configuration, e.g. {x: 0, y: 0}
, that serves as the starting values of the animation. Under this light, the default provided means "a style config that has the same starting values as the destination values".
Note that willEnter
and defaultStyles
serve different purposes. willEnter
only triggers when a previously inexistent TransitionStyle
inside styles
comes into the new render.
(No onRest
for TransitionMotion because we haven't found a good semantics for it yet. Voice your support in the issues section.)
Hard-coded duration goes against fluid interfaces. If your animation is interrupted mid-way, you'd get a weird completion animation if you hard-coded the time. That being said, in the demo section there's a great Spring Parameters Chooser for you to have a feel of what spring is appropriate, rather than guessing a duration in the dark.
TransitionMotion
container itself?You don't. Unless you put it in another TransitionMotion
...
See StaggeredMotion
ref
doesn't work in the children function.React string refs won't work:
1<Motion style={...}>{currentValue => <div ref="stuff" />}</Motion>
This is how React works. Here's the callback ref solution.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 14/19 approved changesets -- score normalized to 7
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
166 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More