import { useEffect, useState } from 'react'; import { animated, useSpring, config } from '@react-spring/web'; import { reduceMotion } from '../initial_state'; import { ShortNumber } from './short_number'; interface Props { value: number; } export const AnimatedNumber: React.FC = ({ value }) => { const [previousValue, setPreviousValue] = useState(value); const direction = value > previousValue ? -1 : 1; const [styles, api] = useSpring( () => ({ from: { transform: `translateY(${100 * direction}%)` }, to: { transform: 'translateY(0%)' }, onRest() { setPreviousValue(value); }, config: { ...config.gentle, duration: 200 }, immediate: true, // This ensures that the animation is not played when the component is first rendered }), [value, previousValue], ); // When the value changes, start the animation useEffect(() => { if (value !== previousValue) { void api.start({ reset: true }); } }, [api, previousValue, value]); if (reduceMotion) { return ; } return ( {value !== previousValue && ( )} ); };