From 850766d54f2f0f2b2cc4671825310c80da997ab7 Mon Sep 17 00:00:00 2001 From: Dmitriy Date: Wed, 22 Apr 2026 06:12:30 +0300 Subject: [PATCH] fix: handle undefined navigator.clipboard with fallback (#697) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add optional chaining check for navigator.clipboard - Fallback to textarea + execCommand for SSR/non-HTTPS contexts - Fixes TypeError when clipboard API is unavailable Co-authored-by: Дмитрий Золотарь --- src/shared/hooks/useCopyToClipboard.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/shared/hooks/useCopyToClipboard.js b/src/shared/hooks/useCopyToClipboard.js index 558aad8..fa5afed 100644 --- a/src/shared/hooks/useCopyToClipboard.js +++ b/src/shared/hooks/useCopyToClipboard.js @@ -12,7 +12,21 @@ export function useCopyToClipboard(resetDelay = 2000) { const timeoutRef = useRef(null); const copy = useCallback((text, id = "default") => { - navigator.clipboard.writeText(text); + const write = async () => { + if (navigator?.clipboard?.writeText) { + await navigator.clipboard.writeText(text); + } else { + const textarea = document.createElement("textarea"); + textarea.value = text; + textarea.style.position = "fixed"; + textarea.style.opacity = "0"; + document.body.appendChild(textarea); + textarea.select(); + document.execCommand("copy"); + document.body.removeChild(textarea); + } + }; + write(); setCopied(id); if (timeoutRef.current) {