{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ring-spinner",
  "type": "registry:item",
  "title": "Ring Spinner",
  "description": "A spinner whose arc stretches as it turns, fills to a known amount, and closes into a check or a cross.",
  "categories": [
    "feedback"
  ],
  "dependencies": [
    "@shopify/react-native-skia",
    "react-native-reanimated",
    "react-native-worklets"
  ],
  "registryDependencies": [
    "https://kinetik-ui.dev/r/kinetik-tokens.json",
    "https://kinetik-ui.dev/r/kinetik-motion.json"
  ],
  "files": [
    {
      "path": "src/components/kinetik/ring-spinner.tsx",
      "type": "registry:file",
      "target": "~/components/kinetik/ring-spinner.tsx",
      "content": "import { Canvas, Group, Path, Skia, vec } from '@shopify/react-native-skia';\nimport { useEffect, useMemo } from 'react';\nimport { AccessibilityInfo, View, type StyleProp, type ViewStyle } from 'react-native';\nimport {\n  cancelAnimation,\n  Easing,\n  useDerivedValue,\n  useSharedValue,\n  withDelay,\n  withRepeat,\n  withSpring,\n  withTiming,\n} from 'react-native-reanimated';\n\nimport { useReduceMotion } from '../../lib/kinetik/hooks/use-reduce-motion';\nimport { durations, easings, spring } from '../../lib/kinetik/motion/motion';\nimport { useKinetikTheme } from '../../lib/kinetik/tokens/theme';\n\nexport type RingSpinnerStatus = 'loading' | 'success' | 'error';\n\nexport type RingSpinnerProps = {\n  /** `success` closes the ring and draws a check; `error` draws a cross. Defaults to `loading`. */\n  status?: RingSpinnerStatus;\n  /** 0 to 1 for a known amount of work; leave unset to spin. */\n  progress?: number;\n  /** Diameter. Defaults to 40. */\n  size?: number;\n  /** Stroke width. Defaults to size / 11. */\n  thickness?: number;\n  /** Arc colour while loading. Defaults to the theme azure. */\n  color?: string;\n  /** Names what is loading. Defaults to 'Loading'. */\n  accessibilityLabel?: string;\n  testID?: string;\n  style?: StyleProp<ViewStyle>;\n};\n\n/** Each cycle the arc's tail catches up this far round the circle. */\nconst ADVANCE = 0.7;\nconst CYCLE_MS = 1500;\n/** Cycles in one long run of the clock; it restarts after about four hours. */\nconst RUN = 10000;\n\nconst ease = (x: number) => {\n  'worklet';\n  return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;\n};\n\n/**\n * A spinner whose arc stretches and shrinks as it turns. Given `progress`, it\n * fills to the amount instead; on `success` it closes into a full ring and\n * draws a check, on `error` a cross. It stops drawing once it has settled.\n */\nexport function RingSpinner({\n  status = 'loading',\n  progress,\n  size = 40,\n  thickness,\n  color,\n  accessibilityLabel = 'Loading',\n  testID,\n  style,\n}: RingSpinnerProps) {\n  const { colors } = useKinetikTheme();\n  const reduced = useReduceMotion();\n  const stroke = thickness ?? Math.max(2, size / 11);\n  const r = size / 2 - stroke / 2;\n  const c = size / 2;\n  const determinate = progress !== undefined;\n  const ink =\n    status === 'success'\n      ? colors.success\n      : status === 'error'\n        ? colors.danger\n        : (color ?? colors.azure);\n\n  const { ring, check, cross } = useMemo(() => {\n    const ringPath = Skia.PathBuilder.Make().addCircle(c, c, r).detach();\n    const tick = Skia.PathBuilder.Make()\n      .moveTo(size * 0.31, size * 0.52)\n      .lineTo(size * 0.44, size * 0.64)\n      .lineTo(size * 0.69, size * 0.38)\n      .detach();\n    const x = Skia.PathBuilder.Make()\n      .moveTo(size * 0.36, size * 0.36)\n      .lineTo(size * 0.64, size * 0.64)\n      .moveTo(size * 0.64, size * 0.36)\n      .lineTo(size * 0.36, size * 0.64)\n      .detach();\n    return { ring: ringPath, check: tick, cross: x };\n  }, [c, r, size]);\n\n  const clock = useSharedValue(0);\n  const settle = useSharedValue(0);\n  const amount = useSharedValue(progress ?? 0);\n  const mark = useSharedValue(0);\n\n  const spinning = status === 'loading' && !determinate && !reduced;\n  useEffect(() => {\n    if (!spinning) {\n      cancelAnimation(clock);\n      return;\n    }\n    clock.set(\n      withRepeat(\n        withTiming(clock.get() + RUN, { duration: CYCLE_MS * RUN, easing: Easing.linear }),\n        -1,\n      ),\n    );\n    return () => cancelAnimation(clock);\n  }, [spinning, clock]);\n\n  useEffect(() => {\n    if (progress !== undefined)\n      amount.set(withSpring(Math.max(0, Math.min(1, progress)), spring('smooth', reduced)));\n  }, [progress, reduced, amount]);\n\n  useEffect(() => {\n    const done = status !== 'loading';\n    settle.set(\n      reduced\n        ? done\n          ? 1\n          : 0\n        : withTiming(done ? 1 : 0, { duration: durations.base, easing: easings.standard }),\n    );\n    mark.set(\n      done\n        ? reduced\n          ? 1\n          : withDelay(\n              durations.quick,\n              withTiming(1, { duration: durations.slow, easing: easings.standard }),\n            )\n        : 0,\n    );\n    if (done) AccessibilityInfo.announceForAccessibility(status === 'success' ? 'Done' : 'Failed');\n  }, [status, reduced, settle, mark]);\n\n  // Where the arc starts, in turns, and how much of the circle it covers.\n  const arc = useDerivedValue(() => {\n    const s = settle.get();\n    if (determinate) {\n      return { from: 0, length: amount.get() + (1 - amount.get()) * s };\n    }\n    if (reduced) return { from: 0, length: 0.25 + 0.75 * s };\n    const k = clock.get();\n    const cycle = Math.floor(k);\n    const t = k - cycle;\n    const head = t < 0.5 ? 0.05 + ADVANCE * ease(t / 0.5) : ADVANCE + 0.05;\n    const tail = t < 0.5 ? 0 : ADVANCE * ease((t - 0.5) / 0.5);\n    const length = head - tail;\n    // The tail's progress carries over from cycle to cycle, and the whole arc drifts round.\n    return { from: cycle * ADVANCE + tail + k * 0.2, length: length + (1 - length) * s };\n  });\n  const transform = useDerivedValue(() => [\n    { rotate: (arc.get().from % 1) * Math.PI * 2 - Math.PI / 2 },\n  ]);\n  const end = useDerivedValue(() => Math.max(0.001, Math.min(1, arc.get().length)));\n\n  return (\n    <View\n      testID={testID}\n      accessible\n      accessibilityRole=\"progressbar\"\n      accessibilityLabel={accessibilityLabel}\n      accessibilityValue={\n        status !== 'loading'\n          ? { text: status === 'success' ? 'Done' : 'Failed' }\n          : determinate\n            ? { min: 0, max: 100, now: Math.round((progress ?? 0) * 100) }\n            : undefined\n      }\n      style={[{ width: size, height: size }, style]}\n    >\n      <Canvas style={{ width: size, height: size }}>\n        <Path path={ring} style=\"stroke\" strokeWidth={stroke} color={colors.track} />\n        <Group origin={vec(c, c)} transform={transform}>\n          <Path\n            path={ring}\n            style=\"stroke\"\n            strokeWidth={stroke}\n            strokeCap=\"round\"\n            color={ink}\n            end={end}\n          />\n        </Group>\n        {status === 'success' ? (\n          <Path\n            path={check}\n            style=\"stroke\"\n            strokeWidth={stroke}\n            strokeCap=\"round\"\n            strokeJoin=\"round\"\n            color={ink}\n            end={mark}\n          />\n        ) : null}\n        {status === 'error' ? (\n          <Path\n            path={cross}\n            style=\"stroke\"\n            strokeWidth={stroke}\n            strokeCap=\"round\"\n            color={ink}\n            end={mark}\n          />\n        ) : null}\n      </Canvas>\n    </View>\n  );\n}\n"
    }
  ],
  "meta": {
    "version": "1.0.0",
    "kind": "component",
    "platforms": [
      "ios",
      "android",
      "web"
    ],
    "category": "feedback",
    "web": "live",
    "perf": {
      "budget": "One small Skia canvas driven by one clock on the UI thread while it spins. The clock stops once it shows progress, success or an error.",
      "measurements": [
        {
          "device": "Pixel 9 emulator, API 35",
          "kind": "emulator",
          "date": "2026-10-13",
          "build": "release",
          "frameMs": 21,
          "p90Ms": 25,
          "baselineMs": 20,
          "jankyPercent": 0.55,
          "atRest": "animating",
          "memoryMb": 153,
          "notes": "The demo upload ring keeps counting, so it draws at rest; a settled spinner stops its clock."
        }
      ]
    },
    "a11y": {
      "reducedMotion": "A still quarter arc while loading; progress, the check and the cross appear without drawing in.",
      "screenReader": "A progress indicator named by accessibilityLabel. It reports a percentage when given progress, reads Done or Failed when it settles, and announces the result.",
      "touchTarget": "Not interactive."
    },
    "requires": "React Native New Architecture (Reanimated 4)",
    "guide": "## Usage\n\n```tsx\nimport { RingSpinner } from '@/components/kinetik/ring-spinner';\n\n<RingSpinner accessibilityLabel=\"Syncing\" status={done ? 'success' : 'loading'} />\n\n// A known amount of work\n<RingSpinner accessibilityLabel=\"Upload\" progress={sent / total} />\n```\n\n## When to use\n\n- Waits inside a card, a row or a button, where a spinner should also show how it ended.\n- Uploads and downloads with a known size, with `progress`.\n\n## When not to use\n\n- Loading whole screens of content. Use Skeleton Sweep.\n- Very short waits, under about half a second. Nothing at all feels faster.\n\n## Notes\n\n- The arc grows and shrinks as it turns, and its tail carries over from one cycle to the next, so the motion never jumps.\n- Set `status` to `success` or `error` and the ring closes, changes colour and draws a check or a cross. The clock stops then, so a settled spinner costs nothing.\n- With reduced motion it shows a still quarter arc while loading, and results appear without drawing in.\n"
  },
  "docs": "Prefer `npx kinetik-ui add ring-spinner`: it installs dependencies with expo install, so versions match your Expo SDK."
}
