-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from shoutem/release/0.14.0
Release/0.14.0
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { PureComponent } from 'react'; | ||
import autoBindReact from 'auto-bind/react'; | ||
import _ from 'lodash'; | ||
import PropTypes from 'prop-types'; | ||
import { Animated, Easing } from 'react-native'; | ||
|
||
export default class Wiggle extends PureComponent { | ||
constructor(props) { | ||
super(props); | ||
|
||
autoBindReact(this); | ||
|
||
this.animation = new Animated.Value(0); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
const { startAnimation: prevStartAnimation } = prevProps; | ||
const { startAnimation } = this.props; | ||
|
||
if (!prevStartAnimation && startAnimation) { | ||
this.triggerAnimation(); | ||
} | ||
} | ||
|
||
triggerAnimation() { | ||
const { style } = this.props; | ||
const duration = _.get(style, 'timingConfig.duration'); | ||
const inputRange = _.get(style, 'interpolateConfig.inputRange'); | ||
const toValue = _.last(inputRange); | ||
|
||
Animated.timing(this.animation, { | ||
duration, | ||
toValue, | ||
ease: Easing.bounce, | ||
}).start(() => { | ||
this.animation.setValue(0); | ||
}); | ||
} | ||
|
||
render() { | ||
const { children, style } = this.props; | ||
const { interpolateConfig, paddingHorizontal } = style; | ||
|
||
const interpolated = this.animation.interpolate({ | ||
...interpolateConfig, | ||
}); | ||
const animatedStyle = { | ||
paddingHorizontal, | ||
transform: [{ translateX: interpolated }], | ||
}; | ||
|
||
return ( | ||
<Animated.View style={[style, animatedStyle]}>{children}</Animated.View> | ||
); | ||
} | ||
} | ||
|
||
Wiggle.propTypes = { | ||
/** | ||
* If set to true, triggers animation | ||
*/ | ||
startAnimation: PropTypes.bool, | ||
/** | ||
* Components to which an effect will be applied | ||
*/ | ||
children: PropTypes.node.isRequired, | ||
style: PropTypes.object, | ||
}; | ||
|
||
Wiggle.defaultProps = { | ||
startAnimation: false, | ||
}; |