# Signature Pad

> Sign with a finger, a pen or the mouse, or type your name instead, with Undo and Clear.

Page: https://uicontext.com/docs/components/signature-pad

Group: Forms.

## When to use

Sign with a finger, a pen or the mouse, or type your name instead, with Undo and Clear. Use [File Field](https://uicontext.com/docs/components/file-field.md) instead when people upload a signed document instead.

## Usage

```ts
import { SignaturePad } from "./ui/uicontext/components/SignaturePad.ts"

const Message = UiSchema.TaggedEnum({ Signed: { path: Schema.String, text: Schema.String } })

export const Delivery = (h: Html.H<typeof Message.Type>) =>
	SignaturePad(h, { key: "delivery", props: { label: "Received by" }, onEmit: { Changed: ({ path, text }) => Message.Signed({ path, text }) } })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Sign a contract

The example the page opens with: Signature Pad in “Sign a contract”.

This example also uses Domain. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/button-family
```

```ts title="src/demo.ts" source="www/src/examples/signature-pad/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { buttonStyle } from "./ui/uicontext/components/Button.ts"
import { SignaturePad } from "./ui/uicontext/components/SignaturePad.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	form: { display: "grid", gap: space.lg, inlineSize: "min(100%, 34rem)" },
	terms: { margin: 0, fontSize: font.sm, lineHeight: 1.5, color: color.muted },
	submit: { justifySelf: "start" },
	done: { margin: 0, fontSize: font.sm, color: color.success },
})

const Message = UiSchema.TaggedEnum({ Signed: { path: Schema.String, text: Schema.String }, Submitted: {} })

// the last step of a contract: sign, or type your name, then agree
export const SignContract = Html.component({
	name: "SignContract",
	model: Schema.Struct({ path: Schema.String, text: Schema.String, tried: Schema.Boolean, sent: Schema.Boolean }),
	message: Message,
}).make({
	init: () => ({ path: "", text: "", tried: false, sent: false }),
	update: {
		Signed: ($, { path, text }) => $.evolve({ path, text, sent: false }),
		Submitted: ($) => $.evolve({ tried: true, sent: $.model.path !== "" || $.model.text !== "" }),
	},
	view: (h, model) =>
		h.form.attrs(sx(styles.form)).on("submit", Message.Submitted(), { preventDefault: true }).children(
			h.p.attrs(sx(styles.terms)).children("Master services agreement between Northwind Ltd and Checkout Studio, effective 1 January 2027."),
			SignaturePad(h, {
				key: "sign",
				props: { label: "Your signature", name: "Ada Okafor, Head of Product", hint: "By signing you agree to the terms above.", invalid: model.tried && model.path === "" && model.text === "" },
				onEmit: { Changed: ({ path, text }) => Message.Signed({ path, text }) },
			}),
			h.button.attrs({ type: "submit", ...sx(...buttonStyle({ tone: "primary" }), styles.submit) }).children("Sign agreement"),
			model.sent ? h.p.attrs({ role: "status", ...sx(styles.done) }).children("Signed. A copy is on its way to your email.") : null,
		),
})
```

### Proof of delivery

`Changed` fires after each stroke, so the app knows at once whether there's a signature.

```ts title="src/delivery.ts" source="www/src/examples/signature-pad/delivery.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { SignaturePad } from "./ui/uicontext/components/SignaturePad.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	wrap: { display: "grid", gap: space.md, inlineSize: "min(100%, 28rem)" },
	parcel: { margin: 0, fontSize: font.sm, color: color.muted, fontVariantNumeric: "tabular-nums" },
	got: { margin: 0, fontSize: font.sm, color: color.fg },
})

const Message = UiSchema.TaggedEnum({ Signed: { path: Schema.String, text: Schema.String } })

// a courier's proof of delivery, on a phone at the door
export const ProofOfDelivery = Html.component({
	name: "ProofOfDelivery",
	model: Schema.Struct({ signed: Schema.Boolean }),
	message: Message,
}).make({
	init: () => ({ signed: false }),
	update: { Signed: ($, { path, text }) => $.evolve({ signed: path !== "" || text !== "" }) },
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			h.p.attrs(sx(styles.parcel)).children("Parcel 3 of 3 · 12 Rua das Flores, Porto"),
			SignaturePad(h, { key: "delivery", props: { id: "delivery", label: "Received by" }, onEmit: { Changed: ({ path, text }) => Message.Signed({ path, text }) } }),
			h.p.attrs({ role: "status", ...sx(styles.got) }).children(model.signed ? "Ready to confirm delivery." : "Waiting for a signature."),
		),
})
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `id` | `string` | `"signature"` | Names the pad's parts on the page; "signature" by default |
| `label` (required) | `string` | — | What's being signed for, above the pad: "Signature" |
| `hint` | `string` | — | Under the pad: "By signing you agree to the terms above." |
| `name` | `string` | — | The signer's name, shown on the line and offered when typing |
| `disabled` | `boolean` | — | — |
| `invalid` | `boolean` | — | Says the pad must be signed |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `Changed` | `{ mode: "draw" \| "type"; path: string; text: string }` | The signature now: drawn strokes as a path in a 600 by 200 box, or a typed name; both empty when cleared |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/signature-pad
```

With npm: `npx shadcn@latest add checkfu/uicontext/signature-pad`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/signature-pad`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/signature-pad`.

It writes these files:

- `src/ui/uicontext/components/SignaturePad.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/SignaturePad.ts](https://github.com/checkfu/uicontext/blob/main/src/SignaturePad.ts)

## Related

- [Form](https://uicontext.com/docs/components/form.md) (`form`): A form built from one Effect Schema. Errors read in plain words, and focus goes to the first field to fix.
- [Form Layout](https://uicontext.com/docs/components/form-layout.md) (`form-layout`): Labels, hints and sections for a form, with a save bar that guards unsaved changes.
- [Repeating Fields](https://uicontext.com/docs/components/repeating-fields.md) (`repeating-fields`): Rows of fields to add, remove and reorder, such as invites or line items.
- [Form Wizard](https://uicontext.com/docs/components/form-wizard.md) (`form-wizard`): A long form in steps. Each step checks its own fields, and progress survives a reload.
- [Date Field](https://uicontext.com/docs/components/date-field.md) (`date-field`): A date typed part by part in the reader's order, with an error that says what's wrong.
- [Date Picker](https://uicontext.com/docs/components/date-picker.md) (`date-picker`): A date field with a calendar. Type words like “next fri”, or pick from the keyboard.
