# Rolling Number

A number whose changed digits roll to their new values: up when it rises, down when it falls. Counts, badges and stat figures use it, so a change is seen rather than just swapped.

- Beta: ready to use; its API can still change in a minor release.
- Category: Text

```tsx
import { RollingNumber } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it for a figure that can change while the reader watches, like a count of open opportunities or a dashboard total, so the change is seen.
- Do: Pass `format` for currency or units, or pass text like $12,400 or 99+. Signs and separators hold still while the digits roll.
- Do: Keep the same `RollingNumber` mounted as its value changes. A remounted one, like one under a new `key`, shows the new value without rolling.
- Don't: Use it for a number that identifies something, like a matter number or a phone number. It never rises or falls; show it as plain text.

## Related

- [Stat Card](./stat-card.md)
- [Badge](./badge.md)

## Examples

What the examples import:

```tsx
import { useState } from "react";
import { Button, RollingNumber } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
function Counter({ start, step }: { start: number; step: number }) {
  const [value, setValue] = useState(start);
  return (
    <div className="flex items-center gap-3">
      <span className="text-heading-lg text-brand-navy min-w-32 font-semibold">
        <RollingNumber value={value} />
      </span>
      <Button intent="outline" size="sm" onClick={() => setValue(value - step)}>
        Down
      </Button>
      <Button intent="outline" size="sm" onClick={() => setValue(value + step)}>
        Up
      </Button>
    </div>
  );
}

const money = (value: number) =>
  (value / 100).toLocaleString("en-AU", {
    style: "currency",
    currency: "AUD",
    maximumFractionDigits: 0,
  });

function Money() {
  const [cents, setCents] = useState(1_240_000);
  return (
    <div className="flex items-center gap-3">
      <span className="text-title text-neutral-text min-w-32 font-semibold">
        <RollingNumber value={cents} format={money} />
      </span>
      <Button
        intent="outline"
        size="sm"
        onClick={() => setCents(cents + 87_500)}
      >
        Add a fee
      </Button>
    </div>
  );
}
```

### Rising and falling

Only the digits that changed move, each a beat behind the one to its right. A new leading place opens its space smoothly, and digits are tabular, so nothing beside the number shifts.

```tsx
<div className="flex flex-col gap-4">
  <Counter start={96} step={3} />
  <Counter start={1180} step={45} />
</div>
```

### Formatted

Pass format for currency or units; signs and separators hold still while the digits roll. Screen readers hear the final value only.

```tsx
<Money />
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (required) | `number \| string` |  | The number, or its text like "$12,400" or "99+": every digit rolls, everything else holds still. |
| `format` | `(value: number) => string` | `(value: number) => Math.round(value).toLocaleString("en-AU")` | How a number reads as text. Whole numbers with thousands separators by default. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
