{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "color-picker",
  "title": "Color Picker",
  "description": "A saturation square, hue and opacity sliders, typed hex and presets, in a popover, inline, as presets only, or as a compact field, with a thumb that never lags behind the hand.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://miseui.junsu4621.workers.dev/r/motion.json",
    "https://miseui.junsu4621.workers.dev/r/popover.json"
  ],
  "files": [
    {
      "path": "registry/miseui/ui/color-picker.tsx",
      "content": "import type * as React from \"react\"\nimport { hexToHsva, hsvaToCss } from \"@/registry/miseui/ui/color-picker-math\"\nimport { ColorPanel, HexField } from \"@/registry/miseui/ui/color-picker-panel\"\nimport { useColorState, type ColorState } from \"@/registry/miseui/ui/color-picker-state\"\nimport { CHECKER_BACKGROUND, SwatchGrid, type ColorSwatch } from \"@/registry/miseui/ui/color-picker-swatches\"\nimport { Popover, PopoverContent, PopoverTrigger } from \"@/registry/miseui/ui/popover\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\ntype ColorPickerVariant = \"popover\" | \"inline\" | \"swatches\" | \"compact\"\ntype ColorPickerEffect = \"spring\"\n\nconst DEFAULT_SWATCHES: readonly ColorSwatch[] = [\n  { value: \"#18181b\", label: \"Ink\" },\n  { value: \"#64748b\", label: \"Slate\" },\n  { value: \"#2563eb\", label: \"Blue\" },\n  { value: \"#0d9488\", label: \"Teal\" },\n  { value: \"#16a34a\", label: \"Green\" },\n  { value: \"#d97706\", label: \"Amber\" },\n  { value: \"#e11d48\", label: \"Rose\" },\n  { value: \"#7c3aed\", label: \"Violet\" },\n]\n\nconst PANEL_POPOVER_CLASS = \"w-64 max-w-(--radix-popover-content-available-width) p-4\"\n\ntype ColorPickerProps = Omit<React.ComponentProps<\"div\">, \"defaultValue\" | \"onChange\"> & {\n  /** Hex: #rrggbb, or #rrggbbaa when see-through. */\n  value?: string\n  defaultValue?: string\n  onValueChange?: (value: string) => void\n  /** `popover` a swatch button opens it, `inline` always open, `swatches` presets only, `compact` typed hex beside a swatch. */\n  variant?: ColorPickerVariant\n  /** `spring` eases the thumb when a click jumps it. A drag always follows the pointer exactly. */\n  effect?: ColorPickerEffect\n  /** Shows the opacity slider. Turned off, every colour stays opaque. */\n  alpha?: boolean\n  /** Presets, as a hex or `{ value, label }`. Pass an empty list to hide them. */\n  swatches?: readonly ColorSwatch[]\n  disabled?: boolean\n  /** Posts the hex with the surrounding form. */\n  name?: string\n  /** Names the trigger or the preset group. */\n  label?: string\n}\n\nfunction ColorSample({ color, className }: { color: string; className?: string }) {\n  return (\n    <span\n      aria-hidden=\"true\"\n      data-slot=\"color-picker-sample\"\n      className={cn(\"relative block size-5 shrink-0 overflow-hidden rounded-sm\", className)}\n      style={{ background: CHECKER_BACKGROUND }}\n    >\n      <span className=\"absolute inset-0 ring-1 ring-foreground/10 ring-inset\" style={{ backgroundColor: color }} />\n    </span>\n  )\n}\n\ninterface VariantProps {\n  state: ColorState\n  alpha: boolean\n  spring: boolean\n  disabled: boolean\n  swatches: readonly ColorSwatch[]\n  label: string\n}\n\nfunction PopoverVariant({ state, alpha, spring, disabled, swatches, label }: VariantProps) {\n  return (\n    <Popover>\n      <PopoverTrigger\n        disabled={disabled}\n        aria-label={`${label}: ${state.hex}`}\n        className={cn(\n          \"inline-flex h-9 items-center gap-2 rounded-md border border-input px-2 shadow-xs outline-none pointer-coarse:h-10\",\n          \"transition-colors duration-(--duration-fast) ease-(--ease-mise) hover:bg-accent\",\n          \"focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50\",\n        )}\n      >\n        <ColorSample color={hsvaToCss(state.color)} />\n        <span className=\"font-mono text-[13px]\">{state.hex}</span>\n      </PopoverTrigger>\n      <PopoverContent align=\"start\" className={PANEL_POPOVER_CLASS}>\n        <ColorPanel state={state} alpha={alpha} spring={spring} disabled={disabled} swatches={swatches} showHex />\n      </PopoverContent>\n    </Popover>\n  )\n}\n\nfunction CompactVariant({ state, alpha, spring, disabled, swatches, label }: VariantProps) {\n  const pickHex = (text: string) => {\n    const parsed = hexToHsva(text)\n    if (parsed) state.update(parsed)\n  }\n  return (\n    <div className=\"flex w-full max-w-44 min-w-0 items-center gap-2\">\n      <Popover>\n        <PopoverTrigger\n          disabled={disabled}\n          aria-label={`${label}: ${state.hex}`}\n          className={cn(\n            \"relative flex size-8 shrink-0 items-center justify-center rounded-md outline-none hover:bg-accent pointer-coarse:size-10\",\n            \"focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50\",\n          )}\n        >\n          <ColorSample color={hsvaToCss(state.color)} />\n        </PopoverTrigger>\n        <PopoverContent align=\"start\" className={PANEL_POPOVER_CLASS}>\n          <ColorPanel state={state} alpha={alpha} spring={spring} disabled={disabled} swatches={swatches} showHex={false} />\n        </PopoverContent>\n      </Popover>\n      <HexField hex={state.hex} disabled={disabled} onCommit={pickHex} />\n    </div>\n  )\n}\n\nfunction ColorPicker({\n  value,\n  defaultValue,\n  onValueChange,\n  variant = \"popover\",\n  effect,\n  alpha = true,\n  swatches = DEFAULT_SWATCHES,\n  disabled = false,\n  name,\n  label = \"Color\",\n  className,\n  ...props\n}: ColorPickerProps) {\n  const state = useColorState({ value, defaultValue, onValueChange, alpha })\n  const spring = effect === \"spring\"\n  const shared = { state, alpha, spring, disabled, swatches, label }\n  return (\n    <div data-slot=\"color-picker\" data-variant={variant} data-effect={effect} className={cn(\"min-w-0\", className)} {...props}>\n      {name ? <input type=\"hidden\" name={name} value={state.hex} disabled={disabled} /> : null}\n      {variant === \"popover\" ? <PopoverVariant {...shared} /> : null}\n      {variant === \"compact\" ? <CompactVariant {...shared} /> : null}\n      {variant === \"inline\" ? (\n        <div className=\"w-full max-w-64 rounded-lg border bg-card p-4\">\n          <ColorPanel state={state} alpha={alpha} spring={spring} disabled={disabled} swatches={swatches} showHex />\n        </div>\n      ) : null}\n      {variant === \"swatches\" ? (\n        <SwatchGrid\n          swatches={swatches}\n          hex={state.hex}\n          disabled={disabled}\n          spring={spring}\n          label={label}\n          onPick={(text) => {\n            const parsed = hexToHsva(text)\n            if (parsed) state.update(parsed)\n          }}\n        />\n      ) : null}\n    </div>\n  )\n}\n\nexport { ColorPicker, type ColorPickerProps, type ColorPickerVariant, type ColorPickerEffect, type ColorSwatch }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/color-picker-panel.tsx",
      "content": "import type * as React from \"react\"\nimport { useState } from \"react\"\nimport { hexToHsva, hsvaToCss, normalizeHex } from \"@/registry/miseui/ui/color-picker-math\"\nimport { ColorArea, ColorTrack } from \"@/registry/miseui/ui/color-picker-drag\"\nimport { CHECKER_BACKGROUND, SwatchGrid, type ColorSwatch } from \"@/registry/miseui/ui/color-picker-swatches\"\nimport type { ColorState } from \"@/registry/miseui/ui/color-picker-state\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\nconst HUE_BACKGROUND = \"linear-gradient(to right, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%)\"\nconst HUE_MAX = 360\nconst ALPHA_MAX = 100\n\ntype HexFieldProps = Omit<React.ComponentProps<\"input\">, \"value\" | \"onChange\"> & {\n  hex: string\n  onCommit: (hex: string) => void\n}\n\n/** Holds half-typed text as a draft: #25 is not a colour yet, and the picker must not jump while someone is on the way to #2563eb. */\nfunction HexField({ hex, onCommit, className, ...props }: HexFieldProps) {\n  const [draft, setDraft] = useState<string | null>(null)\n  const invalid = draft !== null && normalizeHex(draft) === null\n  const commit = () => {\n    if (draft === null) return\n    const normalized = normalizeHex(draft)\n    if (normalized) onCommit(normalized)\n    setDraft(null)\n  }\n  return (\n    <input\n      type=\"text\"\n      spellCheck={false}\n      autoComplete=\"off\"\n      aria-label=\"Hex color\"\n      aria-invalid={invalid || undefined}\n      data-slot=\"color-picker-hex\"\n      value={draft ?? hex}\n      className={cn(\n        \"h-8 w-full min-w-0 rounded-md border border-input bg-transparent px-2 font-mono text-[13px] outline-none pointer-coarse:h-10\",\n        \"transition-[border-color,box-shadow] duration-(--duration-fast) ease-(--ease-mise)\",\n        \"focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50\",\n        \"aria-invalid:border-destructive disabled:cursor-not-allowed disabled:opacity-50\",\n        className,\n      )}\n      onChange={(event) => setDraft(event.target.value)}\n      onBlur={commit}\n      onKeyDown={(event) => {\n        if (event.key === \"Enter\") {\n          event.preventDefault()\n          commit()\n        }\n        if (event.key === \"Escape\") setDraft(null)\n      }}\n      {...props}\n    />\n  )\n}\n\ninterface ColorPanelProps {\n  state: ColorState\n  alpha: boolean\n  spring: boolean\n  disabled: boolean\n  swatches: readonly ColorSwatch[]\n  /** The compact variant already has the hex field outside the panel. */\n  showHex: boolean\n}\n\nfunction ColorPanel({ state, alpha, spring, disabled, swatches, showHex }: ColorPanelProps) {\n  const { color, hex, update } = state\n  const opaque = hsvaToCss({ ...color, a: 1 })\n  const shared = { disabled, spring }\n  const pickHex = (text: string) => {\n    const parsed = hexToHsva(text)\n    if (parsed) update(parsed)\n  }\n  return (\n    <div data-slot=\"color-picker-panel\" className=\"grid gap-4 pointer-coarse:gap-6\">\n      <ColorArea hue={color.h} saturation={color.s} brightness={color.v} thumbColor={opaque} onChange={(s, v) => update({ s, v })} {...shared} />\n      <ColorTrack\n        slot=\"color-picker-hue\"\n        label=\"Hue\"\n        value={color.h}\n        max={HUE_MAX}\n        valueText={`${Math.round(color.h)} degrees`}\n        background={HUE_BACKGROUND}\n        thumbColor={`hsl(${color.h} 100% 50%)`}\n        onChange={(h) => update({ h })}\n        {...shared}\n      />\n      {alpha ? (\n        <ColorTrack\n          slot=\"color-picker-alpha\"\n          label=\"Opacity\"\n          value={color.a * ALPHA_MAX}\n          max={ALPHA_MAX}\n          valueText={`${Math.round(color.a * ALPHA_MAX)}%`}\n          background={`linear-gradient(to right, transparent, ${opaque}), ${CHECKER_BACKGROUND}`}\n          thumbColor={opaque}\n          onChange={(percent) => update({ a: percent / ALPHA_MAX })}\n          {...shared}\n        />\n      ) : null}\n      {showHex ? <HexField hex={hex} disabled={disabled} onCommit={pickHex} /> : null}\n      {swatches.length > 0 ? <SwatchGrid swatches={swatches} hex={hex} disabled={disabled} spring={spring} onPick={pickHex} /> : null}\n    </div>\n  )\n}\n\nexport { ColorPanel, HexField }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/color-picker-drag.tsx",
      "content": "import type * as React from \"react\"\nimport { useLayoutEffect, useRef } from \"react\"\nimport { animate, motion, useMotionValue, useReducedMotion, useTransform } from \"motion/react\"\nimport { SPRING_DEFAULT, clampUnit } from \"@/registry/miseui/lib/motion\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\nconst THUMB_CLASS =\n  \"pointer-events-none absolute size-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-white shadow-[0_0_0_1px_rgb(0_0_0/0.35)]\"\n\ninterface DragOptions {\n  disabled: boolean\n  onMove: (x: number, y: number) => void\n}\n\n/**\n * Pointer capture keeps the drag alive outside the element, and the thumb goes wherever the pointer is.\n * `jump` is true only between press and first move: it is the one moment a spring is allowed to run.\n */\nfunction usePointerDrag({ disabled, onMove }: DragOptions) {\n  const jumpRef = useRef(false)\n  const report = (event: React.PointerEvent<HTMLElement>) => {\n    const rect = event.currentTarget.getBoundingClientRect()\n    onMove(clampUnit((event.clientX - rect.left) / rect.width), clampUnit((event.clientY - rect.top) / rect.height))\n  }\n  const handlers = {\n    onPointerDown: (event: React.PointerEvent<HTMLElement>) => {\n      if (disabled || event.button !== 0) return\n      event.currentTarget.setPointerCapture(event.pointerId)\n      // A touch does not focus on its own, and the arrow keys should pick up where the hand left off.\n      event.currentTarget.focus({ preventScroll: true })\n      jumpRef.current = true\n      report(event)\n    },\n    onPointerMove: (event: React.PointerEvent<HTMLElement>) => {\n      if (!event.currentTarget.hasPointerCapture(event.pointerId)) return\n      jumpRef.current = false\n      report(event)\n    },\n    onPointerUp: () => {\n      jumpRef.current = false\n    },\n    onPointerCancel: () => {\n      jumpRef.current = false\n    },\n  }\n  return { jumpRef, handlers }\n}\n\n/** Follows `percent` exactly, unless this change came from a press that jumped and the spring effect is on. */\nfunction useThumbAxis(percent: number, jumpRef: React.RefObject<boolean>, spring: boolean) {\n  const reduceMotion = useReducedMotion() ?? false\n  const position = useMotionValue(percent)\n  useLayoutEffect(() => {\n    if (spring && !reduceMotion && jumpRef.current) {\n      const controls = animate(position, percent, SPRING_DEFAULT)\n      // The first move of a drag lands here and stops the spring, which hands the thumb back to the pointer.\n      return () => controls.stop()\n    }\n    position.set(percent)\n  }, [percent, position, spring, reduceMotion, jumpRef])\n  return useTransform(position, (value) => `${value}%`)\n}\n\ninterface ColorAreaProps {\n  hue: number\n  saturation: number\n  brightness: number\n  thumbColor: string\n  disabled: boolean\n  spring: boolean\n  label?: string\n  onChange: (saturation: number, brightness: number) => void\n}\n\nconst KEY_PERCENT = 0.01\nconst SHIFT_PERCENT = 0.1\n\nfunction ColorArea({ hue, saturation, brightness, thumbColor, disabled, spring, label = \"Saturation and brightness\", onChange }: ColorAreaProps) {\n  const { jumpRef, handlers } = usePointerDrag({ disabled, onMove: (x, y) => onChange(x, 1 - y) })\n  const left = useThumbAxis(saturation * 100, jumpRef, spring)\n  const top = useThumbAxis((1 - brightness) * 100, jumpRef, spring)\n  const onKeyDown = (event: React.KeyboardEvent) => {\n    if (disabled) return\n    const unit = event.shiftKey ? SHIFT_PERCENT : KEY_PERCENT\n    const moves: Record<string, [number, number] | undefined> = {\n      ArrowLeft: [saturation - unit, brightness],\n      ArrowRight: [saturation + unit, brightness],\n      ArrowUp: [saturation, brightness + unit],\n      ArrowDown: [saturation, brightness - unit],\n    }\n    const move = moves[event.key]\n    if (!move) return\n    event.preventDefault()\n    jumpRef.current = false\n    onChange(clampUnit(move[0]), clampUnit(move[1]))\n  }\n  const saturationPercent = Math.round(saturation * 100)\n  const brightnessPercent = Math.round(brightness * 100)\n  return (\n    <div\n      role=\"slider\"\n      tabIndex={disabled ? -1 : 0}\n      aria-label={label}\n      aria-disabled={disabled || undefined}\n      aria-valuemin={0}\n      aria-valuemax={100}\n      aria-valuenow={saturationPercent}\n      aria-valuetext={`Saturation ${saturationPercent}%, brightness ${brightnessPercent}%`}\n      data-slot=\"color-picker-area\"\n      className=\"relative h-40 w-full cursor-crosshair touch-none rounded-md outline-none select-none focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-disabled:pointer-events-none\"\n      style={{ backgroundImage: `linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, hsl(${hue} 100% 50%))` }}\n      onKeyDown={onKeyDown}\n      {...handlers}\n    >\n      <span aria-hidden=\"true\" className=\"absolute inset-0 rounded-[inherit] ring-1 ring-foreground/10 ring-inset\" />\n      <motion.span aria-hidden=\"true\" className={THUMB_CLASS} style={{ left, top, backgroundColor: thumbColor }} />\n    </div>\n  )\n}\n\ninterface ColorTrackProps {\n  label: string\n  value: number\n  min?: number\n  max: number\n  valueText: string\n  background: string\n  thumbColor: string\n  disabled: boolean\n  spring: boolean\n  slot: string\n  onChange: (value: number) => void\n}\n\nconst TRACK_KEY_STEP = 1\nconst TRACK_LARGE_STEP = 10\n\nfunction ColorTrack({ label, value, min = 0, max, valueText, background, thumbColor, disabled, spring, slot, onChange }: ColorTrackProps) {\n  const range = max - min\n  const set = (next: number) => onChange(Math.min(max, Math.max(min, next)))\n  const { jumpRef, handlers } = usePointerDrag({ disabled, onMove: (x) => set(min + x * range) })\n  const left = useThumbAxis(((value - min) / range) * 100, jumpRef, spring)\n  const onKeyDown = (event: React.KeyboardEvent) => {\n    if (disabled) return\n    const unit = event.shiftKey ? TRACK_LARGE_STEP : TRACK_KEY_STEP\n    const moves: Record<string, number | undefined> = {\n      ArrowLeft: value - unit,\n      ArrowDown: value - unit,\n      ArrowRight: value + unit,\n      ArrowUp: value + unit,\n      PageDown: value - TRACK_LARGE_STEP,\n      PageUp: value + TRACK_LARGE_STEP,\n      Home: min,\n      End: max,\n    }\n    const move = moves[event.key]\n    if (move === undefined) return\n    event.preventDefault()\n    jumpRef.current = false\n    set(move)\n  }\n  return (\n    <div\n      role=\"slider\"\n      tabIndex={disabled ? -1 : 0}\n      aria-label={label}\n      aria-disabled={disabled || undefined}\n      aria-valuemin={min}\n      aria-valuemax={max}\n      aria-valuenow={Math.round(value)}\n      aria-valuetext={valueText}\n      data-slot={slot}\n      className={cn(\n        \"relative h-3 w-full cursor-ew-resize touch-none rounded-full outline-none select-none pointer-coarse:h-4\",\n        \"focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-disabled:pointer-events-none\",\n        // Taller to the pointer than to the eye: 28 px with a mouse, 40 px under a thumb.\n        \"before:absolute before:inset-x-0 before:-inset-y-2 pointer-coarse:before:-inset-y-3\",\n      )}\n      style={{ background }}\n      onKeyDown={onKeyDown}\n      {...handlers}\n    >\n      <span aria-hidden=\"true\" className=\"absolute inset-0 rounded-[inherit] ring-1 ring-foreground/10 ring-inset\" />\n      <motion.span aria-hidden=\"true\" className={cn(THUMB_CLASS, \"top-1/2\")} style={{ left, backgroundColor: thumbColor }} />\n    </div>\n  )\n}\n\nexport { ColorArea, ColorTrack, usePointerDrag }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/color-picker-swatches.tsx",
      "content": "import type * as React from \"react\"\nimport { useId } from \"react\"\nimport { LayoutGroup, motion, useReducedMotion } from \"motion/react\"\nimport { SPRING_DEFAULT } from \"@/registry/miseui/lib/motion\"\nimport { normalizeHex } from \"@/registry/miseui/ui/color-picker-math\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\ntype ColorSwatch = string | { value: string; label: string }\n\n/** Checks drawn with theme tokens, so a see-through colour reads as see-through in light and dark. */\nconst CHECKER_BACKGROUND = \"repeating-conic-gradient(var(--input) 0% 25%, var(--background) 0% 50%) 0 0 / 8px 8px\"\n\nconst SWATCH_KEYS: Record<string, number | undefined> = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }\n\nfunction swatchEntry(swatch: ColorSwatch) {\n  const value = typeof swatch === \"string\" ? swatch : swatch.value\n  return { value, hex: normalizeHex(value) ?? value, label: typeof swatch === \"string\" ? value : swatch.label }\n}\n\ninterface SwatchProps {\n  entry: ReturnType<typeof swatchEntry>\n  selected: boolean\n  tabStop: boolean\n  disabled: boolean\n  slide: boolean\n  onPick: () => void\n  onKeyDown: (event: React.KeyboardEvent<HTMLButtonElement>) => void\n}\n\nfunction Swatch({ entry, selected, tabStop, disabled, slide, onPick, onKeyDown }: SwatchProps) {\n  return (\n    <button\n      type=\"button\"\n      role=\"radio\"\n      aria-checked={selected}\n      aria-label={entry.label}\n      disabled={disabled}\n      tabIndex={tabStop ? 0 : -1}\n      data-slot=\"color-picker-swatch\"\n      className={cn(\n        \"relative size-8 shrink-0 rounded-full outline-none pointer-coarse:size-10\",\n        \"focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50\",\n      )}\n      onClick={onPick}\n      onKeyDown={onKeyDown}\n    >\n      <span aria-hidden=\"true\" className=\"absolute inset-1 rounded-full ring-1 ring-foreground/10 ring-inset dark:ring-foreground/30\" style={{ backgroundColor: entry.hex }} />\n      {selected ? (\n        <motion.span\n          aria-hidden=\"true\"\n          data-slot=\"color-picker-swatch-ring\"\n          layoutId={slide ? \"swatch-ring\" : undefined}\n          transition={SPRING_DEFAULT}\n          className=\"absolute inset-0 rounded-full border-2 border-foreground\"\n        />\n      ) : null}\n    </button>\n  )\n}\n\ninterface SwatchGridProps {\n  swatches: readonly ColorSwatch[]\n  hex: string\n  disabled: boolean\n  spring: boolean\n  label?: string\n  className?: string\n  onPick: (value: string) => void\n}\n\n/** Radio semantics, since one colour is chosen at a time: one tab stop, arrow keys move and select together. */\nfunction SwatchGrid({ swatches, hex, disabled, spring, label = \"Color presets\", className, onPick }: SwatchGridProps) {\n  const groupId = useId()\n  const reduceMotion = useReducedMotion() ?? false\n  const entries = swatches.map(swatchEntry)\n  const selectedIndex = entries.findIndex((entry) => entry.hex === hex)\n  const onKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>, index: number) => {\n    const offset = SWATCH_KEYS[event.key]\n    if (offset === undefined) return\n    event.preventDefault()\n    const next = (index + offset + entries.length) % entries.length\n    onPick(entries[next].value)\n    event.currentTarget.parentElement?.querySelectorAll<HTMLElement>(\"[role='radio']\")[next]?.focus()\n  }\n  return (\n    <LayoutGroup id={groupId}>\n      <div role=\"radiogroup\" aria-label={label} aria-disabled={disabled || undefined} data-slot=\"color-picker-swatches\" className={cn(\"flex flex-wrap gap-2\", className)}>\n        {entries.map((entry, index) => (\n          <Swatch\n            key={entry.value}\n            entry={entry}\n            selected={index === selectedIndex}\n            // With nothing selected the first swatch keeps the group reachable by Tab.\n            tabStop={index === selectedIndex || (selectedIndex === -1 && index === 0)}\n            disabled={disabled}\n            slide={spring && !reduceMotion}\n            onPick={() => onPick(entry.value)}\n            onKeyDown={(event) => onKeyDown(event, index)}\n          />\n        ))}\n      </div>\n    </LayoutGroup>\n  )\n}\n\nexport { CHECKER_BACKGROUND, SwatchGrid, type ColorSwatch }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/color-picker-state.ts",
      "content": "import { useMemo, useState } from \"react\"\nimport { hexToHsva, hsvaToHex, type Hsva } from \"@/registry/miseui/ui/color-picker-math\"\n\nconst FALLBACK_HEX = \"#2563eb\"\nconst FALLBACK_COLOR: Hsva = { h: 0, s: 0, v: 0, a: 1 }\n\ninterface ColorStateOptions {\n  value?: string\n  defaultValue?: string\n  onValueChange?: (value: string) => void\n  alpha: boolean\n}\n\n/**\n * The state is HSV and not hex because black has no hue: dragging the square into a corner and back\n * must not throw the hue bar to red. The owner's hex only wins when it names a different colour.\n */\nfunction useColorState({ value, defaultValue = FALLBACK_HEX, onValueChange, alpha }: ColorStateOptions) {\n  const [internal, setInternal] = useState<Hsva>(() => hexToHsva(value ?? defaultValue) ?? FALLBACK_COLOR)\n  const color = useMemo(() => {\n    const parsed = value === undefined ? null : hexToHsva(value)\n    if (!parsed || hsvaToHex(parsed) === hsvaToHex(internal)) return internal\n    return parsed\n  }, [value, internal])\n  const hex = hsvaToHex(color)\n\n  const update = (patch: Partial<Hsva>) => {\n    const next = { ...color, ...patch, a: alpha ? (patch.a ?? color.a) : 1 }\n    setInternal(next)\n    const nextHex = hsvaToHex(next)\n    if (nextHex !== hex) onValueChange?.(nextHex)\n  }\n\n  return { color, hex, update }\n}\n\ntype ColorState = ReturnType<typeof useColorState>\n\nexport { useColorState, type ColorState }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/color-picker-math.ts",
      "content": "/** Hue 0 to 360; saturation, value and alpha 0 to 1. HSV, because it is what the square and the hue bar draw directly. */\nexport interface Hsva {\n  h: number\n  s: number\n  v: number\n  a: number\n}\n\nexport interface Rgba {\n  r: number\n  g: number\n  b: number\n  a: number\n}\n\nconst HEX_PATTERN = /^#?([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i\nconst CHANNEL_MAX = 255\n\nfunction clampUnitValue(value: number) {\n  return Math.min(1, Math.max(0, value))\n}\n\nfunction hsvToRgb({ h, s, v, a }: Hsva): Rgba {\n  const hue = (((h % 360) + 360) % 360) / 60\n  const channel = (offset: number) => {\n    const k = (offset + hue) % 6\n    return Math.round((v - v * s * Math.max(0, Math.min(k, 4 - k, 1))) * CHANNEL_MAX)\n  }\n  return { r: channel(5), g: channel(3), b: channel(1), a }\n}\n\nfunction rgbToHsv({ r, g, b, a }: Rgba): Hsva {\n  const [red, green, blue] = [r / CHANNEL_MAX, g / CHANNEL_MAX, b / CHANNEL_MAX]\n  const high = Math.max(red, green, blue)\n  const chroma = high - Math.min(red, green, blue)\n  const sextant = () => {\n    if (high === red) return ((green - blue) / chroma + 6) % 6\n    if (high === green) return (blue - red) / chroma + 2\n    return (red - green) / chroma + 4\n  }\n  return { h: chroma === 0 ? 0 : sextant() * 60, s: high === 0 ? 0 : chroma / high, v: high, a }\n}\n\nfunction parseHex(text: string): Rgba | null {\n  const match = HEX_PATTERN.exec(text.trim())\n  if (!match) return null\n  const body = match[1].length <= 4 ? Array.from(match[1], (digit) => digit + digit).join(\"\") : match[1]\n  const channel = (start: number) => Number.parseInt(body.slice(start, start + 2), 16)\n  return { r: channel(0), g: channel(2), b: channel(4), a: body.length === 8 ? channel(6) / CHANNEL_MAX : 1 }\n}\n\n/** Opaque colours stay six digits: nobody wants #2563ebff in their stylesheet. */\nfunction rgbaToHex({ r, g, b, a }: Rgba) {\n  const pair = (channel: number) => Math.round(channel).toString(16).padStart(2, \"0\")\n  const alpha = a >= 1 ? \"\" : pair(clampUnitValue(a) * CHANNEL_MAX)\n  return `#${pair(r)}${pair(g)}${pair(b)}${alpha}`\n}\n\nfunction hexToHsva(text: string): Hsva | null {\n  const rgba = parseHex(text)\n  return rgba ? rgbToHsv(rgba) : null\n}\n\nfunction hsvaToHex(color: Hsva) {\n  return rgbaToHex(hsvToRgb(color))\n}\n\n/** Lower case and full length, so two spellings of one colour compare equal. Null when it is not a colour. */\nfunction normalizeHex(text: string) {\n  const rgba = parseHex(text)\n  return rgba ? rgbaToHex(rgba) : null\n}\n\nfunction hsvaToCss(color: Hsva) {\n  const { r, g, b, a } = hsvToRgb(color)\n  return `rgb(${r} ${g} ${b} / ${a})`\n}\n\nexport { clampUnitValue, hexToHsva, hsvaToCss, hsvaToHex, hsvToRgb, normalizeHex, parseHex, rgbaToHex, rgbToHsv }\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "forms"
  ],
  "type": "registry:ui"
}