tannerlinsley / react-move
- четверг, 20 апреля 2017 г. в 03:11:47
JavaScript
React Move - Beautifully and deterministically animate anything in React
Beautifully and deterministically animate anything in react.
$ yarn add react-move
# or
$ npm install react-move --only=dev
<script src='https://npmcdn.com/react-move@latest/react-move.js'></script>
A component for animating any single object.
data={ Object }
| Required
ignore
prop)default={ Object }
duration={ Number }
| 500
easing={ string | function }
| easeCubicOut
ignore={ []String }
| false
flexDuration={ Boolean }
| false
immutable={ Boolean }
| true
===
between the old data
and new data
is used to detect when an animation should occur. If you wish, you can disable immutable
mode which falls back to using JSON.stringify
to determine if an animation should occur.import React from 'react'
import { Animate } from 'react-move'
<Animate
// Set some default data
default={{
scale: 0,
color: 'blue'
}}
// Update your data to whatever you want
data={{
scale: Math.random() * 1,
color: _.sample(['red', 'blue', 'yellow']),
}}
duration={800}
easing='easeQuadIn' // anything from https://github.com/d3/d3-ease
>
{data => (
<div
style={{
transform: `scale(${data.scale})`,
background: data.color
}}
>
{data.scale * 100}
</div>
)}
</Animate>
A component that enables animating multiple elements, including enter and exit animations.
data={ []Objects }
| []
| Required
getKey={ function }
| (d, i) => i
enter
, update
and leave
states/groups.update={ function }
| Required
entering
or leaving
the list of items.enter={ function }
| () => null
entering
the list of items. If nothing is returned, the update
state is used.leave={ function }
| () => null
leaving
the list of items. If nothing is returned, the update
state is used.duration={ Number }
| 500
easing={ string | function }
| easeCubicOut
stagger={ Number }
| 0
staggerGroups={ Boolean }
| false
entering
, updating
and leaving
.ignore={ []String }
| false
flexDuration={ Boolean }
| false
import React from 'react'
import { Transition } from 'react-move'
const items = _.filter(items, (d, i) => i > Math.random() * 10)
<Transition
// pass an array of items to "data"
data={items}
// use "getKey" to return a unique ID for each item
getKey={(item, index) => index}
// the "update" function returns the items normal state to animate
update={item => ({
translate: 1,
opacity: 1,
color: 'grey'
})}
// the "enter" function returns the items origin state when entering
enter={item => ({
translate: 0,
opacity: 0,
color: 'blue'
})}
// the "leave" function returns the items destination state when leaving
leave={item => ({
translate: 2,
opacity: 0,
color: 'red'
})}
//
duration={800}
easing='easeQuadIn' // anything from https://github.com/d3/d3-ease
stagger={200} // you can also stagger by a percentage of the animation
staggerGroup // use this prop to stagger by enter/exit/update group index instead of by overall index
>
{data => ( // the child function is passed an array of itemStates
<div>
{data.map(item => {
// data[0] === { key: 0, data: 0, state: {...} }
return (
<div
key={item.key}
style={{
transform: `translateX(${100 * item.state.translate}px)`,
opacity: item.state.opacity,
color: item.state.color
}}
>
{item.data} - {Math.round(item.percentage * 100)}
</div>
)
})}
</div>
)}
</Transition>
If you would rather use a different easing function or just build your own, you can! Simply pass a function to the easing
prop and you're off!
<Animate
easing={(t) => { // This is Chart.js's easeOutBounce function :)
if ((t /= 1) < (1 / 2.75)) {
return 1 * (7.5625 * t * t)
} else if (t < (2 / 2.75)) {
return 1 * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75)
} else if (t < (2.5 / 2.75)) {
return 1 * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375)
}
return 1 * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375)
}}
>
To suggest a feature, create an issue if it does not already exist. If you would like to help develop a suggested feature follow these steps:
$ yarn
$ yarn run storybook
src/
directorylocalhost:8000
/stories
, or create a new one if needed$ yarn run storybook
Runs the storybook server$ yarn run test
Runs the test suite$ yarn run prepublish
Builds for NPM distribution$ yarn run docs
Builds the website/docs from the storybook for github pages