A wheel of numbers that spins to its value and settles under a highlight.
Installation
Usage
import {
ScrollingNumber,
ScrollingNumberContainer,
ScrollingNumberHighlight,
ScrollingNumberItems,
} from '@animate-ui/react/primitives/texts/scrolling-number';
export function Score() {
return (
<ScrollingNumberContainer number={1000} step={100} itemsSize={40}>
<ScrollingNumber>
<ScrollingNumberItems />
</ScrollingNumber>
<ScrollingNumberHighlight />
</ScrollingNumberContainer>
);
}Animation
- After
delay, the strip spins through every step up tonumberon a spring (stiffness: 90, damping: 30): fast through the range, then it settles without overshoot. - A new
numbersprings on from where the strip is. - The strip is written by a motion value, without a React render per frame.
onNumberChangereports the value under the highlight as it passes. <AnimateUIProvider tempo>paces the default spring.- Under reduced motion, the wheel lands on the number right away.
- The strip is hidden from screen readers, which read the number once from the container.
API Reference
ScrollingNumberContainer
| Prop | Type | Default |
|---|---|---|
number | number | - |
step | number | - |
itemsSize? | number | 30 |
sideItemsCount? | number | 2 |
direction? | 'btt' | 'ttb' | 'ltr' | 'rtl' | 'btt' |
onNumberChange? | (value: number) => void | - |
inView? | boolean | false |
inViewMargin? | string | '0px' |
inViewOnce? | boolean | true |
...props? | React.ComponentProps<'div'> | - |
ScrollingNumber
| Prop | Type | Default |
|---|---|---|
delay? | number | 0 |
onCompleted? | () => void | - |
transition? | Transition | { stiffness: 90, damping: 30 } |
...props? | HTMLMotionProps<'div'> | - |
ScrollingNumberItems
| Prop | Type | Default |
|---|---|---|
...props? | Omit<React.ComponentProps<'div'>, 'children'> | - |
ScrollingNumberHighlight
| Prop | Type | Default |
|---|---|---|
...props? | React.ComponentProps<'div'> | - |
useScrollingNumber
Returns the container's state inside it (number, step, range, direction, isInView…).