Last active
March 23, 2016 04:07
-
-
Save rt2zz/80802051d6d49816985d to your computer and use it in GitHub Desktop.
animations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { | |
Animated, | |
Component, | |
PropTypes, | |
} from 'react-native' | |
import _ from 'lodash' | |
import config from '../config' | |
class Animation extends Component { | |
_animatedValues = {}; | |
static propTypes = { | |
rules: PropTypes.object.isRequired, | |
Inner: PropTypes.func.isRequired, | |
restProps: PropTypes.object, | |
}; | |
componentWillMount() { | |
this._animatedValues = _.mapValues(this.props.rules, (mapProps) => new Animated.Value(mapProps(this.props.restProps))) | |
} | |
componentWillReceiveProps(nextProps) { | |
_.forEach(nextProps.rules, (mapProps, key) => { | |
let toValue = mapProps(nextProps.restProps) | |
// no animations in lowPerfMode | |
!config.lowPerfMode | |
? Animated.spring(this._animatedValues[key], {toValue}).start() | |
: this._animatedValues[key].setValue(toValue) | |
}) | |
} | |
render() { | |
let {Inner, restProps} = this.props | |
return <Inner {...restProps} {...this._animatedValues} /> | |
} | |
} | |
export default function animator(rules) { | |
return (Inner) => { | |
return (props) => <Animation restProps={props} rules={rules} Inner={Inner} /> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let aniFromProps = (props) => !props.something // animate between 0 or 1 based on some boolean | |
export default animator({ani: aniFromProps})(SomeComponent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not sure i follow how rules are executed and sequenced but I think that is just because i've never actually used Animated in react native. I like the lowPerfMode flag. that's pretty cool