-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode-display-block.tsx
53 lines (49 loc) · 1.5 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
'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 rounded-lg border bg-muted">
<Button
onClick={copyToClipboard}
variant="secondary"
size="icon"
className="h-6 w-6 absolute top-3 right-3 opacity-50 hover:opacity-100 transition-opacity"
>
{isCopied ? (
<CheckIcon className="w-4 h-4 scale-100 transition-all" />
) : (
<CopyIcon className="w-4 h-4 scale-100 transition-all" />
)}
</Button>
<CodeBlock
customStyle={{
background: 'transparent',
padding: '1rem',
}}
text={code}
language="tsx"
showLineNumbers={false}
theme={theme === 'dark' ? dracula : github}
/>
</div>
);
}