A receipt that prints itself: odometer numbers and torn paper in React

How our spend management landing page prints a receipt line by line, rolls totals on odometer digits and tears its edges, with React, Tailwind and Motion.

6 min read

The real component. Click the picture to open it live and try it before you read how it was built.

A spend tool's job is receipts, so the page is made of them. The hero is a thermal printer feeding out a receipt while its total adds up.

Printing, one line at a time

The receipt is a list whose visible length grows on a timer; each new line slides up from the printer's slot. When it finishes it rests a moment, then a fresh receipt starts. With reduced motion you see the finished receipt.

src/registry/landing-pages/tally/printing-receipt.tsx

"use client";

import { useRef } from "react";
import { AnimatePresence, motion, useInView } from "motion/react";
import { EASE_OUT } from "@/lib/motion";
import { useAutoCycle } from "@/hooks/use-auto-cycle";
import { usePrefersReducedMotion } from "@/hooks/use-prefers-reduced-motion";
import { formatDollars } from "./format";
import { TornEdge } from "./torn-edge";
import type { ReceiptLine } from "./types";

const LINE_MS = 650;
// Beats the finished receipt rests before a fresh one starts printing.
const BEATS_AFTER_LINES = 5;

interface PrintingReceiptProps {
  merchant: string;
  lines: ReceiptLine[];
}

// A receipt printing out of a thermal printer's slot, one line at a time, while its total adds up; it rests
// a moment, then a fresh receipt starts. Reduced motion shows the finished receipt.
export function PrintingReceipt({ merchant, lines }: PrintingReceiptProps) {
  const receiptRef = useRef<HTMLDivElement>(null);
  const isInView = useInView(receiptRef, { amount: 0.4 });
  const shouldReduceMotion = usePrefersReducedMotion();
  const [beat] = useAutoCycle(lines.length + BEATS_AFTER_LINES, LINE_MS, isInView && !shouldReduceMotion);
  const printed = shouldReduceMotion ? lines.length : Math.min(beat + 1, lines.length);
  const total = lines.slice(0, printed).reduce((sum, line) => sum + line.cents, 0);

  return (
    <div ref={receiptRef} className="w-full max-w-sm font-fragment text-xs text-zinc-800">
      <div aria-hidden className="relative z-10 h-4 rounded-full bg-zinc-950 shadow-lg" />
      <div className="relative mx-3 -mt-2 bg-white px-6 pt-6 pb-4 shadow-xl">
        <p className="text-center tracking-widest text-zinc-500">{merchant}</p>
        <ul aria-label="Purchases" className="mt-4 flex flex-col gap-2 border-y border-dotted border-zinc-300 py-4">
          <AnimatePresence initial={false}>
            {lines.slice(0, printed).map((line) => (
              <motion.li
                key={line.label}
                initial={{ opacity: 0, y: -8 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ duration: 0.3, ease: EASE_OUT }}
                className="flex justify-between gap-4"
              >
                <span className="truncate">{line.label}</span>
                <span className="tabular-nums">{formatDollars(line.cents)}</span>
              </motion.li>
            ))}
          </AnimatePresence>
        </ul>
        <p className="mt-3 flex justify-between text-sm font-bold">
          <span>TOTAL</span>
          <span className="tabular-nums">{formatDollars(total)}</span>
        </p>
      </div>
      <TornEdge className="mx-3 text-white" />
    </div>
  );
}
The printer and its receipt

Numbers on rollers

Totals are odometers: each digit is a column of 0 to 9 translated up to its value, left to right, so the number lands like a till's tally. Symbols such as $ and % stay fixed, and screen readers get the plain value.

src/registry/landing-pages/tally/odometer.tsx

"use client";

import { motion } from "motion/react";
import { EASE_OUT } from "@/lib/motion";

const DIGITS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const DIGIT_STEP_PERCENT = 10;
const COLUMN_STAGGER_S = 0.08;

interface OdometerProps {
  value: string;
  // While false every digit rests on 0; turning true rolls each one up to its value.
  isRolling: boolean;
}

// A number on mechanical rollers, like a till's tally: each digit rolls up through 0โ€“9 to land in place, left
// to right. Symbols such as $ or % stay fixed. Screen readers get the value itself.
export function Odometer({ value, isRolling }: OdometerProps) {
  return (
    <span className="inline-flex h-10 items-start overflow-hidden font-fragment text-4xl leading-10 text-zinc-950 tabular-nums sm:h-15 sm:text-6xl sm:leading-15 lg:h-12 lg:text-5xl lg:leading-12">
      <span className="sr-only">{value}</span>
      {[...value].map((char, index) => {
        const digit = Number(char);
        if (char === " " || Number.isNaN(digit)) {
          return (
            <span key={index} aria-hidden>
              {char}
            </span>
          );
        }
        return (
          <motion.span
            key={index}
            aria-hidden
            className="flex flex-col"
            initial={false}
            animate={{ y: `-${(isRolling ? digit : 0) * DIGIT_STEP_PERCENT}%` }}
            transition={{
              duration: 1.4,
              delay: index * COLUMN_STAGGER_S,
              ease: EASE_OUT,
            }}
          >
            {DIGITS.map((option) => (
              <span key={option}>{option}</span>
            ))}
          </motion.span>
        );
      })}
    </span>
  );
}
The odometer

One idea, every section

Features are itemised on a long receipt, reviews hang on a desk spike, plans are receipts, the FAQ is fine print and the call to action is a coupon you tear off. One metaphor carried through makes a template feel designed instead of assembled.

More articles