41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
'use client';
|
|
|
|
import * as React from 'react';
|
|
import { Copy, Check } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { toast } from 'sonner';
|
|
|
|
interface CodeSnippetProps {
|
|
code: string;
|
|
language?: string;
|
|
}
|
|
|
|
export function CodeSnippet({ code, language }: CodeSnippetProps) {
|
|
const [copied, setCopied] = React.useState(false);
|
|
|
|
const handleCopy = () => {
|
|
navigator.clipboard.writeText(code);
|
|
setCopied(true);
|
|
toast.success('Copied to clipboard');
|
|
setTimeout(() => setCopied(false), 2000);
|
|
};
|
|
|
|
return (
|
|
<div className="relative group">
|
|
<div className="absolute right-4 top-4 opacity-0 group-hover:opacity-100 transition-opacity">
|
|
<Button
|
|
variant="secondary"
|
|
size="icon-xs"
|
|
onClick={handleCopy}
|
|
className="bg-background/50 backdrop-blur-md border border-border"
|
|
>
|
|
{copied ? <Check className="h-3 w-3" /> : <Copy className="h-3 w-3" />}
|
|
</Button>
|
|
</div>
|
|
<pre className="p-4 rounded-lg bg-input backdrop-blur-sm border border-border overflow-x-auto font-mono text-xs text-muted-foreground leading-relaxed">
|
|
<code>{code}</code>
|
|
</pre>
|
|
</div>
|
|
);
|
|
}
|