From e82c86b365c618d1d74f986b51b48d26f4cb2ee9 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Sun, 1 Oct 2023 01:51:32 -0500 Subject: [PATCH] Handle Copy for http context --- src/utils/useCopy.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils/useCopy.ts b/src/utils/useCopy.ts index ee98846c7..88e07d0ad 100644 --- a/src/utils/useCopy.ts +++ b/src/utils/useCopy.ts @@ -19,8 +19,18 @@ export const useCopy = ({ copiedTimeout = 2000 }: UseCopyProps = {}): [ await Clipboard.write({ string: text }); - } else { + } else if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(text); + } else { + // handle if running on http:// + let 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); if (timeout) clearTimeout(timeout);