Playground

Rolling number

A number whose digits roll to the new value when it changes. Screen readers read the final value.

  • 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

Currency

Digits keep their column as the number grows or shrinks.

Props

RollingNumberProps

PropTypeDefaultDescription
value requirednumber–The number to show. When it changes, each digit rolls to its new value.
localestring–BCP 47 locale for the digits and separators, such as "de-DE". Default the browser's locale.
formatIntl.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

ClassStylesheet
.rolling-numberrolling-number.css
.rolling-number-charrolling-number.css
.rolling-number-digitrolling-number.css
.rolling-number-striprolling-number.css
.rolling-number-visualrolling-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.