You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.0 KiB
77 lines
2.0 KiB
5 years ago
|
import React from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
2 years ago
|
import ShortNumber from 'mastodon/components/short_number';
|
||
5 years ago
|
import TransitionMotion from 'react-motion/lib/TransitionMotion';
|
||
|
import spring from 'react-motion/lib/spring';
|
||
2 years ago
|
import { reduceMotion } from 'flavours/glitch/initial_state';
|
||
5 years ago
|
|
||
4 years ago
|
const obfuscatedCount = count => {
|
||
|
if (count < 0) {
|
||
|
return 0;
|
||
|
} else if (count <= 1) {
|
||
|
return count;
|
||
|
} else {
|
||
|
return '1+';
|
||
|
}
|
||
|
};
|
||
|
|
||
5 years ago
|
export default class AnimatedNumber extends React.PureComponent {
|
||
|
|
||
|
static propTypes = {
|
||
|
value: PropTypes.number.isRequired,
|
||
4 years ago
|
obfuscate: PropTypes.bool,
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
state = {
|
||
|
direction: 1,
|
||
|
};
|
||
|
|
||
|
componentWillReceiveProps (nextProps) {
|
||
|
if (nextProps.value > this.props.value) {
|
||
|
this.setState({ direction: 1 });
|
||
|
} else if (nextProps.value < this.props.value) {
|
||
|
this.setState({ direction: -1 });
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
willEnter = () => {
|
||
|
const { direction } = this.state;
|
||
|
|
||
|
return { y: -1 * direction };
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
|
willLeave = () => {
|
||
|
const { direction } = this.state;
|
||
|
|
||
|
return { y: spring(1 * direction, { damping: 35, stiffness: 400 }) };
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
|
render () {
|
||
4 years ago
|
const { value, obfuscate } = this.props;
|
||
5 years ago
|
const { direction } = this.state;
|
||
5 years ago
|
|
||
|
if (reduceMotion) {
|
||
2 years ago
|
return obfuscate ? obfuscatedCount(value) : <ShortNumber value={value} />;
|
||
5 years ago
|
}
|
||
|
|
||
|
const styles = [{
|
||
5 years ago
|
key: `${value}`,
|
||
|
data: value,
|
||
5 years ago
|
style: { y: spring(0, { damping: 35, stiffness: 400 }) },
|
||
|
}];
|
||
|
|
||
|
return (
|
||
|
<TransitionMotion styles={styles} willEnter={this.willEnter} willLeave={this.willLeave}>
|
||
|
{items => (
|
||
|
<span className='animated-number'>
|
||
5 years ago
|
{items.map(({ key, data, style }) => (
|
||
2 years ago
|
<span key={key} style={{ position: (direction * style.y) > 0 ? 'absolute' : 'static', transform: `translateY(${style.y * 100}%)` }}>{obfuscate ? obfuscatedCount(data) : <ShortNumber value={data} />}</span>
|
||
5 years ago
|
))}
|
||
|
</span>
|
||
|
)}
|
||
|
</TransitionMotion>
|
||
|
);
|
||
|
}
|
||
|
|
||
|
}
|