From 1e5ed3b6eaa5e1bea41d376fe5407d890de9e787 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 ee98846c..965b2966 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:// + 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); if (timeout) clearTimeout(timeout);