UNPKG

@sikka/hawa

Version:

Modern UI Kit made with Tailwind

247 lines (240 loc) • 8.05 kB
"use client"; // elements/signature/Signature.tsx import React3, { useRef, useEffect } from "react"; // util/index.ts import { clsx } from "clsx"; import { twMerge } from "tailwind-merge"; function cn(...inputs) { return twMerge(clsx(inputs)); } // elements/signature/Signature.tsx import SignaturePad from "signature_pad"; import trimCanvas from "trim-canvas"; // elements/label/Label.tsx import * as React2 from "react"; // elements/tooltip/Tooltip.tsx import React from "react"; import * as TooltipPrimitive from "@radix-ui/react-tooltip"; var TooltipContent = React.forwardRef(({ className, sideOffset = 4, size = "default", ...props }, ref) => /* @__PURE__ */ React.createElement( TooltipPrimitive.Content, { ref, sideOffset, className: cn( "hawa-z-50 hawa-overflow-hidden hawa-rounded-md hawa-border hawa-bg-popover hawa-px-3 hawa-py-1.5 hawa-text-sm hawa-text-popover-foreground hawa-shadow-md hawa-animate-in hawa-fade-in-0 hawa-zoom-in-95 data-[state=closed]:hawa-animate-out data-[state=closed]:hawa-fade-out-0 data-[state=closed]:hawa-zoom-out-95 data-[side=bottom]:hawa-slide-in-from-top-2 data-[side=left]:hawa-slide-in-from-right-2 data-[side=right]:hawa-slide-in-from-left-2 data-[side=top]:hawa-slide-in-from-bottom-2", { "hawa-text-xs": size === "small", "hawa-text-xl": size === "large" }, className ), ...props } )); TooltipContent.displayName = TooltipPrimitive.Content.displayName; var TooltipArrow = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ React.createElement(TooltipPrimitive.Arrow, { ref, className: cn(className), ...props })); TooltipArrow.displayName = TooltipPrimitive.Arrow.displayName; var Tooltip = ({ side, size, open, content, children, disabled, defaultOpen, onOpenChange, triggerProps, contentProps, providerProps, delayDuration = 300, ...props }) => { return /* @__PURE__ */ React.createElement( TooltipPrimitive.TooltipProvider, { delayDuration, ...providerProps }, /* @__PURE__ */ React.createElement( TooltipPrimitive.Root, { open: !disabled && open, defaultOpen, onOpenChange, ...props }, /* @__PURE__ */ React.createElement(TooltipPrimitive.Trigger, { ...triggerProps }, children), /* @__PURE__ */ React.createElement( TooltipContent, { size, side, align: "center", ...contentProps, style: { ...contentProps == null ? void 0 : contentProps.style, maxWidth: "var(--radix-tooltip-content-available-width)", maxHeight: "var(--radix-tooltip-content-available-height)" } }, content ) ) ); }; // elements/label/Label.tsx var Label = React2.forwardRef(({ className, hint, hintSide, required, children, ...props }, ref) => /* @__PURE__ */ React2.createElement("div", { className: "hawa-flex hawa-flex-row hawa-items-center hawa-gap-1 hawa-transition-all" }, /* @__PURE__ */ React2.createElement( "label", { ref, className: cn( "hawa-text-sm hawa-font-medium hawa-leading-none peer-disabled:hawa-cursor-not-allowed peer-disabled:hawa-opacity-70", className ), ...props }, children, required && /* @__PURE__ */ React2.createElement("span", { className: "hawa-mx-0.5 hawa-text-red-500" }, "*") ), hint && /* @__PURE__ */ React2.createElement( Tooltip, { content: hint, side: hintSide, triggerProps: { tabIndex: -1, onClick: (event) => event.preventDefault() } }, /* @__PURE__ */ React2.createElement("div", null, /* @__PURE__ */ React2.createElement( "svg", { xmlns: "http://www.w3.org/2000/svg", className: "hawa-h-[14px] hawa-w-[14px] hawa-cursor-help", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round" }, /* @__PURE__ */ React2.createElement("circle", { cx: "12", cy: "12", r: "10" }), /* @__PURE__ */ React2.createElement("path", { d: "M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3" }), /* @__PURE__ */ React2.createElement("path", { d: "M12 17h.01" }) )) ))); Label.displayName = "Label"; // elements/signature/Signature.tsx var Signature = ({ canvasProps, clearOnResize = false, onGetImage, texts, label, labelProps, helperText, ...sigPadProps }) => { const canvasRef = useRef(null); const sigPadRef = useRef(null); const checkClearOnResize = () => { if (!clearOnResize) { return; } resizeCanvas(); }; const resizeCanvas = () => { var _a; const canvas = canvasRef.current; if (canvas && canvas.parentElement) { const ratio = Math.max(window.devicePixelRatio || 1, 1); if (typeof (canvasProps == null ? void 0 : canvasProps.width) === "undefined") { canvas.width = canvas.parentElement.offsetWidth * ratio; } else { canvas.width = Number(canvasProps.width) * ratio; } canvas.height = 150 * ratio; (_a = canvas.getContext("2d")) == null ? void 0 : _a.scale(ratio, ratio); clear(); } }; const getTrimmedCanvas = () => { var _a; const canvas = canvasRef.current; if (!canvas) { throw new Error("Canvas reference is null"); } const copy = document.createElement("canvas"); copy.width = canvas.width; copy.height = canvas.height; (_a = copy.getContext("2d")) == null ? void 0 : _a.drawImage(canvas, 0, 0); return trimCanvas(copy); }; const getSignatureImage = () => { const trimmedCanvas = getTrimmedCanvas(); return trimmedCanvas.toDataURL(); }; useEffect(() => { if (onGetImage) { onGetImage(getSignatureImage); } }, [onGetImage]); useEffect(() => { const canvas = canvasRef.current; if (canvas) { sigPadRef.current = new SignaturePad(canvas, sigPadProps); resizeCanvas(); window.addEventListener("resize", checkClearOnResize); } return () => { window.removeEventListener("resize", checkClearOnResize); }; }, [sigPadProps]); const clear = () => { var _a; return (_a = sigPadRef.current) == null ? void 0 : _a.clear(); }; const isEmpty = () => { var _a; return !!((_a = sigPadRef.current) == null ? void 0 : _a.isEmpty()); }; const fromDataURL = (dataURL, options) => { var _a; return (_a = sigPadRef.current) == null ? void 0 : _a.fromDataURL(dataURL, options); }; const toDataURL = (type, encoderOptions) => { var _a; return (_a = sigPadRef.current) == null ? void 0 : _a.toDataURL(type, encoderOptions); }; const fromData = (pointGroups) => { var _a; return (_a = sigPadRef.current) == null ? void 0 : _a.fromData(pointGroups); }; const toData = () => { var _a; return (_a = sigPadRef.current) == null ? void 0 : _a.toData(); }; return /* @__PURE__ */ React3.createElement("div", { className: "hawa-w-full" }, label && /* @__PURE__ */ React3.createElement(Label, { ...labelProps, className: "hawa-mb-2" }, label || "Signature"), /* @__PURE__ */ React3.createElement( "canvas", { ref: canvasRef, ...canvasProps, className: cn( "hawa-rounded hawa-border hawa-bg-[hsl(var(--constant-background))]", canvasProps == null ? void 0 : canvasProps.className ) } ), /* @__PURE__ */ React3.createElement("div", { className: "hawa-flex hawa-flex-row hawa-justify-between" }, /* @__PURE__ */ React3.createElement( "p", { className: cn( "hawa-my-0 hawa-text-start hawa-text-xs hawa-text-helper-color hawa-transition-all", helperText ? "hawa-h-4 hawa-opacity-100" : "hawa-h-0 hawa-opacity-0" ) }, helperText ), /* @__PURE__ */ React3.createElement("div", { className: "clickable-link hawa-w-fit", onClick: () => clear() }, (texts == null ? void 0 : texts.clear) || "Clear"))); }; export { Signature }; //# sourceMappingURL=index.mjs.map