Rolling number
Digits that roll to their new value.
Demo
Usage
import { RollingNumber } from '@tuttn/slant'
// It shows the value you pass; a new value rolls each changed digit.
<RollingNumber value={visitors} />
// Anything Intl.NumberFormat writes: a currency, a percent, compact notation.
<RollingNumber value={revenue} format={{ style: 'currency', currency: 'EUR' }} locale="de-DE" />
<RollingNumber value={0.412} format={{ style: 'percent', maximumFractionDigits: 1 }} />
<RollingNumber value={1_284_000} format={{ notation: 'compact' }} />
// A slower roll, and an announcement when it changes (it is silent by default).
<RollingNumber value={score} duration={600} aria-live="polite" />
// As the figure of a stat.
<Stat label="Sessions" value={<RollingNumber value={sessions} />} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | — | The number to show. A new value rolls each changed digit to its place: up when the number rises, down when it falls. |
format | Intl.NumberFormatOptions | — | How to write the number, as for `Intl.NumberFormat`: a currency, a percent, compact notation, a fixed number of decimals. |
locale | string | the runtime's own locale | The locale the number is written in, e.g. `de-DE`. Pass one when the page is rendered on a server, so the server and the browser write the same thing. |
duration | number | the --sl-duration-slow token (300ms) | How long a roll takes, in milliseconds. |
…rest | HTMLAttributes<HTMLSpanElement> | — | className, style (the font, say), aria-live, other attributes and the ref go on the root span. |
A rolling number is read-only: it shows the value you pass and keeps no state. Each digit is a column one line tall over a strip of the ten digits; a change moves every column whose digit changed, up when the number rises and down when it falls, wrapping past 9 and 0 the way an odometer does. Columns that appear (99 to 100) open and slide in, and columns that vanish close up and slide out, so the text around the number glides rather than jumps. Separators, the sign, the currency and the percent sign are plain text beside the columns.
It takes the surrounding font and size, with tabular digits so the columns keep one width; set the font on it or on its parent (mono suits a figure). The number is written by Intl.NumberFormat with format and locale, in that locale's own digits and direction. Without a locale it uses the runtime's, which can differ between the server and the browser, so pass one on a page rendered on a server.
Accessibility
Screen readers get one thing: the formatted final value, in a visually hidden span. The rolling columns are aria-hidden. It is not a live region, so a change is silent; add aria-live="polite" where the change itself is news (a score, a total). There is nothing to focus or press.
Motion
A roll lasts duration milliseconds, the --sl-duration-slow token by default, on the --sl-ease-out curve. A new value during a roll picks up from where the columns are. Under prefers-reduced-motion, off screen and in a hidden tab, the new value appears at once. For a figure with a label, see Stat and StatCard.