Toast notifications in React, printed like a receipt (2027)

Build toast notifications in React that a small receipt printer feeds out on paper in jerks, with the real time on each, earlier ones riding up the strip and the oldest dropping off.

5 min read

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

Toasts are needed in every app, and every library's look the same. These print: each notice feeds out of a small receipt printer on paper.

Paper feeds in jerks

A real printer doesn't glide; it feeds in steps. The strip grows in a fixed number of small jumps, which reads instantly as a printer and costs nothing to animate.

src/registry/interactive/receipt/receipt-printer.tsx

"use client";

import { AnimatePresence, motion } from "motion/react";
import { X } from "lucide-react";
import { cn } from "@/lib/utils";

// A printer feeds paper in jerks, not in a glide: the strip grows in this many steps.
const FEED_STEPS = 7;
const feed = (progress: number) => Math.round(progress * FEED_STEPS) / FEED_STEPS;
// The torn top edge of the paper, as a row of teeth.
const TEETH = Array.from({ length: 24 }, (_, index) => `${index * 10},8 ${index * 10 + 5},0`).join(" ");

export interface ReceiptEntry {
  id: number;
  time: string;
  title: string;
  detail: string;
  // A figure at the right end of the title line, such as an amount.
  figure?: string;
}

interface ReceiptPrinterProps {
  // Oldest first; the newest is the one nearest the slot.
  entries: ReceiptEntry[];
  onDismiss: (id: number) => void;
  labels: { dismiss: string; empty: string; device: string };
  isReduced: boolean;
  className?: string;
}

// Notifications as a till receipt. A small dark printer sits at the foot; each new notice feeds out of its slot in
// jerks, pushing the earlier ones up the strip, in the typewriter face a receipt is printed in. Each notice can be
// dismissed, and the paper closes up over the gap.
export function ReceiptPrinter({ entries, onDismiss, labels, isReduced, className }: ReceiptPrinterProps) {
  return (
    <div className={cn("flex w-72 flex-col items-center", className)}>
      <div className="w-64 font-mono text-xs text-slate-900 drop-shadow-xl">
        {entries.length > 0 && (
          <svg aria-hidden viewBox="0 0 240 8" preserveAspectRatio="none" className="block h-2 w-full fill-white">
            <polygon points={`0,8 ${TEETH} 240,8`} />
          </svg>
        )}
        <ul aria-live="polite" className="flex flex-col bg-white">
          <AnimatePresence initial={false}>
            {entries.map((entry) => (
              <motion.li
                key={entry.id}
                initial={isReduced ? false : { height: 0 }}
                animate={{ height: "auto" }}
                exit={{ height: 0, opacity: 0 }}
                transition={{ duration: 0.5, ease: feed }}
                className="overflow-hidden"
              >
                <div className="relative border-b border-dashed border-slate-300 px-4 py-3">
                  <p className="text-slate-500">{entry.time}</p>
                  <p className="mt-1 flex justify-between gap-3 pr-5 text-sm font-semibold">
                    <span>{entry.title}</span>
                    {entry.figure && <span className="tabular-nums">{entry.figure}</span>}
                  </p>
                  <p className="mt-0.5 text-slate-600">{entry.detail}</p>
                  <button
                    type="button"
                    onClick={() => onDismiss(entry.id)}
                    aria-label={`${labels.dismiss}: ${entry.title}`}
                    className="absolute top-2 right-2 grid size-6 place-items-center rounded-sm text-slate-400 hover:text-slate-900 focus-visible:outline-2 focus-visible:outline-slate-900"
                  >
                    <X aria-hidden className="size-3.5" />
                  </button>
                </div>
              </motion.li>
            ))}
          </AnimatePresence>
        </ul>
      </div>
      <div className="relative z-10 -mt-px flex h-16 w-full items-end justify-between rounded-2xl bg-slate-900 px-5 pb-3 text-xs text-slate-400 shadow-2xl inset-shadow-sm inset-shadow-white/20">
        <span aria-hidden className="absolute inset-x-3 top-0 h-2 rounded-b-md bg-black" />
        <span>{labels.device}</span>
        <span className="flex items-center gap-2">
          {entries.length === 0 && labels.empty}
          <span
            aria-hidden
            className={cn("size-1.5 rounded-full", entries.length > 0 ? "bg-emerald-400" : "bg-slate-600")}
          />
        </span>
      </div>
    </div>
  );
}
The printer

A strip with a memory

Each notice carries the real time. Earlier notices ride up the paper, and the strip keeps a fixed number; printing another drops the oldest off the top, torn edge and all.

src/registry/interactive/receipt/interactive-receipt.tsx

"use client";

import { useState } from "react";
import { usePrefersReducedMotion } from "@/hooks/use-prefers-reduced-motion";
import { cn } from "@/lib/utils";
import { ReceiptPrinter, type ReceiptEntry } from "./receipt-printer";

// The strip keeps this many notices; printing another drops the oldest off the top.
const MAX_ENTRIES = 4;

export interface ReceiptNotice {
  // The button that triggers it, then what gets printed.
  action: string;
  title: string;
  detail: string;
  figure?: string;
}

export interface InteractiveReceiptProps {
  title: string;
  description: string;
  notices: ReceiptNotice[];
  labels: { dismiss: string; empty: string; device: string; clear: string };
  className?: string;
}

// Notifications that print. Each button sends a notice to a small receipt printer, which feeds it out on paper with
// the real time on it; earlier notices ride up the strip, and each can be dismissed or the whole strip torn off.
export function InteractiveReceipt({ title, description, notices, labels, className }: InteractiveReceiptProps) {
  const isReduced = usePrefersReducedMotion();
  const [entries, setEntries] = useState<ReceiptEntry[]>([]);

  const print = (notice: ReceiptNotice) => {
    const now = new Date();
    const entry = {
      id: now.getTime(),
      time: now.toLocaleTimeString("en-US", { hour: "numeric", minute: "2-digit", second: "2-digit" }),
      ...notice,
    };
    setEntries((current) => [...current, entry].slice(-MAX_ENTRIES));
  };

  return (
    <section className={cn("relative overflow-hidden bg-white px-5 py-20 text-slate-950 sm:py-28", className)}>
      <div className="mx-auto grid max-w-5xl items-end gap-14 lg:grid-cols-2">
        <div className="flex flex-col items-start gap-8 self-center">
          <div>
            <h2 className="text-3xl font-medium tracking-tight text-balance sm:text-4xl">{title}</h2>
            <p className="mt-3 max-w-md text-pretty text-slate-600">{description}</p>
          </div>
          <div className="flex flex-wrap gap-2.5">
            {notices.map((notice) => (
              <button
                key={notice.action}
                type="button"
                onClick={() => print(notice)}
                className="h-10 rounded-full bg-white px-4 text-sm font-medium shadow-sm ring-1 ring-slate-200 transition duration-150 hover:ring-slate-400 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-sky-600 active:translate-y-px"
              >
                {notice.action}
              </button>
            ))}
            {entries.length > 0 && (
              <button
                type="button"
                onClick={() => setEntries([])}
                className="h-10 rounded-full px-3 text-sm text-slate-600 underline decoration-slate-300 underline-offset-4 hover:decoration-slate-900 focus-visible:outline-2 focus-visible:outline-sky-600"
              >
                {labels.clear}
              </button>
            )}
          </div>
        </div>
        <div className="flex justify-center">
          <ReceiptPrinter
            entries={entries}
            onDismiss={(id) => setEntries((current) => current.filter((entry) => entry.id !== id))}
            labels={labels}
            isReduced={isReduced}
            className="min-h-96 justify-end"
          />
        </div>
      </div>
    </section>
  );
}
The notices

More articles