{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "color-picker",
  "title": "Color Picker",
  "description": "Hex color input that reads and writes a 6-digit hex, with a swatch that opens a saturation and hue picker.",
  "dependencies": [
    "react-colorful@^5.8.1"
  ],
  "registryDependencies": [
    "button",
    "input-group",
    "popover"
  ],
  "files": [
    {
      "path": "components/dentil/color-picker/color-picker.tsx",
      "content": "'use client';\n\nimport { HexColorPicker } from 'react-colorful';\nimport * as React from 'react';\n\nimport { Button } from '@/components/ui/button';\nimport { InputGroup, InputGroupAddon, InputGroupInput, InputGroupText } from '@/components/ui/input-group';\nimport { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';\nimport { cn } from '@/lib/utils';\n\nconst HEX = /^[0-9a-f]{6}$/;\n\n/** A saturated color, so an empty picker doesn't open on black. */\nfunction randomHex() {\n  const hue = Math.floor(Math.random() * 360);\n  const saturation = 0.65;\n  const lightness = 0.52;\n  const chroma = saturation * Math.min(lightness, 1 - lightness);\n  const channel = (n: number) => {\n    const k = (n + hue / 30) % 12;\n    const value = lightness - chroma * Math.max(Math.min(k - 3, 9 - k, 1), -1);\n    return Math.round(255 * value)\n      .toString(16)\n      .padStart(2, '0');\n  };\n  return `${channel(0)}${channel(8)}${channel(4)}`;\n}\n\n/** Six hex digits, with a leading `#` and any other characters removed. */\nfunction toHex(value: string) {\n  return value\n    .replace(/^#/, '')\n    .replace(/[^0-9a-fA-F]/g, '')\n    .slice(0, 6)\n    .toLowerCase();\n}\n\nexport interface ColorPickerProps extends Omit<\n  React.ComponentProps<'input'>,\n  'value' | 'onChange' | 'type' | 'defaultValue' | 'size'\n> {\n  /** 6-digit hex without `#`, e.g. `'ed5656'`. A leading `#` is ignored. */\n  value: string | null | undefined;\n  /** Called with the hex without `#`, including a partial value. Non-hex characters are removed. */\n  onChange: (value: string) => void;\n}\n\nexport function ColorPicker({\n  value,\n  onChange,\n  disabled,\n  placeholder = 'ed5656',\n  className,\n  ...props\n}: ColorPickerProps) {\n  const hex = toHex(value ?? '');\n  const swatch = HEX.test(hex) ? `#${hex}` : undefined;\n  // Keep the last valid color so a half-typed value doesn't jump the picker.\n  const [fallback, setFallback] = React.useState(() => swatch ?? `#${randomHex()}`);\n  if (swatch && fallback !== swatch) setFallback(swatch);\n\n  const commit = (next: string) => onChange(toHex(next));\n\n  return (\n    <div data-slot=\"color-picker\" className={cn('flex w-full items-center gap-2', className)}>\n      <InputGroup data-disabled={disabled ? true : undefined} className=\"min-w-0 flex-1\">\n        <InputGroupAddon>\n          <InputGroupText>#</InputGroupText>\n        </InputGroupAddon>\n        <InputGroupInput\n          placeholder={placeholder}\n          disabled={disabled}\n          spellCheck={false}\n          autoComplete=\"off\"\n          autoCapitalize=\"off\"\n          {...props}\n          value={hex}\n          onChange={(event) => commit(event.target.value)}\n          className=\"font-mono\"\n        />\n      </InputGroup>\n      <Popover>\n        <PopoverTrigger asChild>\n          <Button\n            type=\"button\"\n            variant=\"outline\"\n            size=\"icon\"\n            disabled={disabled}\n            aria-label={swatch ? `Pick a color, current ${swatch}` : 'Pick a color'}\n            className={cn('shrink-0', !swatch && 'bg-muted')}\n            style={swatch ? { backgroundColor: swatch } : undefined}\n          />\n        </PopoverTrigger>\n        <PopoverContent align=\"start\" className=\"w-auto p-3\">\n          <div className=\"flex w-[200px] flex-col gap-3\">\n            <HexColorPicker\n              color={swatch ?? fallback}\n              onChange={commit}\n              className=\"focus-within:ring-ring/50 rounded-md focus-within:ring-2\"\n            />\n            <InputGroup className=\"w-full\">\n              <InputGroupAddon>\n                <InputGroupText>#</InputGroupText>\n              </InputGroupAddon>\n              <InputGroupInput\n                value={hex}\n                onChange={(event) => commit(event.target.value)}\n                disabled={disabled}\n                spellCheck={false}\n                autoComplete=\"off\"\n                autoCapitalize=\"off\"\n                aria-label=\"Hex color\"\n                className=\"font-mono\"\n              />\n            </InputGroup>\n          </div>\n        </PopoverContent>\n      </Popover>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/dentil/color-picker/color-picker.tsx"
    }
  ],
  "type": "registry:component"
}
