{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"otp-reels","type":"registry:ui","title":"OTP Reels","description":"A one-time code input where each digit spins in like a slot reel, shakes on a wrong code and glows on success.","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"registry/ui/otp-reels.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useId, useRef, useState, type ChangeEvent } from \"react\";\nimport { AnimatePresence, MotionConfig, motion, useAnimate } from \"motion/react\";\n\nexport interface OtpReelsProps {\n  /** Number of digits. */\n  length?: number;\n  /** Called with the full code. Return (or resolve) true for success, false to shake and clear. */\n  onComplete?: (code: string) => boolean | void | Promise<boolean | void>;\n  /** Called on every change with the digits typed so far. */\n  onChange?: (code: string) => void;\n  /** Glow color on success. */\n  successColor?: string;\n  /** Border color on a wrong code. */\n  errorColor?: string;\n  /** Accessible label of the input. */\n  label?: string;\n  autoFocus?: boolean;\n  className?: string;\n}\n\ntype Status = \"idle\" | \"checking\" | \"error\" | \"success\";\n\nconst H = 64; // slot height == reel cell height (px)\nconst REEL = Array.from({ length: 20 }, (_, i) => i % 10);\nconst css = `\n@keyframes code-slots-caret{0%,45%{opacity:1}55%,100%{opacity:0}}\n@keyframes code-slots-pulse{0%,100%{opacity:.55}50%{opacity:1}}`;\n\nexport function OtpReels({\n  length = 6,\n  onComplete,\n  onChange,\n  successColor = \"#22c55e\",\n  errorColor = \"#ef4444\",\n  label = \"Verification code\",\n  autoFocus,\n  className,\n}: OtpReelsProps) {\n  // `from` = how many digits existed before the last change, so pasted digits roll in staggered.\n  const [{ value, from }, setState] = useState({ value: \"\", from: 0 });\n  const [status, setStatus] = useState<Status>(\"idle\");\n  const [focused, setFocused] = useState(false);\n  const [scope, animate] = useAnimate<HTMLDivElement>();\n  const input = useRef<HTMLInputElement>(null);\n  const id = useId();\n\n  const check = async (code: string) => {\n    if (!onComplete) return;\n    setStatus(\"checking\");\n    const ok = await onComplete(code);\n    if (ok === true) {\n      setStatus(\"success\");\n      return;\n    }\n    if (ok === false) {\n      setStatus(\"error\");\n      await animate(scope.current, { x: [0, -14, 12, -9, 7, -4, 2, 0] }, { duration: 0.5, ease: \"easeOut\", delay: 0.35 });\n      setState((s) => ({ value: \"\", from: s.value.length }));\n      setStatus(\"idle\");\n      input.current?.focus();\n      return;\n    }\n    setStatus(\"idle\");\n  };\n\n  const handle = (e: ChangeEvent<HTMLInputElement>) => {\n    if (status === \"checking\" || status === \"success\") return;\n    const next = e.target.value.replace(/\\D/g, \"\").slice(0, length);\n    setState({ value: next, from: value.length });\n    if (status === \"error\") setStatus(\"idle\");\n    onChange?.(next);\n    if (next.length === length && next !== value) void check(next);\n  };\n\n  const tone = status === \"success\" ? successColor : status === \"error\" ? errorColor : undefined;\n  const half = length >= 6 && length % 2 === 0 ? length / 2 : -1;\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <style href=\"otp-reels\" precedence=\"default\">\n        {css}\n      </style>\n      <div className={`relative inline-flex ${className ?? \"\"}`}>\n        <div ref={scope} className=\"flex items-center gap-2\" aria-hidden>\n          {Array.from({ length }, (_, i) => {\n            const ch = value[i];\n            const active = focused && status === \"idle\" && i === Math.min(value.length, length - 1) && !(value.length === length);\n            return (\n              <div key={i} className=\"contents\">\n                {i === half && <span className=\"mx-1 h-0.5 w-3 rounded-full bg-border\" />}\n                <motion.div\n                  className=\"relative w-12 overflow-hidden rounded-xl border bg-card text-foreground transition-[border-color,box-shadow] duration-300\"\n                  style={{\n                    height: H,\n                    borderColor: tone ?? (active ? \"var(--ring)\" : \"var(--border)\"),\n                    boxShadow: tone\n                      ? `0 0 0 1px ${tone}, 0 0 26px -6px ${tone}`\n                      : active\n                        ? \"0 0 0 3px color-mix(in oklab, var(--ring) 35%, transparent)\"\n                        : \"inset 0 1px 2px rgb(0 0 0/.12)\",\n                    maskImage: \"linear-gradient(transparent, #000 22%, #000 78%, transparent)\",\n                    animation: status === \"checking\" ? `code-slots-pulse 1s ${i * 0.08}s ease-in-out infinite` : undefined,\n                  }}\n                  animate={status === \"success\" ? { y: [0, -10, 0], scale: [1, 1.06, 1] } : { y: 0, scale: 1 }}\n                  transition={{ duration: 0.5, delay: status === \"success\" ? i * 0.06 : 0, ease: \"easeOut\" }}\n                >\n                  <AnimatePresence initial={false}>\n                    {ch !== undefined && (\n                      <motion.div\n                        key={`${i}-${ch}`}\n                        className=\"absolute inset-x-0 top-0\"\n                        initial={{ y: 0, filter: \"blur(3px)\" }}\n                        animate={{ y: -(10 + Number(ch)) * H, filter: \"blur(0px)\" }}\n                        exit={{ opacity: 0, scale: 0.6, filter: \"blur(4px)\", transition: { duration: 0.18 } }}\n                        transition={{\n                          y: { type: \"spring\", stiffness: 70, damping: 11, mass: 0.9, delay: Math.max(0, i - from) * 0.07 },\n                          filter: { duration: 0.6, delay: Math.max(0, i - from) * 0.07 },\n                        }}\n                      >\n                        {REEL.map((d, k) => (\n                          <span\n                            key={k}\n                            className=\"grid place-items-center text-3xl font-semibold tabular-nums\"\n                            style={{ height: H, color: k === 10 + Number(ch) && tone ? tone : undefined }}\n                          >\n                            {d}\n                          </span>\n                        ))}\n                      </motion.div>\n                    )}\n                  </AnimatePresence>\n                  {active && ch === undefined && (\n                    <span\n                      className=\"absolute left-1/2 top-1/2 h-7 w-0.5 -translate-x-1/2 -translate-y-1/2 rounded-full bg-foreground\"\n                      style={{ animation: \"code-slots-caret 1s step-end infinite\" }}\n                    />\n                  )}\n                </motion.div>\n              </div>\n            );\n          })}\n        </div>\n        <input\n          ref={input}\n          id={id}\n          value={value}\n          onChange={handle}\n          onFocus={() => setFocused(true)}\n          onBlur={() => setFocused(false)}\n          onSelect={(e) => {\n            const el = e.currentTarget;\n            el.setSelectionRange(el.value.length, el.value.length);\n          }}\n          readOnly={status === \"success\"}\n          autoFocus={autoFocus}\n          autoComplete=\"one-time-code\"\n          inputMode=\"numeric\"\n          pattern=\"\\d*\"\n          maxLength={length}\n          aria-label={`${label}, ${length} digits`}\n          aria-invalid={status === \"error\"}\n          spellCheck={false}\n          className=\"absolute inset-0 cursor-text bg-transparent text-transparent caret-transparent opacity-0 outline-none selection:bg-transparent\"\n        />\n        <span className=\"sr-only\" aria-live=\"polite\">\n          {status === \"checking\" ? \"Checking code\" : status === \"error\" ? \"Wrong code, try again\" : status === \"success\" ? \"Code verified\" : \"\"}\n        </span>\n      </div>\n    </MotionConfig>\n  );\n}\n"}]}