import React from "react"; import { CHIP_COLORS, cls } from "../util"; import { ChipColorKey, ChipColorScheme } from "./Chip"; import { CheckIcon } from "../icons"; import { Tooltip } from "./Tooltip"; export interface ColorPickerProps { /** * Currently selected color key */ value?: ChipColorKey; /** * Callback when color selection changes. Passes undefined when "Auto" is selected. */ onChange: (colorKey: ChipColorKey | undefined) => void; /** * Size of the color swatches */ size?: "small" | "medium"; /** * Whether to show the "Auto" option that clears the selection */ allowClear?: boolean; /** * Whether the picker is disabled */ disabled?: boolean; } // Base colors in display order const BASE_COLORS = ["blue", "cyan", "teal", "green", "yellow", "orange", "red", "pink", "purple", "gray"] as const; // Variants in display order (darker to lighter for better visual flow) const VARIANTS = ["Darker", "Dark", "Light", "Lighter"] as const; // Helper to get readable name from color key function getColorDisplayName(colorKey: ChipColorKey): string { // Convert camelCase to readable format: "blueLighter" -> "Blue Lighter" const base = colorKey.replace(/(Lighter|Light|Dark|Darker)$/, ""); const variant = colorKey.replace(base, ""); return `${base.charAt(0).toUpperCase()}${base.slice(1)} ${variant}`; } /** * A color picker component that displays a grid of predefined CHIP_COLORS. * Used for selecting colors for enum values, tags, and other chip-based UI elements. * * @group Form components */ export function ColorPicker({ value, onChange, size = "medium", allowClear = true, disabled = false }: ColorPickerProps) { const swatchSize = size === "small" ? "w-5 h-5" : "w-6 h-6"; const checkSize = size === "small" ? 12 : 14; return (
{allowClear && ( )}
{VARIANTS.map((variant) => ( BASE_COLORS.map((base) => { const colorKey = `${base}${variant}` as ChipColorKey; const colorScheme = CHIP_COLORS[colorKey] as ChipColorScheme; const isSelected = value === colorKey; const displayName = getColorDisplayName(colorKey); return ( ); }) ))}
); }