import { useEffect, useRef, useState } from "react"; import { Eraser } from "lucide-react"; import { Button } from "@edr/ui-common"; import { cn } from "@/lib/utils"; interface ContractSignaturePadProps { onChange: (dataUrl: string | null) => void; className?: string; } export function ContractSignaturePad({ onChange, className, }: ContractSignaturePadProps) { const canvasRef = useRef(null); const drawing = useRef(false); const [empty, setEmpty] = useState(true); useEffect(() => { const canvas = canvasRef.current; if (!canvas) return; const ctx = canvas.getContext("2d"); if (!ctx) return; const dpr = window.devicePixelRatio || 1; const w = canvas.offsetWidth; const h = canvas.offsetHeight; canvas.width = w * dpr; canvas.height = h * dpr; ctx.scale(dpr, dpr); ctx.strokeStyle = "#111"; ctx.lineWidth = 2; ctx.lineCap = "round"; }, []); const getPos = (e: React.MouseEvent | React.TouchEvent) => { const canvas = canvasRef.current!; const rect = canvas.getBoundingClientRect(); if ("touches" in e) { const t = e.touches[0]; return { x: t.clientX - rect.left, y: t.clientY - rect.top }; } return { x: e.clientX - rect.left, y: e.clientY - rect.top }; }; const start = (e: React.MouseEvent | React.TouchEvent) => { drawing.current = true; const ctx = canvasRef.current?.getContext("2d"); const { x, y } = getPos(e); ctx?.beginPath(); ctx?.moveTo(x, y); }; const move = (e: React.MouseEvent | React.TouchEvent) => { if (!drawing.current) return; const ctx = canvasRef.current?.getContext("2d"); const { x, y } = getPos(e); ctx?.lineTo(x, y); ctx?.stroke(); setEmpty(false); onChange(canvasRef.current?.toDataURL("image/png") ?? null); }; const end = () => { drawing.current = false; }; const clear = () => { const canvas = canvasRef.current; if (!canvas) return; const ctx = canvas.getContext("2d"); if (!ctx) return; ctx.clearRect(0, 0, canvas.width, canvas.height); setEmpty(true); onChange(null); }; return (

Draw your signature above

{empty && (

Signature is required before confirming.

)}
); }