import { ChangeEvent, forwardRef, useState, useEffect } from "react"; import { InputRoot, InputRootProps, InputSlot } from "./input"; import { cn } from "../../hooks"; interface ColorInputProps extends Omit { className?: string; /** Color value - supports hex strings, 0x-prefixed strings, numeric hex values, and CSS color names */ value?: string | number; /** Called with normalized hex string (without #) when color changes */ onValueChange?: (hexColor: string) => void; } const ColorInput = forwardRef(function ColorInput( { className, value, defaultValue, onChange, onValueChange, ...props }, ref, ) { // Normalize color value from various formats to a hex string (without #) // Supports: "#aabbcc", "aabbcc", "0xaabbcc", 0xaabbcc (number), "rgb(...)", etc. const normalizeColor = (colorValue: string | number | readonly string[] | undefined): string => { if (colorValue === undefined || colorValue === null) { return "ffffff"; } // Handle numeric values (e.g., 0xaabbcc or 0xaabbccdd) if (typeof colorValue === "number") { // Check if it's a 32-bit value (with alpha) or 24-bit (RGB only) if (colorValue > 0xffffff) { // 0xaabbccdd format - extract RGB, ignore alpha for HTML color input const hex = colorValue.toString(16).padStart(8, "0"); return hex.slice(0, 6); // Take first 6 chars (RGB) } return colorValue.toString(16).padStart(6, "0"); } if (typeof colorValue === "string") { const trimmed = colorValue.trim().toLowerCase(); // Handle 0x prefix (e.g., "0xaabbcc" or "0xaabbccdd") if (trimmed.startsWith("0x")) { const hex = trimmed.slice(2); if (hex.length > 6) { return hex.slice(0, 6); // Take first 6 chars for RGBA format } return hex.padStart(6, "0"); } // Handle # prefix if (trimmed.startsWith("#")) { const hex = trimmed.slice(1); // Handle shorthand (#abc -> aabbcc) if (hex.length === 3) { return hex .split("") .map((c) => c + c) .join(""); } if (hex.length === 4) { // #rgba shorthand - ignore alpha return hex .slice(0, 3) .split("") .map((c) => c + c) .join(""); } if (hex.length > 6) { return hex.slice(0, 6); // Handle #rrggbbaa } return hex; } // Handle rgb/rgba functional notation const rgbMatch = trimmed.match(/^rgba?\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/); if (rgbMatch) { const r = Math.min(255, parseInt(rgbMatch[1], 10)).toString(16).padStart(2, "0"); const g = Math.min(255, parseInt(rgbMatch[2], 10)).toString(16).padStart(2, "0"); const b = Math.min(255, parseInt(rgbMatch[3], 10)).toString(16).padStart(2, "0"); return r + g + b; } // Plain hex string (e.g., "aabbcc" or "aabbccdd") if (/^[0-9a-f]+$/i.test(trimmed)) { if (trimmed.length === 3) { return trimmed .split("") .map((c) => c + c) .join(""); } if (trimmed.length > 6) { return trimmed.slice(0, 6); } return trimmed.padStart(6, "0"); } // Return as-is for CSS color names (e.g., "red", "blue") return trimmed; } return "ffffff"; }; const [color, setColor] = useState(normalizeColor(value || defaultValue)); // Update local state when prop value changes useEffect(() => { if (value !== undefined) { setColor(normalizeColor(value)); } }, [value]); return (
{ const newColor = e.currentTarget.value.replace(/^#/, ""); setColor(newColor); onValueChange?.(newColor); onChange?.(e); }} className={cn( [ "h-6", "w-6", "flex-none", "cursor-pointer", "rounded-1", "[appearance:none]", "[&::-webkit-color-swatch-wrapper]:p-0", "[&::-webkit-color-swatch]:border-line-primary", "[&::-webkit-color-swatch]:rounded-lg", "[&::-moz-color-swatch]:border-line-primary", "[&::-moz-color-swatch]:rounded-lg", ], className, )} /> ) => { const newColor = e.currentTarget.value; setColor(newColor); onValueChange?.(newColor); onChange?.(e); }} > #
); }); export { ColorInput }; export type { ColorInputProps };