forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-code-block.tsx
More file actions
51 lines (44 loc) · 1.5 KB
/
Copy pathcustom-code-block.tsx
File metadata and controls
51 lines (44 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use client";
import { useState } from "react";
import { Check, Copy } from "lucide-react";
interface CustomCodeBlockProps {
code: string;
}
const CustomCodeBlock = ({ code }: CustomCodeBlockProps) => {
const [copied, setCopied] = useState(false);
const handleCopy = async () => {
await navigator.clipboard.writeText(code);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
const highlightCode = (text: string) => {
return text.replace(
/(\S+@\S+)/g,
'<span class="text-emerald-600 dark:text-emerald-400">$1</span>',
);
};
return (
<div className="relative mt-4 rounded-lg border border-black/6 bg-[#FAFAFA] dark:border-white/10 dark:bg-white/5">
<div className="flex items-center justify-between px-4 py-3">
<pre className="text-sm">
<code
className="font-mono text-[#010507] bg-transparent dark:text-white border-none!"
dangerouslySetInnerHTML={{ __html: highlightCode(code) }}
/>
</pre>
<button
onClick={handleCopy}
className="ml-4 shrink-0 rounded p-1.5 text-gray-500 transition-colors hover:bg-black/5 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-white/10 dark:hover:text-gray-200 cursor-pointer"
aria-label="Copy code"
>
{copied ? (
<Check className="h-4 w-4" />
) : (
<Copy className="h-4 w-4" />
)}
</button>
</div>
</div>
);
};
export default CustomCodeBlock;