From 89f260bc7895713f32cf1361b4912f3f893ecbe4 Mon Sep 17 00:00:00 2001 From: Danny Avila <110412045+danny-avila@users.noreply.github.com> Date: Mon, 14 Aug 2023 10:12:00 -0400 Subject: [PATCH] fix(CodeBlock.tsx): fix copy-to-clipboard functionality. The code has been updated to use the copy function from the copy-to-clipboard library instead of the (#806) avigator.clipboard.writeText method. This should fix the issue with browser incompatibility with navigator SDK and allow users to copy code from the CodeBlock component successfully. --- client/src/components/Messages/Content/CodeBlock.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/client/src/components/Messages/Content/CodeBlock.tsx b/client/src/components/Messages/Content/CodeBlock.tsx index f1ffca6d0..c61fc65a2 100644 --- a/client/src/components/Messages/Content/CodeBlock.tsx +++ b/client/src/components/Messages/Content/CodeBlock.tsx @@ -1,4 +1,5 @@ import React, { useRef, useState, RefObject } from 'react'; +import copy from 'copy-to-clipboard'; import { Clipboard, CheckMark } from '~/components'; import { InfoIcon } from 'lucide-react'; import { cn } from '~/utils/'; @@ -22,10 +23,12 @@ const CodeBar: React.FC = React.memo(({ lang, codeRef, plugin = nu onClick={async () => { const codeString = codeRef.current?.textContent; if (codeString) { - navigator.clipboard.writeText(codeString).then(() => { - setIsCopied(true); - setTimeout(() => setIsCopied(false), 3000); - }); + setIsCopied(true); + copy(codeString); + + setTimeout(() => { + setIsCopied(false); + }, 3000); } }} >