-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode-display-block.tsx
54 lines (50 loc) · 1.47 KB
/
code-display-block.tsx
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
52
53
54
'use client';
import { CheckIcon, CopyIcon } from '@radix-ui/react-icons';
import React from 'react';
import { CodeBlock, dracula, github } from 'react-code-blocks';
import { Button } from './ui/button';
import { toast } from 'sonner';
import { useTheme } from 'next-themes';
interface ButtonCodeblockProps {
code: string;
lang: string;
}
export default function CodeDisplayBlock({ code, lang }: ButtonCodeblockProps) {
const [isCopied, setisCopied] = React.useState(false);
const { theme } = useTheme();
const copyToClipboard = () => {
navigator.clipboard.writeText(code);
setisCopied(true);
toast.success('Code copied to clipboard!');
setTimeout(() => {
setisCopied(false);
}, 1500);
};
return (
<div className="relative my-4 overflow-scroll overflow-x-scroll flex flex-col text-start ">
<Button
onClick={copyToClipboard}
variant="ghost"
size="icon"
className="h-5 w-5 absolute top-2 right-2"
>
{isCopied ? (
<CheckIcon className="w-4 h-4 scale-100 transition-all" />
) : (
<CopyIcon className="w-4 h-4 scale-100 transition-all" />
)}
</Button>
<CodeBlock
customStyle={
theme === 'dark'
? { background: '#303033' }
: { background: '#fcfcfc' }
}
text={code}
language="tsx"
showLineNumbers={false}
theme={theme === 'dark' ? dracula : github}
/>
</div>
);
}