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

Commit 5b37744

Browse files
authored
Fix: replace copy to clipboard component (#28)
1 parent 8a436cc commit 5b37744

File tree

4 files changed

+36
-52
lines changed

4 files changed

+36
-52
lines changed

package-lock.json

Lines changed: 0 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@
2121
"@radix-ui/react-switch": "^1.1.1",
2222
"@radix-ui/react-tooltip": "^1.1.6",
2323
"@types/prismjs": "^1.26.5",
24-
"@types/react-copy-to-clipboard": "^5.0.7",
2524
"@types/react-syntax-highlighter": "^15.5.13",
2625
"class-variance-authority": "^0.7.1",
2726
"clsx": "^2.1.1",
2827
"date-fns": "^4.1.0",
2928
"lucide-react": "^0.462.0",
3029
"prismjs": "^1.29.0",
3130
"react": "^18.3.1",
32-
"react-copy-to-clipboard": "^5.1.0",
3331
"react-dom": "^18.3.1",
3432
"react-markdown": "^9.0.1",
3533
"react-router-dom": "^7.0.2",
@@ -61,4 +59,4 @@
6159
"typescript-eslint": "^8.15.0",
6260
"vite": "^6.0.1"
6361
}
64-
}
62+
}

src/components/CopyToClipboard.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import clsx from "clsx";
2+
import { ClipboardCopy } from "lucide-react";
3+
4+
export function CopyToClipboard({
5+
text,
6+
className,
7+
}: {
8+
className?: string;
9+
text: string;
10+
}) {
11+
const copyToClipboard = async () => {
12+
try {
13+
await navigator.clipboard.writeText(text);
14+
} catch {
15+
return;
16+
}
17+
};
18+
19+
return (
20+
<button
21+
onClick={copyToClipboard}
22+
className={clsx(
23+
`absolute top-4 right-8 p-2 rounded-md
24+
bg-gray-700/50 hover:bg-gray-700/70
25+
transition-opacity duration-200
26+
opacity-0 group-hover:opacity-100`,
27+
className,
28+
)}
29+
>
30+
<ClipboardCopy className="w-5 h-5 text-gray-200" />
31+
</button>
32+
);
33+
}

src/components/Markdown.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import remarkGfm from "remark-gfm";
22
import ReactMarkdown from "react-markdown";
33
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
44
import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
5-
import { CopyToClipboard } from "react-copy-to-clipboard";
6-
import { ClipboardCopy } from "lucide-react";
75
import { cn } from "@/lib/utils";
6+
import { CopyToClipboard } from "./CopyToClipboard";
87

98
interface Props {
109
children: string;
@@ -61,18 +60,7 @@ export function Markdown({ children, className = "" }: Props) {
6160
>
6261
{String(children).replace(/\n$/, "")}
6362
</SyntaxHighlighter>
64-
<CopyToClipboard text={String(children).replace(/\n$/, "")}>
65-
<button
66-
className="
67-
absolute top-4 right-8 p-2 rounded-md
68-
bg-gray-700/50 hover:bg-gray-700/70
69-
transition-opacity duration-200
70-
opacity-0 group-hover:opacity-100
71-
"
72-
>
73-
<ClipboardCopy className="w-5 h-5 text-gray-200" />
74-
</button>
75-
</CopyToClipboard>
63+
<CopyToClipboard text={String(children).replace(/\n$/, "")} />
7664
</div>
7765
) : (
7866
<SyntaxHighlighter

0 commit comments

Comments
 (0)