66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
|
|
'use client';
|
||
|
|
|
||
|
|
import { cn } from '@/lib/utils/cn';
|
||
|
|
import { Check, Copy } from 'lucide-react';
|
||
|
|
import { useState } from 'react';
|
||
|
|
import { toast } from 'sonner';
|
||
|
|
|
||
|
|
interface ColorSwatchProps {
|
||
|
|
color: string;
|
||
|
|
size?: 'sm' | 'md' | 'lg';
|
||
|
|
showLabel?: boolean;
|
||
|
|
onClick?: () => void;
|
||
|
|
className?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function ColorSwatch({
|
||
|
|
color,
|
||
|
|
size = 'md',
|
||
|
|
showLabel = true,
|
||
|
|
onClick,
|
||
|
|
className,
|
||
|
|
}: ColorSwatchProps) {
|
||
|
|
const [copied, setCopied] = useState(false);
|
||
|
|
|
||
|
|
const sizeClasses = {
|
||
|
|
sm: 'h-12 w-12',
|
||
|
|
md: 'h-16 w-16',
|
||
|
|
lg: 'h-24 w-24',
|
||
|
|
};
|
||
|
|
|
||
|
|
const handleCopy = (e: React.MouseEvent) => {
|
||
|
|
e.stopPropagation();
|
||
|
|
navigator.clipboard.writeText(color);
|
||
|
|
setCopied(true);
|
||
|
|
toast.success(`Copied ${color}`);
|
||
|
|
setTimeout(() => setCopied(false), 2000);
|
||
|
|
};
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div className={cn('flex flex-col items-center gap-2', className)}>
|
||
|
|
<button
|
||
|
|
className={cn(
|
||
|
|
'relative rounded-lg ring-2 ring-border transition-all hover:scale-105 hover:ring-primary',
|
||
|
|
'focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2',
|
||
|
|
'group',
|
||
|
|
sizeClasses[size]
|
||
|
|
)}
|
||
|
|
style={{ backgroundColor: color }}
|
||
|
|
onClick={onClick || handleCopy}
|
||
|
|
aria-label={`Color ${color}`}
|
||
|
|
>
|
||
|
|
<div className="absolute inset-0 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity bg-black/20 rounded-lg">
|
||
|
|
{copied ? (
|
||
|
|
<Check className="h-5 w-5 text-white" />
|
||
|
|
) : (
|
||
|
|
<Copy className="h-5 w-5 text-white" />
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
</button>
|
||
|
|
{showLabel && (
|
||
|
|
<span className="text-xs font-mono text-muted-foreground">{color}</span>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|