]>
cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/components/animated_number.js
1 import React
from 'react';
2 import PropTypes
from 'prop-types';
3 import { FormattedNumber
} from 'react-intl';
4 import TransitionMotion
from 'react-motion/lib/TransitionMotion';
5 import spring
from 'react-motion/lib/spring';
6 import { reduceMotion
} from 'mastodon/initial_state';
8 const obfuscatedCount
= count
=> {
11 } else if (count
<= 1) {
18 export default class AnimatedNumber
extends React
.PureComponent
{
21 value: PropTypes
.number
.isRequired
,
22 obfuscate: PropTypes
.bool
,
29 componentWillReceiveProps (nextProps
) {
30 if (nextProps
.value
> this.props
.value
) {
31 this.setState({ direction: 1 });
32 } else if (nextProps
.value
< this.props
.value
) {
33 this.setState({ direction: -1 });
38 const { direction
} = this.state
;
40 return { y: -1 * direction
};
44 const { direction
} = this.state
;
46 return { y: spring(1 * direction
, { damping: 35, stiffness: 400 }) };
50 const { value
, obfuscate
} = this.props
;
51 const { direction
} = this.state
;
54 return obfuscate
? obfuscatedCount(value
) : <FormattedNumber value
={value
} />;
60 style: { y: spring(0, { damping: 35, stiffness: 400 }) },
64 <TransitionMotion styles
={styles
} willEnter
={this.willEnter
} willLeave
={this.willLeave
}>
66 <span className
='animated-number'>
67 {items
.map(({ key
, data
, style
}) => (
68 <span key
={key
} style
={{ position: (direction
* style
.y
) > 0 ? 'absolute' : 'static', transform: `translateY(${style.y * 100}%)` }}>{obfuscate
? obfuscatedCount(data
) : <FormattedNumber value
={data
} />}</span>
This page took 0.098296 seconds and 6 git commands to generate.