diff --git a/apps/web/components/editor/signature-dialog.tsx b/apps/web/components/editor/signature-dialog.tsx index 0dd442b83..6b986c111 100644 --- a/apps/web/components/editor/signature-dialog.tsx +++ b/apps/web/components/editor/signature-dialog.tsx @@ -5,6 +5,7 @@ import { Button, IconButton } from "@documenso/ui"; import { Dialog, Transition } from "@headlessui/react"; import { LanguageIcon, PencilIcon, TrashIcon } from "@heroicons/react/24/outline"; import SignatureCanvas from "react-signature-canvas"; +import { useDebouncedValue } from "../../hooks/use-debounced-value"; const tabs = [ { name: "Type", icon: LanguageIcon, current: true }, @@ -15,6 +16,9 @@ export default function SignatureDialog(props: any) { const [currentTab, setCurrentTab] = useState(tabs[0]); const [typedSignature, setTypedSignature] = useState(""); const [signatureEmpty, setSignatureEmpty] = useState(true); + // This is a workaround to prevent the canvas from being rendered when the dialog is closed + // we also need the debounce to avoid rendering while transitions are occuring. + const showCanvas = useDebouncedValue(props.open, 1); let signCanvasRef: any | undefined; useEffect(() => { @@ -126,19 +130,21 @@ export default function SignatureDialog(props: any) { "" )} {isCurrentTab("Draw") ? ( -
- { - signCanvasRef = ref; - }} - canvasProps={{ - className: "sigCanvas border-b b-2 border-slate w-full h-full mb-3", - }} - clearOnResize={true} - onEnd={() => { - setSignatureEmpty(signCanvasRef?.isEmpty()); - }} - /> +
+ {showCanvas && ( + { + signCanvasRef = ref; + }} + canvasProps={{ + className: "sigCanvas border-b b-2 border-slate w-full h-full mb-3", + }} + clearOnResize={true} + onEnd={() => { + setSignatureEmpty(signCanvasRef?.isEmpty()); + }} + /> + )} (value: T, delay: number) { + // State and setters for debounced value + const [debouncedValue, setDebouncedValue] = useState(value); + + useEffect(() => { + const handler = setTimeout(() => { + setDebouncedValue(value); + }, delay); + + return () => { + clearTimeout(handler); + }; + }, [value, delay]); + + return debouncedValue; +}