Skip to content

Instantly share code, notes, and snippets.

@aalin
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalin/c5152ffbd73ee1f3ce5f to your computer and use it in GitHub Desktop.
Save aalin/c5152ffbd73ee1f3ce5f to your computer and use it in GitHub Desktop.
var TweeningNumber = React.createClass({
mixins: [tweenState.Mixin],
getInitialProps: function() {
return {
value: 0,
duration: 500
};
},
getInitialState: function() {
return { value: 0 };
},
componentWillReceiveProps: function(nextProps) {
if(nextProps.value && nextProps.value !== this.state.value) {
this.tweenState('value', {
easing: tweenState.easingTypes.easeInOutQuad,
duration: this.props.duration,
delay: 0,
endValue: nextProps.value
});
}
},
render: function() {
var value = this.getTweeningValue('value').toFixed(2);
var digits = value.toString().split('').map(function(digit, index) {
var klass = (digit === '.') ? 'dot': 'digit';
return <span key={index} className={klass}>{digit}</span>;
}, this);
return (
<span className="tweening-number">{digits}</span>
);
}
});
.tweening-number {
.digit {
display: inline-block;
width: .6em;
}
.dot {
display: inline-block;
width: .2em;
}
}
var usage = (
<TweeningNumber value={this.state.amount} />
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment