# Receipt Printer
> A receipt that prints line by line, then tears off and falls when you pull it, built with React.

Source: https://kokonutui.com/docs/cards/receipt-printer

## Installation
```bash
npx shadcn@latest add @kokonutui/receipt-printer
```

## Props

| Prop          | Type         | Default                      | Description                    |
|---------------|--------------|------------------------------|--------------------------------|
| `merchant?`  | `string`     | "Kokonut Coffee"           |                                |
| `order?`     | `string`     | "#4182"                    |                                |
| `date?`      | `string`     | "14 Oct 2026 · 08:42"     |                                |
| `items?`     | `ReceiptItem[]` | `built-in`                |                                |
| `tax?`       | `number`     | `0.08`                       | Tax rate, 0.08 for 8%.        |
| `currency?`  | `string`     | `"$"`                      |                                |
| `autoPrint?` | `boolean`    | `true`                       | Print as soon as the printer scrolls into view. |
| `onPrint?`   | `() => void` |                              |                                |
| `onTear?`    | `() => void` |                              |                                |
| `className?` | `string`     |                              | Extra classes for the root element. |

### ReceiptItem

| Field     | Type     | Description |
|-----------|----------|-------------|
| `name`    | `string` |             |
| `quantity`| `number` |             |
| `price`   | `number` |             |

## Source

```tsx
"use client";

/**
 * @author: @dorianbaffier
 * @description: Receipt Printer
 * @version: 1.0.0
 * @date: 2026-10-14
 * @license: MIT
 * @website: https://kokonutui.com
 * @github: https://github.com/kokonut-labs/kokonutui
 */

import {
  type PointerEvent,
  useCallback,
  useEffect,
  useRef,
  useState,
} from "react";
import { cn } from "@/lib/utils";

// Where the paper comes out, from the top of the card.
const SLOT_Y = 64;
// The printer feeds a line at a time, with a short stop between lines.
const LINE = 15;
const FEED_RATE = 34;
const LINE_PAUSE = 0.055;
const LINE_JITTER = 0.05;
// Simulation, in px and seconds, on a fixed step.
const STEP = 1 / 120;
const GRAVITY = 2600;
const DAMPING = 0.996;
// While hanging from the slot, a pull stretches only this far, with
// growing resistance, and tears past this much drag.
const MAX_PULL = 18;
const TEAR_DRAG = 72;
// A sharp downward yank tears it too, in px per second.
const TEAR_SPEED = 900;
// A tilted sheet glides sideways as it falls.
const GLIDE = 0.35;
const REST_EPSILON = 0.02;

// Definition for ReceiptItem
export interface ReceiptItem {
  name: string;
  quantity: number;
  price: number;
}

// Definition for ReceiptPrinterProps
export interface ReceiptPrinterProps {
  merchant?: string;
  order?: string;
  date?: string;
  items?: ReceiptItem[];
  /** Tax rate, 0.08 for 8%. */
  tax?: number;
  currency?: string;
  /** Print as soon as the printer scrolls into view. */
  autoPrint?: boolean;
  onPrint?: () => void;
  onTear?: () => void;
  className?: string;
}

const DEFAULT_ITEMS: ReceiptItem[] = [
  { name: "Flat white", quantity: 2, price: 4.5 },
  { name: "Almond croissant", quantity: 1, price: 3.8 },
  { name: "Oat milk", quantity: 2, price: 0.6 },
  { name: "Banana bread", quantity: 1, price: 3.2 },
];

// Barcode bar widths, fixed so server and browser agree.
const BARS = [
  2, 1, 1, 3, 1, 2, 2, 1, 3, 1, 1, 2, 1, 1, 2, 3, 1, 2, 1, 1, 3, 2, 1, 1, 2, 1,
  3, 1, 1, 2, 2, 1, 1, 3, 1, 2,
];

function damp(distance: number, max: number) {
  return max * (1 - Math.exp(-distance / max));
}

function haptic(ms = 8) {
  if (typeof navigator !== "undefined" && "vibrate" in navigator) {
    navigator.vibrate(ms);
  }
}

/** Local point p on a sheet turned by `angle`, relative to its corner. */
function turn(px: number, py: number, angle: number) {
  const cos = Math.cos(angle);
  const sin = Math.sin(angle);
  return [px * cos - py * sin, px * sin + py * cos];
}

function Receipt({
  merchant,
  order,
  date,
  items,
  tax,
  currency,
}: Required<
  Pick<
    ReceiptPrinterProps,
    "merchant" | "order" | "date" | "items" | "tax" | "currency"
  >
>) {
  const money = (n: number) => n.toFixed(2);
  const subtotal = items.reduce((sum, i) => sum + i.quantity * i.price, 0);
  const taxAmount = subtotal * tax;
  return (
    <div className="px-5 pt-6 pb-8 font-mono text-[11px] text-zinc-800 leading-[15px]">
      <p className="text-center font-semibold text-xs tracking-[0.2em]">
        {merchant.toUpperCase()}
      </p>
      <p className="text-center text-zinc-500">{date}</p>
      <p className="text-center text-zinc-500">Order {order}</p>
      <p
        aria-hidden="true"
        className="my-[7px] overflow-hidden whitespace-nowrap text-zinc-400"
      >
        {"- ".repeat(40)}
      </p>
      {items.map((item) => (
        <p className="flex justify-between gap-3" key={item.name}>
          <span className="truncate">
            {item.quantity} × {item.name}
          </span>
          <span className="tabular-nums">
            {money(item.quantity * item.price)}
          </span>
        </p>
      ))}
      <p
        aria-hidden="true"
        className="my-[7px] overflow-hidden whitespace-nowrap text-zinc-400"
      >
        {"- ".repeat(40)}
      </p>
      <p className="flex justify-between text-zinc-500">
        <span>Subtotal</span>
        <span className="tabular-nums">{money(subtotal)}</span>
      </p>
      <p className="flex justify-between text-zinc-500">
        <span>Tax {Math.round(tax * 100)}%</span>
        <span className="tabular-nums">{money(taxAmount)}</span>
      </p>
      <p className="mt-1 flex justify-between font-semibold text-[13px]">
        <span>Total</span>
        <span className="tabular-nums">
          {currency}
          {money(subtotal + taxAmount)}
        </span>
      </p>
      <div aria-hidden="true" className="mt-4 flex h-9 justify-center gap-px">
        {BARS.map((width, i) => (
          <span
            className={i % 2 ? "bg-transparent" : "bg-zinc-800"}
            key={`bar-${i.toString()}`}
            style={{ width }}
          />
        ))}
      </div>
      <p className="mt-3 text-center text-zinc-500">Thank you, come again</p>
    </div>
  );
}

// Remaining component implementation omitted for brevity...

export default function ReceiptPrinter({
  merchant = "Kokonut Coffee",
  order = "#4182",
  date = "14 Oct 2026 · 08:42",
  items = DEFAULT_ITEMS,
  tax = 0.08,
  currency = "$",
  autoPrint = true,
  onPrint,
  onTear,
  className,
}: ReceiptPrinterProps) {
  // Component logic...
}
