← Components

Communication

Rolling number

A number whose digits roll to new values like an odometer. Each digit column rolls on its own spring, staggered from the right.

Examples and variants

Counter

Add and subtract; watch each column roll.

12,480
Example
function RollingCounter() {
  const [value, setValue] = useState(12480)
  return (
    <div className="flex flex-col items-center gap-3">
      <RollingNumber value={value} className="text-[length:var(--type-headline-size)] font-semibold" />
      <div className="flex gap-2">
        <Button size="sm" variant="secondary" onClick={() => setValue((v) => v - 1)}>
          −1
        </Button>
        <Button size="sm" variant="secondary" onClick={() => setValue((v) => v + 137)}>
          +137
        </Button>
        <Button size="sm" variant="secondary" onClick={() => setValue((v) => v + 9000)}>
          +9,000
        </Button>
      </div>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/rolling-number.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/rolling-number.tsxrolling-number
      components/ui/rolling-number.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      
      /*
        Rolling number: a number whose digits roll to new values like an odometer.
        Each digit column rolls on its own spring, staggered from the right.
        Separators stay put. Screen readers hear the final value only.
      */
      function RollingNumber({
        value,
        format,
        locale,
        className,
      }: {
        value: number
        /** Intl.NumberFormat options (currency, compact, decimals). */
        format?: Intl.NumberFormatOptions
        locale?: string
        className?: string
      }) {
        const text = new Intl.NumberFormat(locale, format).format(value)
        const chars = text.split("")
        return (
          <span data-slot="rolling-number" className={cn("inline-flex tabular-nums", className)}>
            <span aria-hidden className="inline-flex overflow-hidden">
              {chars.map((char, i) => {
                const fromRight = chars.length - 1 - i
                if (!/\d/.test(char)) {
                  return (
                    <span key={`s-${fromRight}`} className="rolling-sep">
                      {char}
                    </span>
                  )
                }
                const digit = Number(char)
                return (
                  <span key={`d-${fromRight}`} className="relative inline-block h-[1lh] overflow-hidden">
                    <span
                      className="rolling-column flex flex-col"
                      style={{ translate: `0 ${-digit}lh`, transitionDelay: `calc(var(--duration-stagger) * ${fromRight})` }}
                    >
                      {Array.from({ length: 10 }, (_, n) => (
                        <span key={n} className="h-[1lh]">
                          {n}
                        </span>
                      ))}
                    </span>
                  </span>
                )
              })}
            </span>
            <span className="sr-only">{text}</span>
          </span>
        )
      }
      
      export { RollingNumber }
      

Imports

What the examples on this page import:

Imports
import {
import { ActivityStack, type Activity } from "@/components/ui/activity-stack"
import { Button } from "@/components/ui/button"
import { Dropzone } from "@/components/ui/dropzone"
import { DynamicIsland } from "@/components/ui/dynamic-island"
import { Progress } from "@/components/ui/progress"
import { RollingNumber } from "@/components/ui/rolling-number"
import { Spinner } from "@/components/ui/spinner"
import { Toggle } from "@/components/ui/toggle"
import { tokenMs } from "@/lib/motion/token-ms"

Anatomy

  1. 1Digit columns · 0 to 9 stacked; translated to the digit.
  2. 2Separators · Commas and symbols stay put.

Props

PropTypeDefaultDescription
valuenumber—The number to show.
formatIntl.NumberFormatOptions—Currency, compact, decimals.
localestring—Formatting locale.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

This utility has no component tokens; it uses semantic tokens directly.

TokenPoints toLightDark

Motion

TriggerWhat happensTokens
Value changeEach column rolls on a spring, right to left.--spring-snappy, --duration-stagger

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Screen readers read the final formatted value only.

Usage

Do

  • Use for counts, totals, prices that change live.

Don’t

  • Don't use for numbers people need to copy mid-change.

Code

components/ui/rolling-number.tsx
"use client"

import * as React from "react"
import { cn } from "cn"

/*
  Rolling number: a number whose digits roll to new values like an odometer.
  Each digit column rolls on its own spring, staggered from the right.
  Separators stay put. Screen readers hear the final value only.
*/
function RollingNumber({
  value,
  format,
  locale,
  className,
}: {
  value: number
  /** Intl.NumberFormat options (currency, compact, decimals). */
  format?: Intl.NumberFormatOptions
  locale?: string
  className?: string
}) {
  const text = new Intl.NumberFormat(locale, format).format(value)
  const chars = text.split("")
  return (
    <span data-slot="rolling-number" className={cn("inline-flex tabular-nums", className)}>
      <span aria-hidden className="inline-flex overflow-hidden">
        {chars.map((char, i) => {
          const fromRight = chars.length - 1 - i
          if (!/\d/.test(char)) {
            return (
              <span key={`s-${fromRight}`} className="rolling-sep">
                {char}
              </span>
            )
          }
          const digit = Number(char)
          return (
            <span key={`d-${fromRight}`} className="relative inline-block h-[1lh] overflow-hidden">
              <span
                className="rolling-column flex flex-col"
                style={{ translate: `0 ${-digit}lh`, transitionDelay: `calc(var(--duration-stagger) * ${fromRight})` }}
              >
                {Array.from({ length: 10 }, (_, n) => (
                  <span key={n} className="h-[1lh]">
                    {n}
                  </span>
                ))}
              </span>
            </span>
          )
        })}
      </span>
      <span className="sr-only">{text}</span>
    </span>
  )
}

export { RollingNumber }