import React, { useState, useRef } from 'react'; interface ColorPaletteProps { colors: string[]; size?: 'small' | 'large'; } const ColorPalette: React.FC = ({ colors, size = 'small' }) => { const [copiedColor, setCopiedColor] = useState(null); const [hoveredColorId, setHoveredColorId] = useState(null); const textAreaRef = useRef(null); const handleColorClick = (color: string) => { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(color).then(() => { setCopiedColor(color); setTimeout(() => setCopiedColor(null), 1500); }); } else { // Fallback method for iOS and other unsupported browsers const textArea = textAreaRef.current; if (textArea) { textArea.value = color; textArea.select(); try { document.execCommand('copy'); setCopiedColor(color); setTimeout(() => setCopiedColor(null), 1500); } catch (err) { console.error('Failed to copy color: ', err); } textArea.blur(); } } }; const sizeClasses = size === 'small' ? 'w-full pt-full' : 'w-8 h-8 pt-full'; return ( <>
{colors.map((color, index) => { const colorId = `color-${index}-${color}`; return (
handleColorClick(color)} onMouseEnter={() => setHoveredColorId(colorId)} onMouseLeave={() => setHoveredColorId(null)} > {size === 'small' && hoveredColorId === colorId && (
{color}
)} {size === 'large' && (
{color}
)} {copiedColor === color && (
Copied!
)}
); })}