# Number roll

> Numbers that change like an odometer: every digit rolls in the direction the number moved.

A number that jumps from one value to the next throws away the most satisfying moment on the page. Here each digit is its own column and rolls to its new value, the way the number actually moved: going up, a 9 rolls on to 0 rather than spinning back down. New places slide open from nothing and lost places fold away, so 999 becoming 1,000 grows a digit and a comma in front of you. It formats with Intl.NumberFormat, so currency, percent, decimals and compact numbers all roll.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/number-roll.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/number-roll
- Registry item (source): https://hairlineui.com/r/number-roll.json

## Usage

```tsx
import { NumberRoll } from "@/components/number-roll";

<NumberRoll value={downloads} />
<NumberRoll value={price} format={{ style: "currency", currency: "USD", maximumFractionDigits: 0 }} />
```

## In a live metric

Give it a new value whenever yours changes; it works out which digits roll and which way.

```tsx
"use client";
import { useEffect, useState } from "react";
import { NumberRoll } from "@/components/number-roll";

export function LiveSignups() {
  const [count, setCount] = useState<number>(initialCount);

  useEffect(() => {
    const events = new EventSource("/api/signups/stream");
    events.onmessage = (e) => setCount(Number(e.data));
    return () => events.close();
  }, []);

  return (
    <p className="text-5xl font-medium tracking-tight">
      <NumberRoll value={count} /> <span className="text-base text-muted-foreground">founders on the list</span>
    </p>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `number` | – | The number to show; change it and the digits roll. |
| `format / locales` | `Intl.NumberFormatOptions / string \| string[]` | – | Currency, percent, decimals, compact ("9.4K") and grouping, as Intl.NumberFormat does them. Without locales it formats in the visitor’s locale; pass one (e.g. from the request) to use it on the server and in the browser alike. |
| `prefix / suffix` | `string` | – | Text around the number, e.g. "+" or " users". |
| `from` | `number` | – | Start here and roll to value once painted. |
| `duration` | `number` | `900` | Roll time in ms. |
| `direction` | `"up" \| "down"` | – | Always roll this way. Use "down" for a countdown’s seconds, so 00 → 59 rolls back like a clock instead of spinning forward. |

## States

- **rolling**: Each changed digit rolls up or down with the number, easing out over 0.9s; unchanged digits stay perfectly still.
- **growing**: A new place (and its comma) slides open from zero width, and its digit rolls up from 0.
- **shrinking**: A place that is no longer needed folds to nothing on the left.
- **from**: With from, it starts at that value and rolls to the real one as soon as it’s painted, e.g. stats from 0.

## What it handles

A number that jumps from one value to the next throws away the most satisfying moment on the page. This rolls each digit the way the number actually moved, and many other components in the library are built on it.

- **It rolls the way it moved**: Going up, a 9 rolls on to 0 rather than spinning back down. Unchanged digits stay perfectly still.
- **Places that open and fold away**: 999 becoming 1,000 opens a new digit and a comma in front of you.
- **Any format Intl.NumberFormat supports**: Currency, percent, decimals, compact numbers like 9.4K, and grouping in any locale.
- **Count up from a start**: Set from, and it starts there and rolls to the real value as soon as it is painted.
- **Countdowns that read like a clock**: direction="down" makes 00 roll back to 59 instead of spinning forward.
- **Real text underneath**: Screen readers, search and copy and paste get the full number. Digits use tabular figures so nothing shifts sideways.

## Replaces

a counter animation library, odometer CSS, Intl.NumberFormat wiring, tabular figure styling, a screen reader fallback

## Where it fits

- **Pricing pages**: Prices that roll when someone switches between monthly and yearly billing.
- **Live dashboards**: Metrics that change while people are looking at them.
- **Landing page stats**: Numbers that count up when they scroll into view.
- **Countdowns and timers**: Seconds and minutes that roll back like a clock.

## Notes

- The full number is real text for screen readers, search and copy and paste; the rolling columns are hidden from assistive tech.
- Digits use tabular figures, so nothing shifts sideways while it rolls.
- No animation library: each column is a CSS transform, re-centred quietly after each roll.
- With reduced motion, digits change in place without rolling.
- Safe to server-render: without locales, the server and the hydrating render both format in en-US so the markup matches (Node and a browser can disagree, e.g. "$29" against "US$29"), then the visitor’s locale takes over in place, without a roll. Rendered only in the browser, it uses the visitor’s locale from the first paint. Pass locales to skip the switch.

License: MIT (https://hairlineui.com/license#components)
