Sign with a finger, a pen or the mouse, or type your name instead, with Undo and Clear.
- 3.7 kB JS 3.3 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 13 tests
This example also uses Domain. Install it too:
npx shadcn@latest add checkfu/uicontext/button-familyimport { 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 agreeexport 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, ),})When to use
Sign with a finger, a pen or the mouse, or type your name instead, with Undo and Clear. Use File Field instead when people upload a signed document instead.
Usage
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
Proof of delivery
Changed fires after each stroke, so the app knows at once whether there's a signature.
Parcel 3 of 3 · 12 Rua das Flores, Porto
Waiting for a signature.
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 doorexport 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 |
labelrequired | 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
pnpm dlx shadcn@latest add checkfu/uicontext/signature-pad