Digit Swap

Fixed-slot digits that roll on change, with direction and stagger control.

Preview

"use client";

import { useState } from "react";
import { DigitSwap } from "@/components/motion/digit-swap";

const CARD_NUMBER = "4242 4242 4242 0806";
const MASKED_NUMBER = "•••• •••• •••• 0806";

export function DigitSwapPreview() {
  const [revealed, setRevealed] = useState(false);

  return (
    <div className="flex w-80 flex-col gap-5">
      <div className="flex flex-col gap-2">
        <span className="text-xs font-medium text-muted-foreground">
          Card number
        </span>
        <DigitSwap
          value={revealed ? CARD_NUMBER : MASKED_NUMBER}
          animationKey={revealed ? "revealed" : "masked"}
          direction={revealed ? "up" : "down"}
          suffixLength={4}
          glyphClassName={
            revealed ? "text-foreground" : "text-muted-foreground"
          }
          suffixClassName="text-foreground"
          className="font-mono text-lg tracking-[0.08em] tabular-nums"
        />
      </div>

      <button
        type="button"
        aria-label={revealed ? "Animate masked number" : "Animate card number"}
        aria-pressed={revealed}
        onClick={() => setRevealed((current) => !current)}
        className="h-10 self-start rounded-full border border-border px-5 text-sm font-medium text-foreground outline-none transition-colors hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring"
      >
        Animate
      </button>
    </div>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/digit-swap

API Reference

value
string | number

Numeric or masked value rendered in fixed character slots.

Default—
animationKey?
string | number

Replays every glyph when the value itself contains unchanged characters.

Default—
direction?
"up" | "down"

Direction the next glyph enters from.

Defaultup
duration?
number

Per-glyph transition duration in seconds.

Default0.18
stagger?
number

Delay in seconds between neighboring glyphs.

Default0.006
suffixLength?
number

Number of final characters that receive `suffixClassName`.

Default0
className?
string

Classes merged onto the root element.

Default—
glyphClassName?
string

Classes merged onto every animated character.

Default—
suffixClassName?
string

Classes added to the last `suffixLength` characters.

Default—
Also on React Native. Rolling tickers and count-ups.

More in Number Animation

Updated