{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "verification-code",
  "type": "registry:block",
  "title": "Verification code",
  "description": "Enter a one-time code sent by SMS, email or an authenticator app: one box per digit, paste fills them all, the last digit submits, and 'Resend' waits for a countdown. For MFA at login, confirming an email at sign-up, or approving a payment.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "https://unbuiltui.com/r/banner.json",
    "https://unbuiltui.com/r/button.json",
    "https://unbuiltui.com/r/input.json",
    "https://unbuiltui.com/r/utils.json"
  ],
  "files": [
    {
      "path": "components/blocks/verification-code.tsx",
      "type": "registry:component",
      "target": "components/blocks/verification-code.tsx",
      "content": "\"use client\";\n\nimport { CircleAlert } from \"lucide-react\";\nimport * as React from \"react\";\nimport { Banner, BannerDescription, BannerTitle } from \"@/components/ui/banner\";\nimport { Button } from \"@/components/ui/button\";\nimport { Input } from \"@/components/ui/input\";\nimport { cn } from \"@/lib/utils\";\n\n// UnbuiltUI block: enter a one-time code sent by SMS, email or an authenticator app.\n// One box per digit, paste fills them all, the last digit submits, and \"Resend\"\n// waits for a countdown. Works for sign-in (MFA), sign-up and confirming a payment.\n\ntype VerificationCodeProps = {\n  title?: string;\n  /** Where the code went, e.g. \"+34 •••• •• 42\" or \"j•••@mail.com\". */\n  destination?: string;\n  length?: number;\n  /** Server error, e.g. \"That code is wrong or expired\". Clears the boxes. */\n  error?: string;\n  loading?: boolean;\n  onComplete?: (code: string) => void;\n  onResend?: () => void;\n  /** Seconds before Resend is enabled again. */\n  resendSeconds?: number;\n  onUseAnotherMethod?: () => void;\n  className?: string;\n};\n\nfunction VerificationCode({\n  title = \"Enter the code\",\n  destination,\n  length = 6,\n  error,\n  loading = false,\n  onComplete,\n  onResend,\n  resendSeconds = 30,\n  onUseAnotherMethod,\n  className,\n}: VerificationCodeProps) {\n  const id = React.useId();\n  const [digits, setDigits] = React.useState<string[]>(() => Array(length).fill(\"\"));\n  const [wait, setWait] = React.useState(resendSeconds);\n  const boxes = React.useRef<(HTMLInputElement | null)[]>([]);\n  const code = digits.join(\"\");\n\n  React.useEffect(() => {\n    if (wait <= 0) return;\n    const timer = setTimeout(() => setWait((w) => w - 1), 1000);\n    return () => clearTimeout(timer);\n  }, [wait]);\n\n  // A new server error means the code was wrong: clear and start again.\n  React.useEffect(() => {\n    if (!error) return;\n    setDigits(Array(length).fill(\"\"));\n    boxes.current[0]?.focus();\n  }, [error, length]);\n\n  function fill(from: number, text: string) {\n    const typed = text\n      .replace(/\\D/g, \"\")\n      .slice(0, length - from)\n      .split(\"\");\n    if (typed.length === 0) return;\n    const next = [...digits];\n    typed.forEach((digit, i) => {\n      next[from + i] = digit;\n    });\n    setDigits(next);\n    boxes.current[Math.min(from + typed.length, length - 1)]?.focus();\n    if (next.every(Boolean)) onComplete?.(next.join(\"\"));\n  }\n\n  function keyDown(index: number, event: React.KeyboardEvent<HTMLInputElement>) {\n    if (event.key === \"Backspace\" && !digits[index] && index > 0) {\n      const next = [...digits];\n      next[index - 1] = \"\";\n      setDigits(next);\n      boxes.current[index - 1]?.focus();\n      event.preventDefault();\n    }\n    if (event.key === \"ArrowLeft\") boxes.current[index - 1]?.focus();\n    if (event.key === \"ArrowRight\") boxes.current[index + 1]?.focus();\n  }\n\n  return (\n    <form\n      noValidate\n      data-slot=\"verification-code\"\n      onSubmit={(event) => {\n        event.preventDefault();\n        if (code.length === length) onComplete?.(code);\n      }}\n      className={cn(\"flex w-full max-w-sm flex-col gap-6\", className)}\n    >\n      <div className=\"flex flex-col gap-1.5\">\n        <h1 className=\"font-semibold text-2xl tracking-tight\">{title}</h1>\n        <p id={`${id}-hint`} className=\"text-muted-foreground text-sm\">\n          We sent a {length}-digit code\n          {destination ? (\n            <>\n              {\" \"}\n              to <span className=\"font-medium text-foreground\">{destination}</span>\n            </>\n          ) : null}\n          .\n        </p>\n      </div>\n\n      {error && (\n        <Banner variant=\"destructive\">\n          <CircleAlert />\n          <BannerTitle>{error}</BannerTitle>\n          <BannerDescription>Check the latest code we sent and try again.</BannerDescription>\n        </Banner>\n      )}\n\n      <fieldset\n        className=\"flex justify-between gap-2\"\n        aria-describedby={`${id}-hint`}\n        disabled={loading}\n      >\n        <legend className=\"sr-only\">Verification code</legend>\n        {digits.map((digit, index) => (\n          <Input\n            // biome-ignore lint/suspicious/noArrayIndexKey: one box per position\n            key={index}\n            ref={(element) => {\n              boxes.current[index] = element;\n            }}\n            value={digit}\n            inputMode=\"numeric\"\n            autoComplete={index === 0 ? \"one-time-code\" : \"off\"}\n            aria-label={`Digit ${index + 1} of ${length}`}\n            aria-invalid={Boolean(error)}\n            maxLength={length}\n            onChange={(event) => {\n              if (event.target.value) return fill(index, event.target.value);\n              setDigits(digits.map((d, i) => (i === index ? \"\" : d)));\n            }}\n            onPaste={(event) => {\n              event.preventDefault();\n              fill(index, event.clipboardData.getData(\"text\"));\n            }}\n            onKeyDown={(event) => keyDown(index, event)}\n            onFocus={(event) => event.target.select()}\n            className=\"h-14 min-w-0 flex-1 px-0 text-center font-semibold text-xl tabular-nums\"\n          />\n        ))}\n      </fieldset>\n\n      <div className=\"flex flex-col gap-3\">\n        <Button\n          type=\"submit\"\n          size=\"lg\"\n          className=\"w-full\"\n          loading={loading}\n          disabled={code.length < length}\n        >\n          {loading ? \"Verifying\" : \"Verify\"}\n        </Button>\n        <p className=\"text-center text-muted-foreground text-sm\">\n          Didn't get it?{\" \"}\n          {wait > 0 ? (\n            <span className=\"tabular-nums\">Resend in {wait}s</span>\n          ) : (\n            <Button\n              type=\"button\"\n              variant=\"link\"\n              className=\"h-auto p-0\"\n              onClick={() => {\n                setWait(resendSeconds);\n                onResend?.();\n              }}\n            >\n              Resend code\n            </Button>\n          )}\n        </p>\n        {onUseAnotherMethod && (\n          <Button type=\"button\" variant=\"ghost\" className=\"w-full\" onClick={onUseAnotherMethod}>\n            Use another method\n          </Button>\n        )}\n      </div>\n    </form>\n  );\n}\n\nexport { VerificationCode };\n"
    }
  ]
}
