Files
sign/apps/web/components/editor/signature-dialog.tsx

214 lines
8.4 KiB
TypeScript
Raw Normal View History

import { classNames } from "@documenso/lib";
import { Button, IconButton } from "@documenso/ui";
import { Dialog, Transition } from "@headlessui/react";
import {
LanguageIcon,
PencilIcon,
2023-02-17 19:08:23 +01:00
TrashIcon,
} from "@heroicons/react/24/outline";
import { Fragment, useEffect, useState } from "react";
2023-02-17 19:08:23 +01:00
import SignatureCanvas from "react-signature-canvas";
import { localStorage } from "@documenso/lib";
const tabs = [
2023-02-17 19:08:23 +01:00
{ name: "Type", icon: LanguageIcon, current: true },
{ name: "Draw", icon: PencilIcon, current: false },
];
export default function SignatureDialog(props: any) {
const [currentTab, setCurrentTab] = useState(tabs[0]);
const [typedSignature, setTypedSignature] = useState("");
2023-02-17 19:08:23 +01:00
const [signatureEmpty, setSignatureEmpty] = useState(true);
let signCanvasRef: any | undefined;
2023-02-17 19:08:23 +01:00
useEffect(() => {
setTypedSignature(localStorage.getItem("typedSignature") || "");
2023-02-24 17:04:51 +01:00
}, []);
return (
<>
<Transition.Root show={props.open} as={Fragment}>
2023-03-09 13:05:45 +01:00
<Dialog
as="div"
className="relative z-10"
onClose={() => {
props.setOpen(false);
setCurrent(tabs[0]);
}}
>
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" />
</Transition.Child>
<div className="fixed inset-0 z-10 overflow-y-auto">
<div className="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
enterTo="opacity-100 translate-y-0 sm:scale-100"
leave="ease-in duration-200"
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
2023-02-17 19:08:23 +01:00
<Dialog.Panel className="min-h-[350px] relative transform overflow-hidden rounded-lg bg-white px-4 pt-5 pb-4 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-sm sm:p-6">
2023-02-16 18:32:16 +01:00
<div className="">
<div className="border-b border-gray-200 mb-3">
<nav className="-mb-px flex space-x-8" aria-label="Tabs">
{tabs.map((tab) => (
2023-02-17 19:08:23 +01:00
<a
key={tab.name}
onClick={() => {
setCurrent(tab);
}}
className={classNames(
tab.current
? "border-neon text-neon"
: "border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300",
"group inline-flex items-center py-4 px-1 border-b-2 font-medium text-sm cursor-pointer"
)}
aria-current={tab.current ? "page" : undefined}
>
<tab.icon
className={classNames(
tab.current
2023-02-17 19:08:23 +01:00
? "text-neon"
: "text-gray-400 group-hover:text-gray-500",
"-ml-0.5 mr-2 h-5 w-5"
)}
2023-02-17 19:08:23 +01:00
aria-hidden="true"
/>
<span>{tab.name}</span>
</a>
))}
</nav>
</div>
{isCurrentTab("Type") ? (
2023-02-16 18:53:23 +01:00
<div>
<div className="my-8 border-b border-gray-300 mb-3">
2023-02-16 18:53:23 +01:00
<input
value={typedSignature}
2023-02-16 18:53:23 +01:00
onChange={(e) => {
setTypedSignature(e.target.value);
2023-02-16 18:53:23 +01:00
}}
className={classNames(
typedSignature ? "font-qwigley text-4xl" : "",
"leading-none h-10 align-bottom mt-14 text-center block w-full focus:border-neon focus:ring-neon text-2xl"
)}
2023-02-16 18:53:23 +01:00
placeholder="Kindly type your name"
/>
</div>
<div className="float-right">
<Button
color="secondary"
2023-02-24 16:43:42 +01:00
onClick={() => {
props.onClose();
props.setOpen(false);
2023-03-09 13:05:45 +01:00
setCurrent(tabs[0]);
2023-02-24 16:43:42 +01:00
}}
2023-02-16 18:53:23 +01:00
>
Cancel
</Button>
2023-02-17 13:37:28 +01:00
<Button
className="ml-3"
disabled={!typedSignature}
2023-02-17 13:37:28 +01:00
onClick={() => {
localStorage.setItem(
"typedSignature",
typedSignature
);
2023-02-17 13:37:28 +01:00
props.onClose({
type: "type",
typedSignature: typedSignature,
2023-02-17 13:37:28 +01:00
});
}}
>
2023-02-16 18:53:23 +01:00
Sign
</Button>
</div>
</div>
) : (
""
)}
{isCurrentTab("Draw") ? (
2023-02-17 19:08:23 +01:00
<div className="">
<SignatureCanvas
ref={(ref) => {
signCanvasRef = ref;
2023-02-17 19:08:23 +01:00
}}
canvasProps={{
className:
"sigCanvas border-b b-2 border-slate w-full h-full mb-3",
}}
2023-02-18 17:16:50 +01:00
clearOnResize={true}
2023-02-17 19:08:23 +01:00
onEnd={() => {
setSignatureEmpty(signCanvasRef?.isEmpty());
2023-02-17 19:08:23 +01:00
}}
/>
<IconButton
className="block float-left"
icon={TrashIcon}
onClick={() => {
signCanvasRef?.clear();
setSignatureEmpty(signCanvasRef?.isEmpty());
2023-02-17 19:08:23 +01:00
}}
></IconButton>
<div className="mt-10 float-right">
2023-02-16 18:53:23 +01:00
<Button
color="secondary"
2023-02-24 16:43:42 +01:00
onClick={() => {
props.onClose();
props.setOpen(false);
2023-03-09 13:05:45 +01:00
setCurrent(tabs[0]);
2023-02-24 16:43:42 +01:00
}}
2023-02-16 18:53:23 +01:00
>
Cancel
</Button>
2023-02-17 19:08:23 +01:00
<Button
className="ml-3"
onClick={() => {
props.onClose({
type: "draw",
signatureImage:
signCanvasRef.toDataURL("image/png"),
2023-02-17 19:08:23 +01:00
});
}}
disabled={signatureEmpty}
>
2023-02-16 18:53:23 +01:00
Sign
</Button>
</div>
</div>
) : (
""
)}
</div>
</Dialog.Panel>
</Transition.Child>
</div>
</div>
</Dialog>
</Transition.Root>
</>
);
2023-03-09 13:05:45 +01:00
function isCurrentTab(tabName: string): boolean {
return currentTab.name === tabName;
}
2023-03-09 13:05:45 +01:00
function setCurrent(t: any) {
tabs.forEach((tab) => {
tab.current = tab.name === t.name;
});
setCurrentTab(t);
}
}