{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "signature-pad",
  "type": "registry:item",
  "title": "Signature Pad",
  "description": "Sign with a finger: the ink is full when slow and thins with speed, with Undo and a Clear that fades it away.",
  "categories": [
    "input"
  ],
  "dependencies": [
    "@shopify/react-native-skia",
    "react-native-gesture-handler",
    "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/signature-pad.tsx",
      "type": "registry:file",
      "target": "~/components/kinetik/signature-pad.tsx",
      "content": "import { Canvas, Group, Line, Path, Skia, vec, type SkPath } from '@shopify/react-native-skia';\nimport { useState } from 'react';\nimport { Pressable, StyleSheet, Text, View, type StyleProp, type ViewStyle } from 'react-native';\nimport { Gesture, GestureDetector } from 'react-native-gesture-handler';\nimport Animated, {\n  useAnimatedStyle,\n  useDerivedValue,\n  useSharedValue,\n  withTiming,\n} from 'react-native-reanimated';\nimport { scheduleOnRN } from 'react-native-worklets';\n\nimport { useHaptic } from '../../lib/kinetik/hooks/use-haptic';\nimport { useReduceMotion } from '../../lib/kinetik/hooks/use-reduce-motion';\nimport { durations, easings, timing } from '../../lib/kinetik/motion/motion';\nimport { useKinetikTheme } from '../../lib/kinetik/tokens/theme';\nimport { minTouchTarget } from '../../lib/kinetik/tokens/tokens';\n\n/** One point of a stroke: position and ink width there. */\nexport type InkPoint = { x: number; y: number; w: number };\nexport type InkStroke = readonly InkPoint[];\n\nexport type SignaturePadProps = {\n  /** Called with every stroke after each change. Empty when cleared. */\n  onChange?: (strokes: readonly InkStroke[]) => void;\n  /** Pad height. Defaults to 200. */\n  height?: number;\n  /** Widest ink, drawn when the pen moves slowly. Defaults to 4.5. */\n  maxWidth?: number;\n  /** Thinnest ink, drawn at speed. Defaults to 1.4. */\n  minWidth?: number;\n  /** Words under the line while the pad is empty. Defaults to \"Sign here\". */\n  hint?: string;\n  /** Read by screen readers. Defaults to \"Signature\". */\n  accessibilityLabel?: string;\n  /** A tick on Undo and Clear. Defaults to true. */\n  haptics?: boolean;\n  testID?: string;\n  style?: StyleProp<ViewStyle>;\n};\n\n/**\n * A filled outline around a stroke whose width varies along it: each point is\n * pushed out to both sides along the normal, the two edges are joined with\n * smoothing curves, and round ends close it.\n */\nexport function inkOutline(points: InkStroke): SkPath {\n  'worklet';\n  const b = Skia.PathBuilder.Make();\n  const n = points.length;\n  if (n === 0) return b.detach();\n  const first = points[0]!;\n  if (n === 1) return b.addCircle(first.x, first.y, first.w / 2).detach();\n  const left: { x: number; y: number }[] = [];\n  const right: { x: number; y: number }[] = [];\n  const normal: number[] = [];\n  for (let i = 0; i < n; i++) {\n    const p = points[i]!;\n    const a = points[Math.max(0, i - 1)]!;\n    const c = points[Math.min(n - 1, i + 1)]!;\n    let dx = c.x - a.x;\n    let dy = c.y - a.y;\n    const len = Math.hypot(dx, dy) || 1;\n    dx /= len;\n    dy /= len;\n    const r = p.w / 2;\n    left.push({ x: p.x - dy * r, y: p.y + dx * r });\n    right.push({ x: p.x + dy * r, y: p.y - dx * r });\n    normal.push(Math.atan2(dx, -dy));\n  }\n  const edge = (pts: { x: number; y: number }[]) => {\n    for (let i = 1; i < pts.length - 1; i++) {\n      const p = pts[i]!;\n      const q = pts[i + 1]!;\n      b.quadTo(p.x, p.y, (p.x + q.x) / 2, (p.y + q.y) / 2);\n    }\n    const end = pts[pts.length - 1]!;\n    b.lineTo(end.x, end.y);\n  };\n  // A round end: half a circle from one edge to the other, around the tip.\n  const cap = (p: InkPoint, from: number) => {\n    for (let k = 1; k < 8; k++) {\n      const t = from - (k * Math.PI) / 8;\n      b.lineTo(p.x + Math.cos(t) * (p.w / 2), p.y + Math.sin(t) * (p.w / 2));\n    }\n  };\n  const last = points[n - 1]!;\n  b.moveTo(left[0]!.x, left[0]!.y);\n  edge(left);\n  cap(last, normal[n - 1]!);\n  const back = right.reverse();\n  b.lineTo(back[0]!.x, back[0]!.y);\n  edge(back);\n  cap(first, normal[0]! + Math.PI);\n  b.close();\n  return b.detach();\n}\n\n/** The strokes as one SVG document, for saving or sending. */\nexport function signatureSvg(\n  strokes: readonly InkStroke[],\n  width: number,\n  height: number,\n  ink: string,\n): string {\n  const paths = strokes\n    .map((s) => `<path d=\"${inkOutline(s).toSVGString()}\" fill=\"${ink}\"/>`)\n    .join('');\n  return `<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 ${width} ${height}\">${paths}</svg>`;\n}\n\n/**\n * A pad to sign with a finger. The ink is full and round when the finger\n * moves slowly and thins as it speeds up, like a fountain pen; Undo takes the\n * last stroke back and Clear fades the whole signature away.\n */\nexport function SignaturePad({\n  onChange,\n  height = 200,\n  maxWidth = 4.5,\n  minWidth = 1.4,\n  hint = 'Sign here',\n  accessibilityLabel = 'Signature',\n  haptics = true,\n  testID,\n  style,\n}: SignaturePadProps) {\n  const { colors, font, radius, space } = useKinetikTheme();\n  const reduced = useReduceMotion();\n  const haptic = useHaptic(haptics);\n  const [strokes, setStrokes] = useState<{ points: InkStroke; path: SkPath }[]>([]);\n  const [width, setWidth] = useState(0);\n  const live = useSharedValue<InkPoint[]>([]);\n  const fade = useSharedValue(1);\n  const empty = strokes.length === 0;\n\n  const update = (next: { points: InkStroke; path: SkPath }[]) => {\n    // A cleared pad starts the next signature fully opaque again.\n    if (next.length === 0) fade.set(1);\n    setStrokes(next);\n    onChange?.(next.map((s) => s.points));\n  };\n  const commit = (points: InkPoint[]) => {\n    if (points.length === 0) return;\n    update([...strokes, { points, path: inkOutline(points) }]);\n  };\n\n  /** Adds a point; `speed` is the finger's speed in points per millisecond. */\n  const add = (x: number, y: number, speed: number, first: boolean) => {\n    'worklet';\n    if (first) {\n      live.set([{ x, y, w: (maxWidth + minWidth) / 2 }]);\n      return;\n    }\n    const pts = live.get();\n    const prev = pts[pts.length - 1];\n    if (!prev || Math.hypot(x - prev.x, y - prev.y) < 1.5) return;\n    // Speed thins the ink; the width eases towards its target so strokes stay smooth.\n    const target = Math.max(minWidth, Math.min(maxWidth, maxWidth - speed * 1.6));\n    const w = prev.w * 0.65 + target * 0.35;\n    live.modify((arr) => {\n      'worklet';\n      arr.push({ x, y, w });\n      return arr;\n    });\n  };\n\n  const pan = Gesture.Pan()\n    .withTestId('signature-pad')\n    .minDistance(0)\n    .maxPointers(1)\n    .onBegin((e) => add(e.x, e.y, 0, true))\n    .onUpdate((e) => add(e.x, e.y, Math.hypot(e.velocityX, e.velocityY) / 1000, false))\n    .onFinalize(() => {\n      const pts = live.get();\n      scheduleOnRN(commit, pts.slice());\n      live.set([]);\n    });\n\n  const current = useDerivedValue(() => inkOutline(live.get()));\n  const opacity = useDerivedValue(() => fade.get());\n\n  const undo = () => {\n    if (empty) return;\n    haptic('tick');\n    update(strokes.slice(0, -1));\n  };\n  const clear = () => {\n    if (empty) return;\n    haptic('tick');\n    if (reduced) {\n      update([]);\n      return;\n    }\n    fade.set(\n      withTiming(0, { duration: durations.base, easing: easings.exit }, (done) => {\n        if (done) scheduleOnRN(update, []);\n      }),\n    );\n  };\n  const hintStyle = useAnimatedStyle(() => ({\n    opacity: withTiming(empty ? 1 : 0, timing('quick')),\n  }));\n  const base = height - 44;\n\n  return (\n    <View testID={testID} style={[{ gap: space[2] }, style]}>\n      <GestureDetector gesture={pan}>\n        <View\n          collapsable={false}\n          accessible\n          accessibilityRole=\"image\"\n          accessibilityLabel={`${accessibilityLabel}, ${empty ? 'empty' : `${strokes.length} strokes`}`}\n          accessibilityHint=\"Draw with one finger\"\n          onLayout={(e) => setWidth(e.nativeEvent.layout.width)}\n          style={[\n            styles.pad,\n            {\n              height,\n              backgroundColor: colors.surface,\n              borderColor: colors.border,\n              borderRadius: radius.lg,\n            },\n          ]}\n        >\n          {width > 0 ? (\n            <Canvas style={StyleSheet.absoluteFill}>\n              <Line\n                p1={vec(24, base)}\n                p2={vec(width - 24, base)}\n                color={colors.borderStrong}\n                strokeWidth={1}\n                style=\"stroke\"\n              />\n              <Line\n                p1={vec(26, base - 18)}\n                p2={vec(36, base - 8)}\n                color={colors.textSubtle}\n                strokeWidth={1.5}\n                style=\"stroke\"\n              />\n              <Line\n                p1={vec(36, base - 18)}\n                p2={vec(26, base - 8)}\n                color={colors.textSubtle}\n                strokeWidth={1.5}\n                style=\"stroke\"\n              />\n              <Group opacity={opacity}>\n                {strokes.map((s, i) => (\n                  <Path key={i} path={s.path} color={colors.text} />\n                ))}\n              </Group>\n              <Path path={current} color={colors.text} />\n            </Canvas>\n          ) : null}\n          <Animated.Text\n            pointerEvents=\"none\"\n            style={[\n              styles.hint,\n              { top: base + 8, color: colors.textSubtle, fontSize: font.size.xs },\n              hintStyle,\n            ]}\n          >\n            {hint}\n          </Animated.Text>\n        </View>\n      </GestureDetector>\n      <View style={styles.actions}>\n        <Action label=\"Undo\" disabled={empty} onPress={undo} />\n        <Action label=\"Clear\" disabled={empty} onPress={clear} />\n      </View>\n    </View>\n  );\n}\n\nfunction Action({\n  label,\n  disabled,\n  onPress,\n}: {\n  label: string;\n  disabled: boolean;\n  onPress: () => void;\n}) {\n  const { colors, font, radius, space } = useKinetikTheme();\n  return (\n    <Pressable\n      onPress={onPress}\n      disabled={disabled}\n      accessibilityRole=\"button\"\n      accessibilityLabel={label}\n      accessibilityState={{ disabled }}\n      style={({ pressed }) => [\n        styles.action,\n        {\n          paddingHorizontal: space[4],\n          borderRadius: radius.pill,\n          backgroundColor: pressed ? colors.surfaceSunken : undefined,\n          opacity: disabled ? 0.4 : 1,\n        },\n      ]}\n    >\n      <Text style={{ color: colors.text, fontSize: font.size.sm, fontWeight: font.weight.medium }}>\n        {label}\n      </Text>\n    </Pressable>\n  );\n}\n\nconst styles = StyleSheet.create({\n  pad: { borderWidth: 1, overflow: 'hidden' },\n  hint: { position: 'absolute', left: 24 },\n  actions: { flexDirection: 'row', justifyContent: 'flex-end', gap: 4 },\n  action: { minHeight: minTouchTarget, justifyContent: 'center' },\n});\n"
    }
  ],
  "meta": {
    "version": "1.0.0",
    "kind": "component",
    "platforms": [
      "ios",
      "android",
      "web"
    ],
    "category": "input",
    "web": "live",
    "perf": {
      "budget": "One Skia canvas. Only the stroke being drawn is rebuilt, on the UI thread, as points arrive; finished strokes are cached paths. Nothing runs at rest.",
      "measurements": [
        {
          "device": "Pixel 9 emulator, API 35",
          "kind": "emulator",
          "date": "2026-10-13",
          "build": "release",
          "frameMs": 16,
          "p90Ms": 17,
          "baselineMs": 18,
          "jankyPercent": 6.29,
          "atRest": "idle",
          "memoryMb": 147
        }
      ]
    },
    "a11y": {
      "reducedMotion": "Clear removes the ink at once instead of fading it.",
      "screenReader": "An image named \"Signature\" that says whether it is empty or how many strokes it has, plus Undo and Clear buttons.",
      "touchTarget": "The pad is the full width and 200 pt tall; Undo and Clear are 44 pt tall."
    },
    "requires": "React Native New Architecture (Reanimated 4)",
    "guide": "## Usage\n\n```tsx\nimport { SignaturePad, signatureSvg } from '@/components/kinetik/signature-pad';\n\n<SignaturePad onChange={setStrokes} />;\n\n// Later, to save or upload:\nconst svg = signatureSvg(strokes, 320, 200, '#111111');\n```\n\n## When to use\n\n- Signing to accept something: a delivery, a lease, a consent form.\n- Quick hand-drawn input such as initials.\n\n## When not to use\n\n- Drawing or note-taking apps. They need pressure, colours, erasing and zoom.\n- As the only way to agree to something: offer a typed name or a checkbox too, for people who cannot draw.\n\n## Notes\n\n- Ink is widest when the finger moves slowly and thins with speed, then each stroke is stored as points with a width (`InkPoint`).\n- `inkOutline` turns a stroke into a filled Skia path and `signatureSvg` turns all strokes into an SVG document.\n- The stroke being drawn is built on the UI thread; finished strokes are kept as paths and never rebuilt.\n- With reduced motion Clear removes the ink at once instead of fading it.\n"
  },
  "docs": "Prefer `npx kinetik-ui add signature-pad`: it installs dependencies with expo install, so versions match your Expo SDK."
}
