{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"input-otp","type":"registry:block","title":"Input OTP","description":"A six-digit code input with per-digit focus lifting, paste and arrow-key handling, a resend timer, and a verified state.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/input-otp/input-otp.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useRef, useState, type ClipboardEvent, type KeyboardEvent } from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { Check, RotateCcw } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type InputOtpProps = {\n  className?: string;\n  /** Number of digits. Defaults to 6. */\n  length?: number;\n  /** Prefilled code. Defaults to the demo code. */\n  defaultValue?: string;\n  label?: string;\n  /** Email or phone the code was sent to. */\n  destination?: string;\n  /** Seconds before the resend button becomes available. */\n  resendSeconds?: number;\n  onComplete?: (code: string) => void;\n  onResend?: () => void;\n};\n\n/* ------------------------------------------------------------------ */\n/* helpers                                                             */\n/* ------------------------------------------------------------------ */\n\nfunction digitsOnly(value: string, length: number) {\n  return value.replace(/\\D/g, \"\").slice(0, length);\n}\n\nfunction makeDigits(value: string, length: number) {\n  const clean = digitsOnly(value, length);\n  return Array.from({ length }, (_, index) => clean[index] ?? \"\");\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function InputOtp({\n  className,\n  length = 6,\n  defaultValue = \"240917\",\n  label = \"Verify your sign-in\",\n  destination = \"ama@northwindlabs.com\",\n  resendSeconds = 30,\n  onComplete,\n  onResend,\n}: InputOtpProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const inputs = useRef<Array<HTMLInputElement | null>>([]);\n  const inView = useInView(ref, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n\n  const [digits, setDigits] = useState<string[]>(() => makeDigits(defaultValue, length));\n  const [active, setActive] = useState(-1);\n  const [seconds, setSeconds] = useState(resendSeconds);\n\n  const complete = digits.every((digit) => digit !== \"\");\n  const code = digits.join(\"\");\n\n  useEffect(() => {\n    if (seconds <= 0) return;\n    const timer = setTimeout(() => setSeconds((value) => Math.max(0, value - 1)), 1000);\n    return () => clearTimeout(timer);\n  }, [seconds]);\n\n  const focusCell = (index: number) => {\n    const next = Math.max(0, Math.min(length - 1, index));\n    setActive(next);\n    requestAnimationFrame(() => inputs.current[next]?.focus());\n  };\n\n  const commit = (next: string[], focus?: number) => {\n    setDigits(next);\n    if (focus !== undefined) focusCell(focus);\n    const nextCode = next.join(\"\");\n    if (next.every((digit) => digit !== \"\") && nextCode.length === length) {\n      onComplete?.(nextCode);\n    }\n  };\n\n  const handleChange = (index: number, raw: string) => {\n    const clean = raw.replace(/\\D/g, \"\");\n    const next = [...digits];\n\n    if (!clean) {\n      next[index] = \"\";\n      commit(next, index);\n      return;\n    }\n\n    let cursor = index;\n    for (const char of clean) {\n      if (cursor >= length) break;\n      next[cursor] = char;\n      cursor += 1;\n    }\n\n    commit(next, Math.min(cursor, length - 1));\n  };\n\n  const handleKeyDown = (index: number, event: KeyboardEvent<HTMLInputElement>) => {\n    if (event.key === \"Backspace\") {\n      event.preventDefault();\n      const next = [...digits];\n      if (next[index]) {\n        next[index] = \"\";\n        commit(next, index);\n      } else if (index > 0) {\n        next[index - 1] = \"\";\n        commit(next, index - 1);\n      }\n      return;\n    }\n\n    if (event.key === \"Delete\") {\n      event.preventDefault();\n      const next = [...digits];\n      next[index] = \"\";\n      commit(next, index);\n      return;\n    }\n\n    if (event.key === \"ArrowLeft\") {\n      event.preventDefault();\n      focusCell(index - 1);\n    }\n\n    if (event.key === \"ArrowRight\") {\n      event.preventDefault();\n      focusCell(index + 1);\n    }\n  };\n\n  const handlePaste = (index: number, event: ClipboardEvent<HTMLInputElement>) => {\n    const pasted = event.clipboardData.getData(\"text\");\n    const clean = digitsOnly(pasted, length - index);\n    if (!clean) return;\n\n    event.preventDefault();\n    const next = [...digits];\n    clean.split(\"\").forEach((char, offset) => {\n      next[index + offset] = char;\n    });\n    commit(next, Math.min(index + clean.length, length - 1));\n  };\n\n  const reset = () => {\n    const empty = Array.from({ length }, () => \"\");\n    setDigits(empty);\n    focusCell(0);\n  };\n\n  const resend = () => {\n    if (seconds > 0) return;\n    setSeconds(resendSeconds);\n    onResend?.();\n    reset();\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div ref={ref} className={cn(\"flex w-full justify-center\", className)}>\n        <motion.div\n          initial={{ opacity: 0, y: reduce ? 0 : 16 }}\n          animate={{ opacity: inView ? 1 : 0, y: 0 }}\n          transition={{ duration: reduce ? 0 : 0.55, ease: EASE }}\n          className=\"w-full max-w-sm rounded-xl border border-border bg-card p-5 text-card-foreground shadow-sm sm:p-6\"\n        >\n          <motion.div\n            initial={{ opacity: 0, y: reduce ? 0 : 6 }}\n            animate={{ opacity: inView ? 1 : 0, y: 0 }}\n            transition={{ duration: reduce ? 0 : 0.5, ease: EASE, delay: reduce ? 0 : 0.05 }}\n          >\n            <h2 className=\"text-[17px] font-semibold tracking-tight\">{label}</h2>\n            <p className=\"mt-1 text-[12.5px] leading-5 text-muted-foreground\">\n              Enter the {length}-digit code sent to{\" \"}\n              <span className=\"font-medium text-foreground\">{destination}</span>.\n            </p>\n          </motion.div>\n\n          <div\n            role=\"group\"\n            aria-label={`${label}, ${length} digits`}\n            className=\"mt-5 flex items-center gap-1.5 sm:gap-2\"\n          >\n            {digits.map((digit, index) => {\n              const isActive = active === index;\n              return (\n                <motion.div\n                  key={index}\n                  initial={reduce ? false : { opacity: 0, y: 8 }}\n                  animate={{ opacity: inView ? 1 : 0, y: 0 }}\n                  transition={{\n                    duration: reduce ? 0 : 0.4,\n                    ease: EASE,\n                    delay: reduce ? 0 : 0.08 + index * 0.035,\n                  }}\n                  className=\"min-w-0 flex-1\"\n                >\n                  <motion.input\n                    ref={(element) => {\n                      inputs.current[index] = element;\n                    }}\n                    type=\"text\"\n                    inputMode=\"numeric\"\n                    autoComplete={index === 0 ? \"one-time-code\" : \"off\"}\n                    pattern=\"[0-9]*\"\n                    maxLength={1}\n                    value={digit}\n                    aria-label={`Digit ${index + 1} of ${length}`}\n                    aria-invalid={false}\n                    onChange={(event) => handleChange(index, event.target.value)}\n                    onKeyDown={(event) => handleKeyDown(index, event)}\n                    onPaste={(event) => handlePaste(index, event)}\n                    onFocus={(event) => {\n                      setActive(index);\n                      event.currentTarget.select();\n                    }}\n                    onBlur={() => setActive((current) => (current === index ? -1 : current))}\n                    animate={reduce ? undefined : { y: isActive ? -2 : 0 }}\n                    transition={{ duration: 0.2, ease: EASE }}\n                    className={cn(\n                      \"h-12 w-full rounded-lg border bg-background text-center text-[19px] font-semibold tabular-nums caret-foreground transition-colors sm:h-14 sm:text-[21px]\",\n                      \"focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\",\n                      isActive ? \"border-foreground/40\" : \"border-border\"\n                    )}\n                  />\n                </motion.div>\n              );\n            })}\n          </div>\n\n          <div className=\"mt-4 min-h-[22px]\" aria-live=\"polite\">\n            <AnimatePresence mode=\"wait\" initial={false}>\n              {complete ? (\n                <motion.div\n                  key=\"accepted\"\n                  initial={reduce ? false : { opacity: 0, y: 4 }}\n                  animate={{ opacity: 1, y: 0 }}\n                  exit={{ opacity: 0 }}\n                  transition={{ duration: reduce ? 0 : 0.3, ease: EASE }}\n                  className=\"flex items-center gap-2\"\n                >\n                  <span className=\"relative flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-success/10 text-success\">\n                    <Check className=\"h-3 w-3\" strokeWidth={3} aria-hidden=\"true\" />\n                    {!reduce ? (\n                      <motion.span\n                        aria-hidden=\"true\"\n                        initial={{ scale: 0.6, opacity: 0.55 }}\n                        animate={{ scale: 1.9, opacity: 0 }}\n                        transition={{ duration: 0.9, ease: \"easeOut\" }}\n                        className=\"absolute inset-0 rounded-full ring-1 ring-success\"\n                      />\n                    ) : null}\n                  </span>\n                  <p className=\"text-[12.5px] text-muted-foreground\">\n                    <span className=\"font-medium text-foreground\">Code accepted</span> — welcome\n                    back, Ama.\n                  </p>\n                </motion.div>\n              ) : (\n                <motion.p\n                  key=\"hint\"\n                  initial={reduce ? false : { opacity: 0 }}\n                  animate={{ opacity: 1 }}\n                  exit={{ opacity: 0 }}\n                  transition={{ duration: reduce ? 0 : 0.25 }}\n                  className=\"text-[12.5px] leading-5 text-muted-foreground\"\n                >\n                  {code.length} of {length} digits entered\n                </motion.p>\n              )}\n            </AnimatePresence>\n          </div>\n\n          <div className=\"mt-4 flex items-center justify-between gap-3 border-t border-border pt-4\">\n            {complete ? (\n              <button\n                type=\"button\"\n                onClick={reset}\n                className=\"inline-flex h-7 items-center gap-1.5 rounded-md text-[12px] font-medium text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\"\n              >\n                <RotateCcw className=\"h-3.5 w-3.5\" aria-hidden=\"true\" />\n                Start over\n              </button>\n            ) : (\n              <button\n                type=\"button\"\n                onClick={resend}\n                disabled={seconds > 0}\n                className={cn(\n                  \"inline-flex h-7 items-center rounded-md text-[12px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\",\n                  seconds > 0\n                    ? \"cursor-default text-muted-foreground\"\n                    : \"text-foreground hover:underline underline-offset-4\"\n                )}\n              >\n                {seconds > 0 ? `Resend code in 0:${String(seconds).padStart(2, \"0\")}` : \"Resend code\"}\n              </button>\n            )}\n\n            <a\n              href=\"/contact\"\n              className=\"rounded-sm text-[12px] text-muted-foreground underline-offset-4 transition-colors hover:text-foreground hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\"\n            >\n              Need help?\n            </a>\n          </div>\n        </motion.div>\n      </div>\n    </MotionConfig>\n  );\n}\n\nexport default InputOtp;\n"}]}