Etiquetas y estados
Color Picker
Muestras de color predefinidas y color libre (rhf-color-picker de ZasExpress, controlado).
Uso
Instalación
Con la CLI de zasui (ver Introducción): instala el componente con sus dependencias y queda listo para usar.
pnpm dlx @medialityc/zasui add color-pickerVer el código
'use client';
import { cn } from '@/lib/utils';
// Base: react-hook-form/rhf-color-picker.tsx de ZasExpress. Cambia: controlado (value/onChange/error) en vez de
// leer el formulario, para no atar la librería a react-hook-form; en el front va dentro de un <Controller>.
interface ColorPickerProps {
value?: string;
onChange: (color: string) => void;
id?: string;
label?: string;
helperText?: string;
error?: string;
disabled?: boolean;
required?: boolean;
colorPresets?: string[];
showColorInput?: boolean;
containerClassName?: string;
}
const defaultColorPresets = ['#2563eb', '#22c55e', '#f59e42', '#ef4444', '#a21caf', '#64748b'];
export function ColorPicker({
value,
onChange,
id,
label,
helperText,
error,
disabled = false,
required = false,
colorPresets = defaultColorPresets,
showColorInput = true,
containerClassName,
}: ColorPickerProps) {
return (
<div className={cn('space-y-2', containerClassName)}>
{label && (
<label htmlFor={id} className='text-sm font-medium text-gray-700 dark:text-gray-200'>
{label}
{required && <span className='ml-1 text-red-500'>*</span>}
</label>
)}
<div className='flex items-center gap-4'>
<div className='flex flex-wrap gap-2'>
{colorPresets.map((color) => (
<button
key={color}
type='button'
disabled={disabled}
className={cn(
'h-8 w-8 rounded-full border-2 transition-all duration-200 hover:scale-110',
value === color ? 'border-gray-400 ring-2 ring-gray-300' : 'border-gray-200 hover:border-gray-300',
disabled && 'cursor-not-allowed opacity-50 hover:scale-100',
)}
style={{ backgroundColor: color }}
onClick={() => !disabled && onChange(color)}
aria-label={`Seleccionar color ${color}`}
/>
))}
</div>
{showColorInput && (
<div className='flex items-center gap-2'>
<svg className='h-4 w-4 text-gray-500' fill='none' viewBox='0 0 24 24' stroke='currentColor'>
<path
strokeLinecap='round'
strokeLinejoin='round'
strokeWidth={2}
d='M7 21a4 4 0 01-4-4V5a2 2 0 012-2h4a2 2 0 012 2v12a4 4 0 01-4 4zM21 5a2 2 0 00-2-2h-4a2 2 0 00-2 2v12a4 4 0 004 4h4a2 2 0 002-2V5z'
/>
</svg>
<input
type='color'
id={id}
value={value || '#000000'}
onChange={(e) => onChange(e.target.value)}
disabled={disabled}
className={cn(
'h-8 w-16 cursor-pointer rounded border p-1 disabled:cursor-not-allowed',
'h-10 w-10 cursor-pointer appearance-none rounded-md border-2 border-gray-300 transition-all hover:scale-105',
disabled && 'opacity-50',
)}
/>
</div>
)}
</div>
{helperText && !error && <p className='text-xs text-gray-500'>{helperText}</p>}
{error && <p className='text-xs text-red-500'>{error}</p>}
</div>
);
}