Rolling number
A number whose digits roll to the new value when it changes. Screen readers read the final value.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- Built on native elements
- The animated digits are aria-hidden; the formatted final value is plain text for screen readers.
- With prefers-reduced-motion the digits change without rolling.
- Not interactive: no focus stops.
- Styles in one file:
rolling-number.css
Import
import '@monoline/css';
import {RollingNumber} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add rolling-number
Usage
import {useState} from 'react';
import {Button, RollingNumber} from '@monoline/react';
function Counter() {
const [n, setN] = useState(1204);
return (
<div style={{display: 'flex', gap: 'var(--s4)', alignItems: 'center'}}>
<strong style={{fontSize: 'var(--display-sm)'}}><RollingNumber value={n} format={{style: 'currency', currency: 'USD', maximumFractionDigits: 0}} /></strong>
<Button variant="line" size="sm" onClick={() => setN((v) => v + 137)}>Add 137</Button>
<Button variant="line" size="sm" onClick={() => setN((v) => Math.max(0, v - 999))}>Subtract 999</Button>
</div>
);
}
<Counter />
Examples
Props
RollingNumberProps
| Prop | Type | Default | Description |
|---|---|---|---|
value required | number | – | The number to show. When it changes, each digit rolls to its new value. |
locale | string | – | BCP 47 locale for the digits and separators, such as "de-DE". Default the browser's locale. |
format | Intl.NumberFormatOptions | – | `Intl.NumberFormat` options, for example `{style: 'currency', currency: 'USD'}`. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
This component sets no state attributes.
CSS classes
| Class | Stylesheet |
|---|---|
.rolling-number | rolling-number.css |
.rolling-number-char | rolling-number.css |
.rolling-number-digit | rolling-number.css |
.rolling-number-strip | rolling-number.css |
.rolling-number-visual | rolling-number.css |
Customization
Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.
app.css
.rolling-number {
border-color: var(--accent);
}
Try the props
Change a prop and the component above updates; the code below is what you would write. To edit the code itself, use the Playground.
1,204
example.tsx
<RollingNumber value={1204} locale="en-US" />
Accessibility
- The animated digits are aria-hidden; the formatted final value is plain text for screen readers.
- With prefers-reduced-motion the digits change without rolling.
- Not interactive: no focus stops.