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.
This commit is contained in:
Danny Avila 2023-08-14 10:12:00 -04:00 committed by GitHub
parent d00c7354cd
commit 89f260bc78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<CodeBarProps> = 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);
}
}}
>