Skip to content

The Open Source DocuSign Alternative.

fix: debounce display of signing canvas

Mythie4dcb0a684dba
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<boolean>(props.open, 1);
let signCanvasRef: any | undefined;
useEffect(() => {
@@ -126,19 +130,21 @@ export default function SignatureDialog(props: any) {
""
)}
{isCurrentTab("Draw") ? (
- <div className="">
- <SignatureCanvas
- ref={(ref) => {
- signCanvasRef = ref;
- }}
- canvasProps={{
- className: "sigCanvas border-b b-2 border-slate w-full h-full mb-3",
- }}
- clearOnResize={true}
- onEnd={() => {
- setSignatureEmpty(signCanvasRef?.isEmpty());
- }}
- />
+ <div className="" key={props.open ? "closed" : "open"}>
+ {showCanvas && (
+ <SignatureCanvas
+ ref={(ref) => {
+ signCanvasRef = ref;
+ }}
+ canvasProps={{
+ className: "sigCanvas border-b b-2 border-slate w-full h-full mb-3",
+ }}
+ clearOnResize={true}
+ onEnd={() => {
+ setSignatureEmpty(signCanvasRef?.isEmpty());
+ }}
+ />
+ )}
<IconButton
className="float-left block"
icon={TrashIcon}
diff --git a/apps/web/hooks/use-debounced-value.ts b/apps/web/hooks/use-debounced-value.ts
new file mode 100644
index 000000000..eebbec716
--- /dev/null
+++ b/apps/web/hooks/use-debounced-value.ts
@@ -0,0 +1,18 @@
+import { useEffect, useState } from "react";
+
+export function useDebouncedValue<T>(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;
+}