The code powering m.abunchtell.com https://m.abunchtell.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

animated_number.js 1.2 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. }