{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "swatch-picker",
  "type": "registry:item",
  "title": "Swatch Picker",
  "description": "Colour swatches with a ring that glides to your pick, a small squeeze and a check in readable ink.",
  "categories": [
    "input"
  ],
  "dependencies": [
    "react-native-reanimated",
    "react-native-worklets"
  ],
  "registryDependencies": [
    "https://kinetik-ui.dev/r/kinetik-tokens.json",
    "https://kinetik-ui.dev/r/kinetik-motion.json",
    "https://kinetik-ui.dev/r/kinetik-haptics.json"
  ],
  "files": [
    {
      "path": "src/components/kinetik/swatch-picker.tsx",
      "type": "registry:file",
      "target": "~/components/kinetik/swatch-picker.tsx",
      "content": "import { useEffect, useRef, useState } from 'react';\nimport { Pressable, StyleSheet, View, type StyleProp, type ViewStyle } from 'react-native';\nimport Animated, {\n  useAnimatedStyle,\n  useSharedValue,\n  withSequence,\n  withSpring,\n  withTiming,\n} from 'react-native-reanimated';\n\nimport { useHaptic } from '../../lib/kinetik/hooks/use-haptic';\nimport { useReduceMotion } from '../../lib/kinetik/hooks/use-reduce-motion';\nimport { spring } from '../../lib/kinetik/motion/motion';\nimport { useKinetikTheme } from '../../lib/kinetik/tokens/theme';\nimport { minTouchTarget, palettes } from '../../lib/kinetik/tokens/tokens';\n\nexport type Swatch = {\n  /** Any colour React Native accepts; `#RRGGBB` also picks a readable check colour. */\n  value: string;\n  /** Read by screen readers, e.g. \"Sage\". */\n  label: string;\n};\n\nexport type SwatchPickerProps = {\n  swatches: readonly Swatch[];\n  value: string;\n  onValueChange: (value: string) => void;\n  /** Names the group, e.g. \"Accent colour\". */\n  accessibilityLabel: string;\n  /** Diameter of each swatch. Defaults to 32. */\n  size?: number;\n  /** A tick when the colour changes. Defaults to true. */\n  haptics?: boolean;\n  testID?: string;\n  style?: StyleProp<ViewStyle>;\n};\n\nconst CELL = minTouchTarget;\nconst GAP = 3;\nconst RING = 2;\n\n/** True when dark ink reads better than light ink on the colour. */\nexport function isLight(color: string): boolean {\n  const m = /^#([0-9a-f]{6})$/i.exec(color);\n  if (!m?.[1]) return false;\n  const n = parseInt(m[1], 16);\n  const lin = (c: number) => {\n    const s = c / 255;\n    return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;\n  };\n  const l = 0.2126 * lin((n >> 16) & 255) + 0.7152 * lin((n >> 8) & 255) + 0.0722 * lin(n & 255);\n  return l > 0.4;\n}\n\n/**\n * A row of colour swatches. A ring in the chosen colour glides from swatch\n * to swatch, the swatch under it gives a small squeeze, and a check pops in\n * whose ink is picked to stay readable on that colour.\n */\nexport function SwatchPicker({\n  swatches,\n  value,\n  onValueChange,\n  accessibilityLabel,\n  size = 32,\n  haptics = true,\n  testID,\n  style,\n}: SwatchPickerProps) {\n  const reduced = useReduceMotion();\n  const haptic = useHaptic(haptics);\n  const [spots, setSpots] = useState<Record<string, { x: number; y: number }>>({});\n  const x = useSharedValue(0);\n  const y = useSharedValue(0);\n  const placed = useRef(false);\n  const spot = spots[value];\n  const ring = size + (GAP + RING) * 2;\n\n  useEffect(() => {\n    if (!spot) return;\n    const snap = reduced || !placed.current;\n    placed.current = true;\n    x.set(snap ? spot.x : withSpring(spot.x, spring('smooth')));\n    y.set(snap ? spot.y : withSpring(spot.y, spring('smooth')));\n  }, [spot, reduced, x, y]);\n\n  const ringStyle = useAnimatedStyle(() => ({\n    opacity: spot ? 1 : 0,\n    transform: [{ translateX: x.get() }, { translateY: y.get() }],\n  }));\n\n  return (\n    <View\n      testID={testID}\n      accessibilityRole=\"radiogroup\"\n      accessibilityLabel={accessibilityLabel}\n      style={[styles.wrap, style]}\n    >\n      <Animated.View\n        pointerEvents=\"none\"\n        style={[\n          styles.ring,\n          {\n            width: ring,\n            height: ring,\n            borderRadius: ring / 2,\n            borderColor: value,\n            left: (CELL - ring) / 2,\n            top: (CELL - ring) / 2,\n          },\n          ringStyle,\n        ]}\n      />\n      {swatches.map((s) => (\n        <Dot\n          key={s.value}\n          swatch={s}\n          size={size}\n          selected={s.value === value}\n          reduced={reduced}\n          onPress={() => {\n            if (s.value === value) return;\n            haptic('tick');\n            onValueChange(s.value);\n          }}\n          onPlace={(px, py) =>\n            setSpots((m) => {\n              const prev = m[s.value];\n              return prev && prev.x === px && prev.y === py\n                ? m\n                : { ...m, [s.value]: { x: px, y: py } };\n            })\n          }\n        />\n      ))}\n    </View>\n  );\n}\n\ntype DotProps = {\n  swatch: Swatch;\n  size: number;\n  selected: boolean;\n  reduced: boolean;\n  onPress: () => void;\n  onPlace: (x: number, y: number) => void;\n};\n\nfunction Dot({ swatch, size, selected, reduced, onPress, onPlace }: DotProps) {\n  const { colors } = useKinetikTheme();\n  const on = useSharedValue(selected ? 1 : 0);\n  const squeeze = useSharedValue(1);\n  useEffect(() => {\n    on.set(withSpring(selected ? 1 : 0, spring('bouncy', reduced)));\n    if (selected && !reduced) {\n      squeeze.set(\n        withSequence(withTiming(0.86, { duration: 90 }), withSpring(1, spring('bouncy'))),\n      );\n    }\n  }, [selected, reduced, on, squeeze]);\n\n  const body = useAnimatedStyle(() => ({ transform: [{ scale: squeeze.get() }] }));\n  const check = useAnimatedStyle(() => ({\n    opacity: Math.min(1, on.get() * 1.5),\n    transform: [{ scale: 0.4 + on.get() * 0.6 }, { rotate: '-45deg' }],\n  }));\n  const ink = isLight(swatch.value) ? palettes.light.text : palettes.dark.text;\n\n  return (\n    <Pressable\n      onPress={onPress}\n      onLayout={(e) => onPlace(e.nativeEvent.layout.x, e.nativeEvent.layout.y)}\n      accessibilityRole=\"radio\"\n      accessibilityLabel={swatch.label}\n      accessibilityState={{ checked: selected }}\n      style={styles.cell}\n    >\n      <Animated.View\n        style={[\n          styles.dot,\n          {\n            width: size,\n            height: size,\n            borderRadius: size / 2,\n            backgroundColor: swatch.value,\n            borderColor: colors.border,\n          },\n          body,\n        ]}\n      >\n        <Animated.View\n          style={[\n            styles.check,\n            { borderColor: ink, width: size * 0.42, height: size * 0.24 },\n            check,\n          ]}\n        />\n      </Animated.View>\n    </Pressable>\n  );\n}\n\nconst styles = StyleSheet.create({\n  wrap: { flexDirection: 'row', flexWrap: 'wrap', gap: 4 },\n  ring: { position: 'absolute', borderWidth: RING },\n  cell: { width: CELL, height: CELL, alignItems: 'center', justifyContent: 'center' },\n  dot: { borderWidth: StyleSheet.hairlineWidth, alignItems: 'center', justifyContent: 'center' },\n  check: { borderLeftWidth: 2.2, borderBottomWidth: 2.2, marginTop: -2 },\n});\n"
    }
  ],
  "meta": {
    "version": "1.0.0",
    "kind": "component",
    "platforms": [
      "ios",
      "android",
      "web"
    ],
    "category": "input",
    "web": "live",
    "perf": {
      "budget": "Plain views; one ring on two shared values and two per swatch for the squeeze and check. Nothing runs at rest.",
      "measurements": [
        {
          "device": "Pixel 9 emulator, API 35",
          "kind": "emulator",
          "date": "2026-10-13",
          "build": "release",
          "frameMs": 17,
          "p90Ms": 18,
          "baselineMs": 18,
          "jankyPercent": 0.18,
          "atRest": "idle",
          "memoryMb": 145
        }
      ]
    },
    "a11y": {
      "reducedMotion": "The ring jumps to the new swatch and the check appears without a spring.",
      "screenReader": "A radio group named by accessibilityLabel; each swatch is a radio read by its label.",
      "touchTarget": "Each swatch is the centre of a 44 by 44 pt cell."
    },
    "requires": "React Native New Architecture (Reanimated 4)",
    "guide": "## Usage\n\n```tsx\nimport { SwatchPicker } from '@/components/kinetik/swatch-picker';\n\n<SwatchPicker\n  accessibilityLabel=\"Accent colour\"\n  swatches={[\n    { value: '#2B6BEF', label: 'Cobalt' },\n    { value: '#3BA58B', label: 'Sea glass' },\n    { value: '#F2C14E', label: 'Butter' },\n  ]}\n  value={accent}\n  onValueChange={setAccent}\n/>;\n```\n\n## When to use\n\n- Picking from a fixed set of colours: a label colour, a theme accent, a product finish.\n- Small sets where seeing every option at once matters more than precision.\n\n## When not to use\n\n- Any colour at all. Use a full colour picker with hue and lightness.\n- Choices that are not colours. Use Glide Radio or Choice Cards.\n\n## Notes\n\n- Every swatch needs a `label`: screen readers read the name, never the hex value.\n- With `#RRGGBB` values the check is drawn in dark or light ink, whichever reads better on that colour.\n- Each swatch sits in a 44 pt cell, so `size` can go down to 24 without shrinking the target.\n- With reduced motion the ring jumps and the swatch does not squeeze.\n"
  },
  "docs": "Prefer `npx kinetik-ui add swatch-picker`: it installs dependencies with expo install, so versions match your Expo SDK."
}
