{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "spring-pin-input",
  "type": "registry:ui",
  "title": "Spring Pin Input",
  "author": "Rishabh Bose <https://theb0se.vercel.app/>",
  "description": "Tactile OTP verification code input with per-digit spring impulse bounce, staggered completion wave, and harmonic error shake.",
  "dependencies": [
    "framer-motion",
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "components/ui/spring-pin-input.tsx",
      "content": "\"use client\";\n\nimport React, { useRef, useState } from \"react\";\nimport { motion, useReducedMotion } from \"framer-motion\";\nimport { ShieldCheck, RotateCcw } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface SpringPinInputProps {\n  /** Number of OTP verification digits. Default: 6 */\n  length?: number;\n  /** Initial digits string for preview convenience. Default: \"482\" */\n  defaultValue?: string;\n  /** Spring stiffness coefficient for keystroke slot bounce. Default: 420 */\n  stiffness?: number;\n  /** Spring damping coefficient for keystroke slot bounce. Default: 22 */\n  damping?: number;\n  /** Callback fired when all digits are filled */\n  onComplete?: (code: string) => void;\n  className?: string;\n}\n\nexport function SpringPinInput({\n  length = 6,\n  defaultValue = \"482\",\n  stiffness = 420,\n  damping = 22,\n  onComplete,\n  className,\n}: SpringPinInputProps) {\n  const shouldReduceMotion = useReducedMotion() ?? false;\n  const [digits, setDigits] = useState<string[]>(() => {\n    const arr = Array.from({ length }, (_, i) => defaultValue[i] || \"\");\n    return arr;\n  });\n  const [focusedIndex, setFocusedIndex] = useState<number | null>(null);\n  const [shakeCount, setShakeCount] = useState(0);\n  const inputRefs = useRef<Array<HTMLInputElement | null>>([]);\n\n  const isComplete = digits.every((d) => d !== \"\");\n\n  const updateDigit = (index: number, char: string) => {\n    if (!/^[0-9]?$/.test(char)) {\n      setShakeCount((c) => c + 1);\n      return;\n    }\n\n    const next = [...digits];\n    next[index] = char;\n    setDigits(next);\n\n    if (char && index < length - 1) {\n      inputRefs.current[index + 1]?.focus();\n    }\n\n    if (next.every((d) => d !== \"\")) {\n      onComplete?.(next.join(\"\"));\n    }\n  };\n\n  const handleKeyDown = (\n    index: number,\n    e: React.KeyboardEvent<HTMLInputElement>\n  ) => {\n    if (e.key === \"Backspace\") {\n      e.preventDefault();\n      if (digits[index]) {\n        updateDigit(index, \"\");\n      } else if (index > 0) {\n        updateDigit(index - 1, \"\");\n        inputRefs.current[index - 1]?.focus();\n      }\n    } else if (e.key === \"ArrowLeft\" && index > 0) {\n      e.preventDefault();\n      inputRefs.current[index - 1]?.focus();\n    } else if (e.key === \"ArrowRight\" && index < length - 1) {\n      e.preventDefault();\n      inputRefs.current[index + 1]?.focus();\n    }\n  };\n\n  const handlePaste = (e: React.ClipboardEvent<HTMLInputElement>) => {\n    e.preventDefault();\n    const pasted = e.clipboardData\n      .getData(\"text\")\n      .replace(/\\D/g, \"\")\n      .slice(0, length);\n    if (!pasted) {\n      setShakeCount((c) => c + 1);\n      return;\n    }\n    const next = Array.from({ length }, (_, i) => pasted[i] || \"\");\n    setDigits(next);\n    const focusIdx = Math.min(pasted.length, length - 1);\n    inputRefs.current[focusIdx]?.focus();\n    if (next.every((d) => d !== \"\")) {\n      onComplete?.(next.join(\"\"));\n    }\n  };\n\n  const resetPin = () => {\n    setDigits(Array.from({ length }, () => \"\"));\n    inputRefs.current[0]?.focus();\n  };\n\n  return (\n    <motion.div\n      key={shakeCount}\n      animate={\n        shakeCount > 0 && !shouldReduceMotion\n          ? { x: [0, -8, 8, -5, 5, 0] }\n          : { x: 0 }\n      }\n      transition={{ duration: 0.32 }}\n      className={cn(\n        \"inline-flex flex-col items-center gap-3 rounded-2xl border border-neutral-200 bg-white p-5 dark:border-[#222222] dark:bg-[#0A0A0A]\",\n        className\n      )}\n    >\n      <div className=\"flex w-full items-center justify-between text-xs\">\n        <span className=\"inline-flex items-center gap-1.5 font-semibold text-black dark:text-white\">\n          <ShieldCheck className=\"h-4 w-4\" />\n          <span>{isComplete ? \"Code Verified\" : \"Verification Code\"}</span>\n        </span>\n        <button\n          type=\"button\"\n          onClick={resetPin}\n          aria-label=\"Clear verification digits\"\n          className=\"inline-flex items-center gap-1 text-[11px] text-neutral-400 transition-colors hover:text-black dark:hover:text-white\"\n        >\n          <RotateCcw className=\"h-3 w-3\" />\n          Clear\n        </button>\n      </div>\n\n      <div\n        role=\"group\"\n        aria-label=\"One-time verification code digits\"\n        className=\"flex items-center gap-2\"\n      >\n        {digits.map((digit, idx) => {\n          const isFocused = focusedIndex === idx;\n          const isFilled = digit !== \"\";\n\n          return (\n            <motion.div\n              key={idx}\n              animate={\n                shouldReduceMotion\n                  ? {}\n                  : {\n                      y: isComplete ? [0, -6, 0] : isFilled ? -2 : 0,\n                      scale: isFocused ? 1.07 : isFilled ? 1.02 : 1,\n                    }\n              }\n              transition={{\n                type: \"spring\",\n                stiffness,\n                damping,\n                delay: isComplete ? idx * 0.035 : 0,\n              }}\n              className=\"relative\"\n            >\n              <input\n                ref={(el) => {\n                  inputRefs.current[idx] = el;\n                }}\n                type=\"text\"\n                inputMode=\"numeric\"\n                maxLength={1}\n                value={digit}\n                aria-label={`Digit ${idx + 1} of ${length}`}\n                onFocus={(e) => {\n                  setFocusedIndex(idx);\n                  e.target.select();\n                }}\n                onBlur={() => setFocusedIndex(null)}\n                onChange={(e) => updateDigit(idx, e.target.value.slice(-1))}\n                onKeyDown={(e) => handleKeyDown(idx, e)}\n                onPaste={handlePaste}\n                className={cn(\n                  \"h-12 w-10 rounded-xl border text-center font-mono text-base font-bold transition-colors focus:outline-none\",\n                  isFocused\n                    ? \"border-black bg-white text-black ring-2 ring-black/15 dark:border-white dark:bg-[#121212] dark:text-white dark:ring-white/15\"\n                    : isFilled\n                      ? \"border-neutral-400 bg-neutral-50 text-black dark:border-[#383838] dark:bg-[#141414] dark:text-white\"\n                      : \"border-neutral-200 bg-white text-neutral-400 dark:border-[#222222] dark:bg-[#0D0D0D]\"\n                )}\n              />\n            </motion.div>\n          );\n        })}\n      </div>\n\n      <div className=\"text-[11px] text-neutral-400 dark:text-neutral-500\">\n        Type digits, paste code, or press non-numeric key to test shake\n      </div>\n    </motion.div>\n  );\n}\n\nexport default SpringPinInput;\n",
      "type": "registry:ui",
      "target": "components/ui/spring-pin-input.tsx"
    },
    {
      "path": "lib/utils.ts",
      "content": "import { type ClassValue, clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport async function copyToClipboard(text: string): Promise<boolean> {\n  if (typeof window === \"undefined\") return false;\n\n  try {\n    if (navigator.clipboard && window.isSecureContext) {\n      await navigator.clipboard.writeText(text);\n      return true;\n    }\n\n    const textArea = document.createElement(\"textarea\");\n    textArea.value = text;\n    textArea.style.position = \"fixed\";\n    textArea.style.left = \"-999999px\";\n    textArea.style.top = \"-999999px\";\n    document.body.appendChild(textArea);\n    textArea.focus();\n    textArea.select();\n    const succeeded = document.execCommand(\"copy\");\n    textArea.remove();\n    return succeeded;\n  } catch {\n    return false;\n  }\n}\n",
      "type": "registry:lib",
      "target": "lib/utils.ts"
    }
  ]
}
