A number whose digits roll to their new value, like an odometer.
Installation
Usage
import { SlidingNumber } from '@animate-ui/react/primitives/texts/sliding-number';
export function Price({ value }: { value: number }) {
return <SlidingNumber number={value} thousandSeparator="," />;
}Animation
- Each digit sits on a wheel that springs to its new value (
stiffness: 200, damping: 20, mass: 0.4), straight through the digits in between: going from 9 to 0, it rolls back through 8, 7… - With
fromNumber, the number counts tonumberon a spring (stiffness: 90, damping: 50), every digit rolling on the way; a newnumberthen counts on from the current value. Without it, the digits roll straight to their new value. - Digits are positioned in percentages of their own height: no measuring, right on the first frame and when the font size changes. Counting doesn't re-render React per frame.
<AnimateUIProvider tempo>paces the default springs.- Under reduced motion, the digits change without rolling.
- Screen readers read the number once, not every rolled digit.
API Reference
SlidingNumber
| Prop | Type | Default |
|---|---|---|
number | number | - |
fromNumber? | number | - |
onNumberChange? | (number: number) => void | - |
padStart? | boolean | false |
decimalSeparator? | string | '.' |
decimalPlaces? | number | 0 |
thousandSeparator? | string | - |
transition? | SpringOptions | { stiffness: 200, damping: 20, mass: 0.4 } |
delay? | number | 0 |
initiallyStable? | boolean | false |
inView? | boolean | false |
inViewMargin? | string | '0px' |
inViewOnce? | boolean | true |
...props? | Omit<HTMLMotionProps<'span'>, 'children'> | - |