From e165ce232a1470386b33f524bbe4f4d782908ff6 Mon Sep 17 00:00:00 2001 From: Kieran Date: Fri, 29 Sep 2023 09:59:58 +0100 Subject: [PATCH] Fix copy for insecure context --- packages/app/src/useCopy.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/app/src/useCopy.ts b/packages/app/src/useCopy.ts index dde650bb..c7cfda5f 100644 --- a/packages/app/src/useCopy.ts +++ b/packages/app/src/useCopy.ts @@ -5,10 +5,21 @@ export const useCopy = (timeout = 2000) => { const [copied, setCopied] = useState(false); const copy = async (text: string) => { + setError(false); try { - await navigator.clipboard.writeText(text); + if (navigator.clipboard && window.isSecureContext) { + navigator.clipboard.writeText(text); + } else { + const textArea = document.createElement("textarea"); + textArea.value = text; + textArea.style.position = "absolute"; + textArea.style.opacity = "0"; + document.body.appendChild(textArea); + textArea.select(); + await document.execCommand("copy"); + textArea.remove(); + } setCopied(true); - setError(false); } catch (error) { setError(true); }