# Signature Pad

A line to sign on with a mouse, a finger or a stylus, inked like a ballpoint. Submits with its form; exports SVG or PNG.

Docs: https://ballpoint.st9wd.com/docs/signature-pad

```bash
npx shadcn@latest add @ballpoint/signature-pad
```

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

<form action={sign}>
  <SignaturePad name="signature" required />
  <Button type="submit">Sign</Button>
</form>
```

Props:

- `name`: `string`. The form field the signature is submitted under, as a data URL.
- `required`: `boolean`. The form won't submit unsigned; the pad turns red and the browser says why.
- `valueFormat`: `"svg" | "png"` (default `"svg"`). What the form receives.
- `onChange`: `(value: string) => void`. After every stroke, and on clear, with the form value (empty when cleared).
- `ref`: `Ref<SignaturePadHandle>`. clear(), undo(), isEmpty(), strokes(), toSVG(options), toDataURL(type, options). Exports are trimmed to the ink, in blue ballpoint unless you pass a color.
- `label`: `string` (default `"Signature"`). Names the pad for screen readers.
- `placeholder`: `string` (default `"Sign here"`). Written above the line until the first stroke.
- `disabled`: `boolean`. Stops the pen.
- `seed`: `string | number`. Pins the drawing of the box and the line.

Pen settings: `draw`, `roughness`, `weight`, `speed` (see Pen settings).

## Example

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { SignaturePad } from "@/components/ui/signature-pad";

export default function SignaturePadDemo() {
  const [signed, setSigned] = useState("");
  return (
    <form
      className="flex w-full max-w-md flex-col gap-4"
      onSubmit={(event) => {
        event.preventDefault();
        setSigned(String(new FormData(event.currentTarget).get("signature") ?? ""));
      }}
    >
      <SignaturePad name="signature" required seed="signature-demo" />
      <div className="flex min-h-12 items-center gap-4">
        <Button type="submit" seed="signature-submit">
          Sign
        </Button>
        {signed && (
          // eslint-disable-next-line @next/next/no-img-element
          <img src={signed} alt="The signature, as the form sent it" className="h-12 w-auto" />
        )}
      </div>
    </form>
  );
}
```

## Undo, clear, export

The ref hands back undo(), clear(), and exports; here a PNG, trimmed to the ink.

```tsx
"use client";

import { useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import { SignaturePad, type SignaturePadHandle } from "@/components/ui/signature-pad";

export default function SignaturePadControls() {
  const pad = useRef<SignaturePadHandle>(null);
  const [empty, setEmpty] = useState(true);
  const [png, setPng] = useState("");
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <SignaturePad ref={pad} placeholder="Sign with your finger" onChange={(value) => {
          setEmpty(value === "");
          setPng("");
        }} seed="spc-pad" />
      <div className="flex flex-wrap items-center gap-3">
        <Button variant="outline" size="sm" disabled={empty} onClick={() => pad.current?.undo()} seed="spc-undo">
          Undo
        </Button>
        <Button variant="ghost" size="sm" disabled={empty} onClick={() => pad.current?.clear()} seed="spc-clear">
          Clear
        </Button>
        <Button size="sm" disabled={empty} onClick={() => setPng(pad.current?.toDataURL("image/png") ?? "")} seed="spc-export">
          Export PNG
        </Button>
        {png && (
          // eslint-disable-next-line @next/next/no-img-element
          <img src={png} alt="The exported signature" className="h-10 w-auto" />
        )}
      </div>
    </div>
  );
}
```

## In a form

A required signature beside a name and a checkbox, read back from the form data.

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { SignaturePad } from "@/components/ui/signature-pad";

export default function SignaturePadForm() {
  const [result, setResult] = useState<{ name: string; signature: string } | null>(null);
  return (
    <form
      className="w-full max-w-md"
      onSubmit={(event) => {
        event.preventDefault();
        const data = new FormData(event.currentTarget);
        setResult({ name: String(data.get("name")), signature: String(data.get("signature")) });
      }}
    >
      <FieldGroup>
        <Field>
          <FieldLabel htmlFor="spf-name">Full name</FieldLabel>
          <Input id="spf-name" name="name" required defaultValue="Grace Hopper" seed="spf-name" />
        </Field>
        <SignaturePad name="signature" required label="Sign to agree" placeholder="Sign here" seed="spf-pad" />
        <Field orientation="horizontal">
          <Checkbox id="spf-copy" name="copy" defaultChecked seed="spf-copy" />
          <FieldLabel htmlFor="spf-copy">Send me a copy</FieldLabel>
        </Field>
        <div className="flex min-h-12 items-center gap-4">
          <Button type="submit" seed="spf-submit">
            Agree and sign
          </Button>
          {result && (
            <span className="flex items-center gap-3 text-ink-2">
              Signed by {result.name}
              {/* eslint-disable-next-line @next/next/no-img-element */}
              <img src={result.signature} alt="Signature" className="h-10 w-auto" />
            </span>
          )}
        </div>
      </FieldGroup>
    </form>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
