{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "otp-verification-block",
  "title": "OTP / Verification Code Block",
  "description": "A complete two-step verification screen: text, authenticator or email codes, six boxes with auto-advance, paste and auto-submit, a shake and attempts left on a wrong code, a timed lockout, a resend cooldown, an authenticator refresh ring, recovery codes, trust this device, and a success state.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/otp-verification-block/otp-verification-block.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\ntype Method = \"sms\" | \"app\" | \"email\";\n\nexport interface OtpVerificationBlockProps {\n  phone?: string;\n  email?: string;\n  length?: number;\n  /** Return true when the code is right. Defaults to comparing with demoCode. */\n  onVerify?: (code: string, method: Method, trustDevice: boolean) => Promise<boolean> | boolean;\n  onResend?: (method: Method) => void;\n  demoCode?: string;\n  recoveryCode?: string;\n  maxAttempts?: number;\n  resendSeconds?: number;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { bg: \"#0A0A0A\", text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.55)\", faint: \"rgba(245,244,241,0.3)\", panel: \"#0E0E0E\", border: \"rgba(255,255,255,0.1)\", field: \"rgba(255,255,255,0.03)\", fieldBorder: \"rgba(255,255,255,0.12)\", focus: \"rgba(245,244,241,0.85)\", primary: \"#F5F4F1\", onPrimary: \"#0A0A0A\", seg: \"rgba(255,255,255,0.05)\", error: \"#FF7A6B\", ok: \"#3FBF7F\", ring: \"rgba(245,244,241,0.6)\" },\n  light: { bg: \"#F4F4F1\", text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.55)\", faint: \"rgba(10,10,10,0.3)\", panel: \"#FFFFFF\", border: \"rgba(10,10,10,0.1)\", field: \"#FFFFFF\", fieldBorder: \"rgba(10,10,10,0.16)\", focus: \"rgba(10,10,10,0.85)\", primary: \"#0A0A0A\", onPrimary: \"#FFFFFF\", seg: \"rgba(10,10,10,0.05)\", error: \"#E5484D\", ok: \"#1F9D5C\", ring: \"rgba(10,10,10,0.5)\" },\n};\n\nconst METHODS: { id: Method; label: string }[] = [\n  { id: \"sms\", label: \"Text message\" },\n  { id: \"app\", label: \"Authenticator\" },\n  { id: \"email\", label: \"Email\" },\n];\nconst mask = (v: string, kind: \"phone\" | \"email\") => (kind === \"phone\" ? `•••• ${v.replace(/\\D/g, \"\").slice(-4)}` : v.replace(/^(.)(.*)(@.*)$/, (_, a: string, b: string, c: string) => `${a}${\"•\".repeat(Math.min(6, b.length))}${c}`));\nconst clock = (s: number) => `${Math.floor(s / 60)}:${String(s % 60).padStart(2, \"0\")}`;\n\nexport function OtpVerificationBlock({ phone = \"+49 151 2234 4821\", email = \"lena@nexora.io\", length = 6, onVerify, onResend, demoCode = \"428617\", recoveryCode = \"4F7Q-2KXM\", maxAttempts = 3, resendSeconds = 30, theme = \"dark\", className }: OtpVerificationBlockProps) {\n  const p = PALETTES[theme];\n  const reduce = !!useReducedMotion();\n  const uid = React.useId();\n  const [method, setMethod] = React.useState<Method>(\"sms\");\n  const [digits, setDigits] = React.useState<string[]>(() => Array(length).fill(\"\"));\n  const [status, setStatus] = React.useState<\"idle\" | \"checking\" | \"error\" | \"locked\" | \"done\">(\"idle\");\n  const [attempts, setAttempts] = React.useState(0);\n  const [resendLeft, setResendLeft] = React.useState(resendSeconds);\n  const [lockLeft, setLockLeft] = React.useState(0);\n  const [appTick, setAppTick] = React.useState(0);\n  const [trust, setTrust] = React.useState(true);\n  const [recovery, setRecovery] = React.useState(false);\n  const [recoveryText, setRecoveryText] = React.useState(\"\");\n  const [toast, setToast] = React.useState<string | null>(null);\n  const [shake, setShake] = React.useState(0);\n  const boxes = React.useRef<(HTMLInputElement | null)[]>([]);\n  const timer = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n\n  // One clock drives the resend cooldown, the lockout and the authenticator ring.\n  React.useEffect(() => {\n    if (status === \"done\") return;\n    const id = setInterval(() => {\n      setResendLeft((s) => Math.max(0, s - 1));\n      setLockLeft((s) => Math.max(0, s - 1));\n      setAppTick((t) => (t + 1) % 30);\n    }, 1000);\n    return () => clearInterval(id);\n  }, [status]);\n  React.useEffect(() => {\n    if (status === \"locked\" && lockLeft === 0) {\n      const t = setTimeout(() => {\n        setStatus(\"idle\");\n        setAttempts(0);\n        boxes.current[0]?.focus();\n      }, 0);\n      return () => clearTimeout(t);\n    }\n  }, [status, lockLeft]);\n  React.useEffect(() => {\n    if (!toast) return;\n    const t = setTimeout(() => setToast(null), 2200);\n    return () => clearTimeout(t);\n  }, [toast]);\n  React.useEffect(\n    () => () => {\n      if (timer.current) clearTimeout(timer.current);\n    },\n    [],\n  );\n\n  const reset = (focus = true) => {\n    setDigits(Array(length).fill(\"\"));\n    if (focus) requestAnimationFrame(() => boxes.current[0]?.focus());\n  };\n  const verify = async (code: string) => {\n    setStatus(\"checking\");\n    const ok = onVerify\n      ? await onVerify(code, method, trust)\n      : await new Promise<boolean>((resolve) => {\n          timer.current = setTimeout(() => resolve(code === demoCode || code === recoveryCode), reduce ? 0 : 750);\n        });\n    if (ok) {\n      setStatus(\"done\");\n      return;\n    }\n    const n = attempts + 1;\n    setAttempts(n);\n    setShake((s) => s + 1);\n    if (n >= maxAttempts) {\n      setStatus(\"locked\");\n      setLockLeft(30);\n      reset(false);\n    } else {\n      setStatus(\"error\");\n      reset();\n    }\n  };\n  const fill = (start: number, chars: string) => {\n    const clean = chars.replace(/\\D/g, \"\").slice(0, length - start);\n    if (!clean) return;\n    const next = [...digits];\n    [...clean].forEach((c, i) => (next[start + i] = c));\n    setDigits(next);\n    if (status === \"error\") setStatus(\"idle\");\n    const end = Math.min(start + clean.length, length - 1);\n    boxes.current[end]?.focus();\n    if (next.every(Boolean)) void verify(next.join(\"\"));\n  };\n  const onKey = (i: number, e: React.KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"Backspace\") {\n      e.preventDefault();\n      const next = [...digits];\n      if (next[i]) next[i] = \"\";\n      else if (i > 0) {\n        next[i - 1] = \"\";\n        boxes.current[i - 1]?.focus();\n      }\n      setDigits(next);\n    } else if (e.key === \"ArrowLeft\" && i > 0) boxes.current[i - 1]?.focus();\n    else if (e.key === \"ArrowRight\" && i < length - 1) boxes.current[i + 1]?.focus();\n  };\n  const switchMethod = (m: Method) => {\n    setMethod(m);\n    setRecovery(false);\n    if (status === \"error\") setStatus(\"idle\");\n    reset();\n    if (m !== \"app\") setResendLeft(resendSeconds);\n  };\n  const resend = () => {\n    setResendLeft(resendSeconds);\n    setToast(`New code sent to ${method === \"sms\" ? mask(phone, \"phone\") : mask(email, \"email\")}`);\n    onResend?.(method);\n    reset();\n  };\n\n  const ring = { [\"--tw-ring-color\" as string]: p.ring, [\"--tw-ring-offset-color\" as string]: p.panel } as React.CSSProperties;\n  const focus = \"outline-none focus-visible:ring-2 focus-visible:ring-offset-2\";\n  const busy = status === \"checking\";\n  const locked = status === \"locked\";\n  const left = maxAttempts - attempts;\n  const sentTo = method === \"sms\" ? mask(phone, \"phone\") : method === \"email\" ? mask(email, \"email\") : null;\n  const recoveryOk = /^[A-Z0-9]{4}-[A-Z0-9]{4}$/.test(recoveryText.trim().toUpperCase());\n\n  return (\n    <section className={cn(\"@container grid min-h-[640px] w-full place-items-center px-4 py-12\", className)} style={{ background: p.bg, color: p.text }}>\n      <motion.div layout={!reduce} transition={{ duration: 0.25 }} className=\"w-full max-w-[400px] rounded-2xl border p-6 @[480px]:p-8\" style={{ background: p.panel, borderColor: p.border }}>\n        <AnimatePresence mode=\"wait\" initial={false}>\n          {status === \"done\" ? (\n            <motion.div key=\"done\" initial={reduce ? false : { opacity: 0, scale: 0.97 }} animate={{ opacity: 1, scale: 1 }} className=\"flex flex-col items-center py-4 text-center\" role=\"status\">\n              <motion.span className=\"grid h-14 w-14 place-items-center rounded-full\" style={{ background: `${p.ok}22`, color: p.ok }} initial={reduce ? false : { scale: 0.5 }} animate={{ scale: 1 }} transition={{ type: \"spring\", stiffness: 420, damping: 16 }}>\n                <svg width=\"26\" height=\"26\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.4\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                  <motion.path d=\"m5 12.5 4.5 4.5L19 7.5\" initial={reduce ? false : { pathLength: 0 }} animate={{ pathLength: 1 }} transition={{ duration: 0.35, delay: 0.1 }} />\n                </svg>\n              </motion.span>\n              <h2 className=\"m-0 mt-5 text-[22px] font-semibold tracking-[-0.02em]\">You&apos;re verified</h2>\n              <p className=\"m-0 mt-2 text-[14px] leading-relaxed\" style={{ color: p.muted }}>\n                {trust ? \"We won't ask for a code on this device for 30 days.\" : \"We'll ask for a code next time you sign in.\"}\n              </p>\n              <button\n                type=\"button\"\n                onClick={() => {\n                  setStatus(\"idle\");\n                  setAttempts(0);\n                  setRecovery(false);\n                  setRecoveryText(\"\");\n                  reset();\n                }}\n                className={cn(\"mt-6 h-11 w-full cursor-pointer rounded-xl border-0 text-[14px] font-semibold\", focus)}\n                style={{ background: p.primary, color: p.onPrimary, ...ring }}\n              >\n                Continue to dashboard\n              </button>\n            </motion.div>\n          ) : (\n            <motion.div key=\"form\" initial={reduce ? false : { opacity: 0 }} animate={{ opacity: 1 }} exit={reduce ? undefined : { opacity: 0 }}>\n              <span className=\"grid h-11 w-11 place-items-center rounded-xl border\" style={{ borderColor: p.border, color: p.text }} aria-hidden=\"true\">\n                <svg width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n                  <path d=\"M12 3 4.5 6v5.5c0 4.3 3.1 8.2 7.5 9.5 4.4-1.3 7.5-5.2 7.5-9.5V6L12 3Z\" />\n                  <path d=\"m9 12 2 2 4-4\" />\n                </svg>\n              </span>\n              <h2 className=\"m-0 mt-5 text-[22px] font-semibold tracking-[-0.02em]\">Two-step verification</h2>\n              <p className=\"m-0 mt-1.5 text-[14px] leading-relaxed\" style={{ color: p.muted }} aria-live=\"polite\">\n                {recovery ? \"Enter one of the recovery codes you saved when you set up two-step verification.\" : sentTo ? (\n                  <>\n                    Enter the {length}-digit code we sent to <span style={{ color: p.text }}>{sentTo}</span>.\n                  </>\n                ) : (\n                  \"Open your authenticator app and enter the current code for Nexora.\"\n                )}\n              </p>\n\n              {!recovery && (\n                <div role=\"radiogroup\" aria-label=\"Where to get the code\" className=\"mt-5 grid grid-cols-3 rounded-xl p-1\" style={{ background: p.seg }}>\n                  {METHODS.map((m) => (\n                    <button key={m.id} type=\"button\" role=\"radio\" aria-checked={method === m.id} onClick={() => switchMethod(m.id)} disabled={busy} className={cn(\"relative h-8 cursor-pointer rounded-lg border-0 bg-transparent px-1 text-[12px] font-medium disabled:cursor-wait\", focus)} style={{ color: method === m.id ? p.text : p.muted, ...ring }}>\n                      {method === m.id && <motion.span layoutId={`${uid}-m`} className=\"absolute inset-0 rounded-lg border\" style={{ background: p.panel, borderColor: p.border }} transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 520, damping: 38 }} />}\n                      <span className=\"relative\">{m.label}</span>\n                    </button>\n                  ))}\n                </div>\n              )}\n\n              {recovery ? (\n                <form\n                  className=\"mt-5\"\n                  onSubmit={(e) => {\n                    e.preventDefault();\n                    if (recoveryOk && !busy && !locked) void verify(recoveryText.trim().toUpperCase());\n                  }}\n                >\n                  <label htmlFor={`${uid}-rc`} className=\"text-[13px] font-medium\">\n                    Recovery code\n                  </label>\n                  <input id={`${uid}-rc`} value={recoveryText} onChange={(e) => setRecoveryText(e.target.value.toUpperCase())} placeholder=\"XXXX-XXXX\" autoComplete=\"off\" spellCheck={false} maxLength={9} disabled={locked} className={cn(\"mt-1.5 h-11 w-full rounded-xl border px-3.5 font-mono text-[15px] tracking-[0.12em]\", focus)} style={{ borderColor: status === \"error\" ? p.error : p.fieldBorder, background: p.field, color: p.text, ...ring }} />\n                  <button type=\"submit\" disabled={!recoveryOk || busy || locked} className={cn(\"mt-4 flex h-11 w-full cursor-pointer items-center justify-center gap-2 rounded-xl border-0 text-[14px] font-semibold disabled:cursor-not-allowed disabled:opacity-50\", focus)} style={{ background: p.primary, color: p.onPrimary, ...ring }}>\n                    {busy ? \"Checking…\" : \"Verify\"}\n                  </button>\n                </form>\n              ) : (\n                <motion.div key={shake} className=\"mt-5 flex items-center justify-between gap-1.5 @[400px]:gap-2\" animate={shake && !reduce ? { x: [0, -8, 8, -5, 5, 0] } : undefined} transition={{ duration: 0.35 }} role=\"group\" aria-label={`${length}-digit code`}>\n                  {digits.map((d, i) => (\n                    <React.Fragment key={i}>\n                      {i === length / 2 && (\n                        <span className=\"h-px w-2 shrink-0\" style={{ background: p.faint }} aria-hidden=\"true\" />\n                      )}\n                      <input\n                        ref={(el) => {\n                          boxes.current[i] = el;\n                        }}\n                        value={d}\n                        onChange={(e) => fill(i, e.target.value.slice(-1) || \"\")}\n                        onPaste={(e) => {\n                          e.preventDefault();\n                          fill(0, e.clipboardData.getData(\"text\"));\n                        }}\n                        onKeyDown={(e) => onKey(i, e)}\n                        onFocus={(e) => e.currentTarget.select()}\n                        inputMode=\"numeric\"\n                        autoComplete={i === 0 ? \"one-time-code\" : \"off\"}\n                        maxLength={1}\n                        disabled={busy || locked}\n                        aria-label={`Digit ${i + 1}`}\n                        aria-invalid={status === \"error\"}\n                        className=\"h-12 w-full min-w-0 rounded-xl border text-center text-[20px] font-semibold tabular-nums transition-colors outline-none focus:border-[var(--f)] disabled:opacity-60 @[400px]:h-14\"\n                        style={{ borderColor: status === \"error\" ? p.error : d ? p.focus : p.fieldBorder, background: p.field, color: p.text, [\"--f\" as string]: p.focus, caretColor: p.text }}\n                      />\n                    </React.Fragment>\n                  ))}\n                </motion.div>\n              )}\n\n              <div className=\"mt-3 min-h-[20px] text-[12.5px]\" aria-live=\"assertive\">\n                <AnimatePresence mode=\"wait\" initial={false}>\n                  {busy ? (\n                    <motion.p key=\"c\" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} className=\"m-0 flex items-center gap-2\" style={{ color: p.muted }}>\n                      <motion.span className=\"h-3 w-3 rounded-full border-2 border-current border-t-transparent\" animate={reduce ? undefined : { rotate: 360 }} transition={{ repeat: Infinity, duration: 0.7, ease: \"linear\" }} />\n                      Checking code…\n                    </motion.p>\n                  ) : locked ? (\n                    <motion.p key=\"l\" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} className=\"m-0\" style={{ color: p.error }}>\n                      Too many attempts. Try again in {clock(lockLeft)}.\n                    </motion.p>\n                  ) : status === \"error\" ? (\n                    <motion.p key=\"e\" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} className=\"m-0\" style={{ color: p.error }}>\n                      That code didn&apos;t work. {left} {left === 1 ? \"attempt\" : \"attempts\"} left.\n                    </motion.p>\n                  ) : !recovery && method === \"app\" ? (\n                    <motion.p key=\"a\" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} className=\"m-0 flex items-center gap-2\" style={{ color: p.muted }}>\n                      <svg width=\"14\" height=\"14\" viewBox=\"0 0 36 36\" aria-hidden=\"true\">\n                        <circle cx=\"18\" cy=\"18\" r=\"15\" fill=\"none\" stroke={p.border} strokeWidth=\"4\" />\n                        <circle cx=\"18\" cy=\"18\" r=\"15\" fill=\"none\" stroke={p.text} strokeWidth=\"4\" strokeDasharray={94.2} strokeDashoffset={(94.2 * appTick) / 30} transform=\"rotate(-90 18 18)\" strokeLinecap=\"round\" />\n                      </svg>\n                      Codes refresh every 30 seconds · {30 - appTick}s left\n                    </motion.p>\n                  ) : (\n                    <motion.p key=\"d\" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} className=\"m-0\" style={{ color: p.faint }}>\n                      Demo code: {demoCode.slice(0, 3)} {demoCode.slice(3)}\n                    </motion.p>\n                  )}\n                </AnimatePresence>\n              </div>\n\n              <label className=\"mt-4 flex cursor-pointer items-center gap-2.5 text-[13px] has-[:focus-visible]:underline\" style={{ color: p.muted }}>\n                <input type=\"checkbox\" checked={trust} onChange={(e) => setTrust(e.target.checked)} className=\"h-4 w-4 cursor-pointer\" style={{ accentColor: p.primary }} />\n                Trust this device for 30 days\n              </label>\n\n              <div className=\"mt-6 flex flex-wrap items-center justify-between gap-2 border-t pt-4 text-[13px]\" style={{ borderColor: p.border }}>\n                {recovery ? (\n                  <button type=\"button\" onClick={() => switchMethod(method)} className={cn(\"cursor-pointer rounded border-0 bg-transparent p-0 text-[13px] underline underline-offset-2\", focus)} style={{ color: p.text, ...ring }}>\n                    Use a code instead\n                  </button>\n                ) : method === \"app\" ? (\n                  <span style={{ color: p.faint }}>No code? Check your app&apos;s clock.</span>\n                ) : resendLeft > 0 ? (\n                  <span className=\"tabular-nums\" style={{ color: p.faint }}>\n                    Resend code in {clock(resendLeft)}\n                  </span>\n                ) : (\n                  <button type=\"button\" onClick={resend} className={cn(\"cursor-pointer rounded border-0 bg-transparent p-0 text-[13px] font-medium underline underline-offset-2\", focus)} style={{ color: p.text, ...ring }}>\n                    Resend code\n                  </button>\n                )}\n                {!recovery && (\n                  <button\n                    type=\"button\"\n                    onClick={() => {\n                      setRecovery(true);\n                      if (status === \"error\") setStatus(\"idle\");\n                    }}\n                    className={cn(\"cursor-pointer rounded border-0 bg-transparent p-0 text-[13px]\", focus)}\n                    style={{ color: p.muted, ...ring }}\n                  >\n                    Use a recovery code\n                  </button>\n                )}\n              </div>\n            </motion.div>\n          )}\n        </AnimatePresence>\n      </motion.div>\n      <AnimatePresence>\n        {toast && (\n          <motion.div key={toast} role=\"status\" initial={reduce ? false : { opacity: 0, y: 8 }} animate={{ opacity: 1, y: 0 }} exit={reduce ? undefined : { opacity: 0 }} className=\"mt-4 rounded-full border px-3.5 py-1.5 text-[12.5px]\" style={{ background: p.panel, borderColor: p.border, color: p.text }}>\n            {toast}\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </section>\n  );\n}\n\nexport default OtpVerificationBlock;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}