enable react-native css transition and animation
npm install animatableYou can enable css-animation and css-transition for react-native easily.
Also works on web.
``sh`
npm install animatable
Animation will be triggered when specified style prop changed.
When property option is specified behaviour is like css-transition.
`js
import { View } from 'react-native'
import animatable from 'animatable'
const TransitionLeftView = animatable({
property: 'left',
duration: 400,
timingFunction: 'ease-in-out'
})(View)
style={{
position: 'absolute',
left: this.state.on ? 100 : 0,
top: 0,
width: 100,
height: 100,
backgroundColor: 'red'
}}
/>
`
#### Screenshot
When keyframes option is specified behaviour is like css-animation.
`js
import { View } from 'react-native'
import animatable from 'animatable'
// define keyframes
const keyframes = {
'0%': { marginLeft: '0%', marginRight: '100%' },
'50%': { marginLeft: '25%', marginRight: '0%' },
'100%': { marginLeft: '100%', marginRight: '0%' }
}
const Bar = animatable({
keyframes: keyframes,
direction: 'alternate-reverse',
duration: 1000,
timingFunction: 'linear',
iterationCount: 'infinite',
})(View)
// render
const Indicator = (props) => (
height: 20,
backgroundColor: 'lightgray'
}}>
height: '100%',
backgroundColor: 'black',
}}
/>
)
`
#### Screenshot
`js
const Bar = animatable({
keyframes: {
'0%': { marginLeft: '0%', marginRight: '100%' },
'50%': { marginLeft: '25%', marginRight: '0%' },
'100%': { marginLeft: '100%', marginRight: '0%' }
},
duration: 1000,
// you can set (prop) => value
playState: (props) => props.enabled ? 'paused' : 'running'
})(View)
enabled={this.state.enabled}
/>
`
`js
import styled from 'styled-components'
const Bar = styled(animatable({
keyframes: {
'0%': { marginLeft: '0%', marginRight: '100%' },
'50%': { marginLeft: '25%', marginRight: '0%' },
'100%': { marginLeft: '100%', marginRight: '0%' }
},
duration: 1000,
playState: (props) => props.enabled ? 'running': 'paused'
})(View))
height: 100%;
background-color: black;`
You have to specify millisec value for duration and delay.
You can use property values same as described on css definitions, and default values are same as css's one of web.
`js
const TransitionComponent = animatable({
// required
property: 'transform',
duration: 200,
// optional
delay: 500,
timingFunction: 'linear'
})(Component)
`
`js
const AnimationComponent = animatable({
// required
keyframes: {
'0%': { marginLeft: '0%', marginRight: '100%' },
'50%': { marginLeft: '25%', marginRight: '0%' },
'100%': { marginLeft: '100%', marginRight: '0%' }
},
duration: 200,
// optional
delay: 500,
timingFunction: 'linear'
iterationCount: 'infinite',
direction: 'normal',
playState: 'running',
// TODO: fillMode: 'forwards'
})(Component)
``
https://yusukeshibata.github.io/animatable/
Yusuke Shibata
MIT