SignaturePad
MD · 1950B gzip budgetCanvas-based signature capture with undo and clear.
- Category
- Forms & input
- Budget
- md, several states or a live subscription
- External runtime
- React / React DOM only
axe fixtureSSR rendersource scanbundle budget
Preview
Live fixtureTry the component here. Locale-aware formatting and direction follow the selected language; example content and labels are not automatically translated. Overlay demos use interactive launchers.
Install
npx shadcn@latest add https://gear5-ui.vercel.app/r/signature-pad.jsonCopies the source into your project. Pulls in 1 shared registry item: cn.
New here? Set up Tailwind and import aliases first →Source & props
Edit on GitHub ↗Prop types and inline documentation are included below. This is repository source; the installer rewrites shared imports for your project.
"use client";
import { useId, useRef, useState } from "react";
import { cn } from "../lib/cn";
export interface SignaturePadProps {
value: string;
onChange: (dataUrl: string) => void;
label: string;
width?: number;
height?: number;
className?: string;
}
export function SignaturePad({ value, onChange, label, width = 400, height = 200, className }: SignaturePadProps) {
const id = useId();
const canvasRef = useRef<HTMLCanvasElement>(null);
const [drawing, setDrawing] = useState(false);
const getPos = (e: React.MouseEvent | React.TouchEvent) => {
const canvas = canvasRef.current;
if (!canvas) return { x: 0, y: 0 };
const rect = canvas.getBoundingClientRect();
const clientX = "touches" in e ? e.touches[0].clientX : e.clientX;
const clientY = "touches" in e ? e.touches[0].clientY : e.clientY;
return {
x: (clientX - rect.left) * (canvas.width / rect.width),
y: (clientY - rect.top) * (canvas.height / rect.height),
};
};
const startDraw = (e: React.MouseEvent | React.TouchEvent) => {
setDrawing(true);
const ctx = canvasRef.current?.getContext("2d");
const pos = getPos(e);
ctx?.beginPath();
ctx?.moveTo(pos.x, pos.y);
};
const draw = (e: React.MouseEvent | React.TouchEvent) => {
if (!drawing) return;
const ctx = canvasRef.current?.getContext("2d");
const pos = getPos(e);
ctx?.lineTo(pos.x, pos.y);
ctx?.stroke();
};
const endDraw = () => {
setDrawing(false);
const canvas = canvasRef.current;
if (canvas) {
onChange(canvas.toDataURL());
}
};
const clear = () => {
const ctx = canvasRef.current?.getContext("2d");
const canvas = canvasRef.current;
if (ctx && canvas) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
onChange("");
}
};
return (
<div className={cn("flex flex-col gap-1.5 text-start", className)}>
<label id={id} className="text-sm font-medium">
{label}
</label>
<canvas
ref={canvasRef}
width={width}
height={height}
aria-labelledby={id}
role="img"
onMouseDown={startDraw}
onMouseMove={draw}
onMouseUp={endDraw}
onMouseLeave={endDraw}
onTouchStart={startDraw}
onTouchMove={draw}
onTouchEnd={endDraw}
className="cursor-crosshair rounded-md border border-neutral-300 dark:border-neutral-700"
style={{ touchAction: "none" }}
/>
<button
type="button"
onClick={clear}
aria-label="Clear signature"
className="self-start rounded px-3 py-1 text-sm text-neutral-600 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:bg-neutral-800"
>
Clear
</button>
</div>
);
}
What CI checks
Quality contract- Bundled, minified and gzipped against its tier budget
- Audited by axe in the state previewed above
- Rendered through react-dom/server with no browser globals
- Scanned for network calls, dangerous sinks, and unguarded animation