import {HexColorInput, HexColorPicker} from 'react-colorful'; import React, {useState} from 'react'; import {parseColor} from '@react-stately/color'; import {ColorSwatch} from './color-swatch'; import {getInputFieldClassNames} from '../forms/input-field/get-input-field-class-names'; import {ColorPresets} from '@common/ui/color-picker/color-presets'; const DefaultPresets = ColorPresets.map(({color}) => color).slice(0, 14); type Props = { defaultValue?: string; onChange?: (e: string) => void; colorPresets?: string[]; showInput?: boolean; }; export function ColorPicker({ defaultValue, onChange, colorPresets, showInput, }: Props) { const [color, setColor] = useState(defaultValue); const presets: string[] = colorPresets || DefaultPresets; const style = getInputFieldClassNames({size: 'sm'}); return (
{ onChange?.(newColor); setColor(newColor); }} />
{presets && ( { if (newColor) { const hex = parseColor(newColor).toString('hex'); onChange?.(hex); setColor(hex); } }} value={color} /> )} {showInput && (
{ onChange?.(newColor); setColor(newColor); }} />
)}
); }