The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

48 lines
1.2 KiB

  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';
  7. export default class AnimatedNumber extends React.PureComponent {
  8. static propTypes = {
  9. value: PropTypes.number.isRequired,
  10. };
  11. willEnter () {
  12. return { y: -1 };
  13. }
  14. willLeave () {
  15. return { y: spring(1, { damping: 35, stiffness: 400 }) };
  16. }
  17. render () {
  18. const { value } = this.props;
  19. if (reduceMotion) {
  20. return <FormattedNumber value={value} />;
  21. }
  22. const styles = [{
  23. key: value,
  24. style: { y: spring(0, { damping: 35, stiffness: 400 }) },
  25. }];
  26. return (
  27. <TransitionMotion styles={styles} willEnter={this.willEnter} willLeave={this.willLeave}>
  28. {items => (
  29. <span className='animated-number'>
  30. {items.map(({ key, style }) => (
  31. <span key={key} style={{ position: style.y > 0 ? 'absolute' : 'static', transform: `translateY(${style.y * 100}%)` }}><FormattedNumber value={key} /></span>
  32. ))}
  33. </span>
  34. )}
  35. </TransitionMotion>
  36. );
  37. }
  38. }