Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit fafc301

Browse files
committed
fix code copy button
1 parent 2f1c91c commit fafc301

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/components/copy-button.jsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,28 @@ const SidebarIcon = styled.div`
2929
const CopyButton = ({text, ...props}) => {
3030
const alert = useAlert()
3131
const clicked = () => {
32-
navigator.clipboard.writeText(text).then(() => {
32+
if (navigator.clipboard && window.isSecureContext) {
33+
navigator.clipboard.writeText(text).then(() => {
34+
alert.show('Copied to clipboard');
35+
});
36+
37+
return;
38+
}
39+
40+
let textArea = document.createElement("textarea");
41+
textArea.value = text;
42+
// make the textarea out of viewport
43+
textArea.style.position = "fixed";
44+
textArea.style.left = "-999999px";
45+
textArea.style.top = "-999999px";
46+
document.body.appendChild(textArea);
47+
textArea.focus();
48+
textArea.select();
49+
50+
return new Promise((res, rej) => {
51+
// here the magic happens
52+
document.execCommand('copy') ? res() : rej();
53+
textArea.remove();
3354
alert.show('Copied to clipboard');
3455
});
3556
}

0 commit comments

Comments
 (0)