Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

Commit

Permalink
Toolbar Icon badge now animates on value change. Set animate: false
Browse files Browse the repository at this point in the history
… to disable.
  • Loading branch information
Salakar committed Jan 6, 2016
1 parent c848693 commit c8f762e
Showing 1 changed file with 62 additions and 14 deletions.
76 changes: 62 additions & 14 deletions lib/IconToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export default class IconToggle extends Component {
};
}

componentDidUpdate(prevProps) {
if (this.props && this.props.badge) {
if (this.props.badge.value !== prevProps.badge.value) {
this._animateBadgeCounter();
}
}
};

static propTypes = {
color: PropTypes.string.isRequired,
opacity: PropTypes.number,
Expand All @@ -28,6 +36,7 @@ export default class IconToggle extends Component {
children: PropTypes.element.isRequired,
badge: PropTypes.shape({
value: PropTypes.number,
animate: PropTypes.bool,
backgroundColor: PropTypes.string,
textColor: PropTypes.string
})
Expand All @@ -36,7 +45,7 @@ export default class IconToggle extends Component {
static defaultProps = {
opacity: .1,
disabled: false,
percent: 90,
percent: 90
};

setSize = (event) => {
Expand Down Expand Up @@ -65,18 +74,20 @@ export default class IconToggle extends Component {

if (badge && typeof badge.value === 'number') {
badge = Object.assign({},
{ value: badge.value },
badge.backgroundColor ? { backgroundColor: badge.backgroundColor } : { backgroundColor: 'paperRed' },
badge.textColor ? { textColor: badge.textColor } : { textColor: '#ffffff' }
{value: badge.value},
badge.backgroundColor ? {backgroundColor: badge.backgroundColor} : {backgroundColor: 'paperRed'},
badge.textColor ? {textColor: badge.textColor} : {textColor: '#ffffff'}
);
}

this.badgeAnim = this.badgeAnim || new Animated.Value(0);

return (
<View {...this._responder}>
<View>
{size &&
<Animated.View
style={[{
<Animated.View
style={[{
position: 'absolute',
left: ((1 - percent) * size) / 2,
top: ((1 - percent) * size) / 2,
Expand All @@ -87,23 +98,43 @@ export default class IconToggle extends Component {
opacity: opacityValue,
backgroundColor: getColor(color)
}]}
/>
/>
}
<View onLayout={(event) => { this.setSize(event) }}>
{children}
</View>
{size && badge && typeof badge.value === 'number' &&
<View style={[
<Animated.View style={[
styles.badgeContainer, {
backgroundColor: getColor(badge.backgroundColor),
top: size / 10,
right: size / 10
right: size / 10,
transform: [ // Array order matters
{scale: this.badgeAnim.interpolate({
inputRange: [0, 1],
outputRange: [1, 1.7]
})},
{translateX: this.badgeAnim.interpolate({
inputRange: [0, 1],
outputRange: [0, -12]
})},
{translateY: this.badgeAnim.interpolate({
inputRange: [0, 1],
outputRange: [0, 10]
})},
{rotate: this.badgeAnim.interpolate({
inputRange: [0, 1],
outputRange: [
'0deg', '45deg' // 'deg' or 'rad'
]
})}
]
}]}
>
<Text style={[styles.badgeText, { color: getColor(badge.textColor) }]}>
{badge.value}
</Text>
</View>
>
<Text style={[styles.badgeText, { color: getColor(badge.textColor) }]}>
{badge.value}
</Text>
</Animated.View>
}
</View>
</View>
Expand All @@ -127,6 +158,7 @@ export default class IconToggle extends Component {
}
};


/**
*
* @private
Expand All @@ -143,6 +175,22 @@ export default class IconToggle extends Component {
}
};

/**
*
* @private
*/
_animateBadgeCounter = () => {
if (this.badgeAnim && this.props.badge && this.props.badge.animate !== false) {
Animated.spring(this.badgeAnim, {
toValue: 0, // Returns to the start
velocity: 8, // Velocity makes it move
tension: -5, // Slow
friction: 1, // Oscillate a lot
duration: 300
}).start();
}
};

/**
*
* @private
Expand Down

1 comment on commit c8f762e

@Salakar
Copy link
Member Author

@Salakar Salakar commented on c8f762e Jan 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See demo app for preview. Right toolbar icon set to animate.

Please sign in to comment.