# Signature pad

> Sign with ink that behaves like ink: it thickens where you slow down, dries in the order you wrote it, and clearing rewinds your strokes.

Signature fields are usually a box that draws a flat grey line and blanks when you clear it. Signature pad writes with a nib: the line thickens where you slow down and thins where you hurry, and the ink is wet, a soft sheen riding along every stroke. Accept and the sheen dries off in the order you wrote it; then the signature lifts off the pad and settles small on the signed line, with your name and the time under it, while the pad folds down to fit. Clear doesn’t blank the pad: the strokes rewind, last one first, back to nothing. “Type instead” swaps the pad for your name in a script, for keyboards and screen readers. onSign gets a vector SVG for your PDF, a PNG and the time; if your save fails, the pad comes back with the ink still on it.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/signature-pad.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `text-morph`
- Docs and live preview: https://hairlineui.com/ui/signature-pad
- Registry item (source): https://hairlineui.com/r/signature-pad.json

## Usage

```tsx
import { SignaturePad } from "@/components/signature-pad";

<SignaturePad
  name="Mira Okafor"
  onSign={({ svg, signedAt }) => fetch("/api/contract/sign", { method: "POST", body: JSON.stringify({ svg, signedAt }) })}
/>
```

## Into a PDF

Send the SVG to your server and stamp it onto the contract with pdf-lib.

```tsx
// app/api/contract/sign/route.ts
import { PDFDocument } from "pdf-lib";
import sharp from "sharp";
import { getContract, saveSigned } from "@/lib/contracts";

export async function POST(req: Request) {
  const { svg, signedAt, contractId } = await req.json();
  const pdf = await PDFDocument.load(await getContract(contractId));
  const png = await sharp(Buffer.from(svg)).png().toBuffer();
  const image = await pdf.embedPng(png);
  const page = pdf.getPages().at(-1)!;
  page.drawImage(image, { x: 72, y: 120, width: 180, height: (180 * image.height) / image.width });
  page.drawText(`Signed ${new Date(signedAt).toUTCString()}`, { x: 72, y: 104, size: 9 });
  await saveSigned(contractId, await pdf.save());
  return Response.json({ ok: true });
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `onSign` | `(result: { svg, dataUrl, typed?, signedAt }) => Promise<unknown> \| void` | – | Called on Accept. Throw or reject to bring the pad back with the ink. |
| `name` | `string` | – | Shown under the signature once signed. |
| `onClear` | `() => void` | – | Called when the pad is cleared. |
| `label` | `string` | `"Sign here"` | The prompt under the line. |
| `ink` | `string` | `a blue-black from your theme` | The ink: any CSS colour. |
| `locale` | `string` | – | For the signing time; the visitor’s once hydrated when left out. |

## States

- **empty**: A signing line with an × and “Sign here” under it; Accept and Clear rest dimmed.
- **write**: Each stroke follows coalesced pointer events through midpoint curves; its width eases between 1.1 and 3.6px from your speed, and a light sheen rides its upper edge while it’s wet.
- **dry**: Accept takes the sheen off segment by segment in writing order (300–900ms by how much ink there is).
- **settle**: The signature, cropped to its ink, lifts off the pad and scales down onto the signed line (620ms) as the pad folds to its signed height (500ms); “Signed” in green, your name and the time under it.
- **clear**: The strokes rewind from the last one back to the first (up to 760ms), then the pad is empty.
- **type instead**: The pad becomes a field that writes your name in a script; Enter or Accept signs with it.
- **refused**: If onSign throws or rejects, the pad comes back with the ink, with a small shake.
- **reduced motion**: No drying, rewinding or settling: each state appears in place.

## What it handles

A signature box draws a flat line of one weight, blanks when you clear it, and leaves no trace of who signed or when once it’s saved.

- **Ink, not a line**: The stroke thickens as you slow and thins as you hurry, and it’s wet until it dries.
- **Signed, visibly**: Accept dries the ink and settles the signature on its line with your name and the time.
- **Clear rewinds**: Strokes come off last one first instead of the pad blanking.
- **Keyboard friendly**: “Type instead” signs with your name in a script.
- **Ready for documents**: A cropped vector SVG, a PNG and the signing time come back to you.
- **Your save decides**: If onSign throws, the pad returns with the ink on it.

## Replaces

a signature canvas, react-signature-canvas, an e-signature field, a typed signature input

## Where it fits

- **Contracts and agreements**: Signing a proposal, an NDA or terms inside your own app.
- **Deliveries and handovers**: Proof of delivery or a key handover on a phone.
- **Waivers and consent**: Clinics, gyms, events and schools.
- **Approvals**: Signing off an invoice, a timesheet or an inspection.

## Notes

- The pad is labelled for screen readers, and “Type instead” gives keyboards a full way to sign; signing is announced, and the signed image has your name as its alt text.
- Strokes are kept as curve segments with their widths, so the SVG you get is the same drawing, cropped to the ink, not a screenshot.
- Pointer events are coalesced, so fast strokes on high-rate screens keep their shape; touch scrolling is off on the pad only.
- Installs Text morph. With reduced motion every state appears in place.

License: MIT (https://hairlineui.com/license#components)
