Command palette

@yasinsensoy/web

Web · Components

SignaturePad

Signature pad: sign with a finger, pen or mouse.

10 exports

Live example

Preview

Code

import { SignaturePad } from "@yasinsensoy/web";

<SignaturePad ref={ref} className="h-48" />

Events

  1. Events appear here when you interact with the preview.

Import

import { SignaturePad, signatureToSvg, isSignatureEmpty } from "@yasinsensoy/web";

SignaturePad

componentClient component

Signature pad: sign with a finger, pen or mouse. Contract approval, delivery confirmation, form signature.

  • Strokes are smoothed; they thicken and thin by speed and pen pressure.
  • Sharp on high-resolution screens; when the area resizes, the signature is redrawn keeping its proportions (strokes are stored as vectors).
  • Undo / redo / clear buttons; Ctrl+Z and Ctrl+Shift+Z work too.
  • Mobile: the page doesn't scroll while drawing; multi-finger touch is ignored.
  • Form: with name the signature is submitted as PNG/SVG/JSON; required joins browser validation. For labeled use see SignatureField.
  • toDataURL(), toSVG(), toBlob() are available via ref.
SignaturePad({ value, defaultValue, onValueChange, color, minWidth, maxWidth, placeholder, toolbar, readOnly, disabled, name, format, required, form, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, ref, classNames, className, id: idProp, ...props }: SignaturePadProps): JSX.Element

Examples

<SignaturePad name="signature" required className="h-48" />

const ref = useRef<SignaturePadHandle>(null);
<SignaturePad ref={ref} color="#1d4ed8" />
<Button onClick={async () => upload(await ref.current?.toBlob())}>Send</Button>

Converts a signature to an SVG string (to save as a file or send to the server). Strokes are written as paths at pad size.

signatureToSvg(value: SignatureValue, { width, height, color, background, }: { width: number; height: number; color?: string; background?: string; }): string

Examples

const svg = signatureToSvg(signature, { width: 600, height: 200 });

Is the signature empty (no strokes)?

isSignatureEmpty(value: SignatureValue | null | undefined): boolean

A point of a signature. Positions are fractions of the pad size (0–1).

NameTypeDescription
xnumberHorizontal position (0–1, relative to the pad width).
ynumberVertical position (0–1, relative to the pad height).
wnumberStroke width at this point (relative to the pad width).

A single stroke drawn without lifting the pen.

type SignatureStroke = readonly SignaturePoint[]

The whole signature: strokes in order.

type SignatureValue = readonly SignatureStroke[]

Functions of SignaturePad accessed with ref.

NameTypeDescription
toDataURL(type?: "image/png" | "image/jpeg" | "image/webp", quality?: number) => stringConverts the signature to an image URL (image/png by default). "" if empty.
toSVG() => stringConverts the signature to an SVG string.
toBlob(type?: "image/png" | "image/jpeg" | "image/webp") => Promise<Blob | null>Converts the signature to a file (for uploading to a server). null if empty.
clear() => voidClears the signature.
undo() => voidUndoes the last stroke.
redo() => voidRedoes what was undone.
isEmpty() => booleanWhether the signature is empty.

Classes that can be passed to the inner parts of SignaturePad.

NameTypeDescription
canvas?stringDrawing area (<canvas>).
hint?string"Sign here" hint and signature line.
toolbar?stringUndo / redo / clear buttons.

Format of the value submitted with the form.

type SignatureFormat = "png" | "svg" | "json"

Props of the SignaturePad component.

Extends: Omit<ComponentProps<"div">, "defaultValue" | "onChange" | "ref" | "color">

NameTypeDefaultDescription
value?SignatureValueSignature (strokes) in controlled use. See SignatureValue.
defaultValue?SignatureValueInitial signature in uncontrolled use.
onValueChange?(value: SignatureValue) => voidCalled when each stroke ends, on clear and on undo.
color?stringthe theme's text colorInk color (CSS).
minWidth?number0.8Thinnest line (px).
maxWidth?number2.8Thickest line (px).
placeholder?ReactNode{@link UIProvider} textHint shown in the middle while empty: text or content. null hides it.
toolbar?booleantrueUndo, redo, clear buttons.
readOnly?booleanDisplay only; can't be drawn on.
disabled?booleanDisabled.
name?stringForm field name; the signature is submitted according to format.
format?SignatureFormat"png"Value submitted with the form: a png image URL (data:image/png…), an svg string or the strokes' json (to redraw later).
required?booleanThe form can't be submitted without a signature.
form?stringId of the form it belongs to.
aria-label?string{@link UIProvider} text ("İmza alanı")Screen reader name.
aria-labelledby?stringId of the element naming the drawing area (e.g. the field label).
ref?Ref<SignaturePadHandle>Access: toDataURL, toSVG, clear… See SignaturePadHandle.
classNames?SignaturePadClassNamesClasses added to the inner parts.