{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "otp-verify",
  "type": "registry:block",
  "title": "OTP Verify",
  "description": "Six-digit email code that submits itself, with a resend countdown.",
  "files": [
    {
      "path": "src/registry/blocks/auth/otp-verify.tsx",
      "type": "registry:block",
      "content": "\"use client\";\n\nimport {\n  useEffect,\n  useId,\n  useRef,\n  useState,\n  type ComponentProps,\n  type CSSProperties,\n  type FormEvent,\n} from \"react\";\nimport { REGEXP_ONLY_DIGITS } from \"input-otp\";\nimport { Loader2 } from \"lucide-react\";\n\nimport { Button } from \"@/components/ui/button\";\nimport {\n  InputOTP,\n  InputOTPGroup,\n  InputOTPSeparator,\n  InputOTPSlot,\n} from \"@/components/ui/input-otp\";\nimport { Label } from \"@/components/ui/label\";\nimport { cn } from \"@/lib/utils\";\n\nexport type OtpVerifyValues = { code: string };\n\nexport type OtpVerifyProps = {\n  /** Product name shown in the wordmark. */\n  brand?: string;\n  title?: string;\n  /** Where the code was sent, already masked, e.g. \"p••••@fieldwork.org\". */\n  destination?: string;\n  /** Seconds before another code can be requested. The first countdown starts on mount. */\n  resendCooldown?: number;\n  footnote?: string;\n  changeEmailHref?: string;\n  continueHref?: string;\n  /** Called with the 6-digit code (automatically once all six are entered). Throw to show an error. */\n  onSubmit?: (values: OtpVerifyValues) => void | Promise<void>;\n  onResend?: () => void | Promise<void>;\n  /** Ink for the wordmark square, rule and button. Defaults to Devsloka blue. */\n  accent?: string;\n  /** Text colour on `accent`. */\n  accentForeground?: string;\n} & Omit<ComponentProps<\"section\">, \"onSubmit\">;\n\nconst DEFAULTS = {\n  brand: \"Larch\",\n  title: \"Enter the code we sent\",\n  destination: \"p••••@fieldwork.org\",\n  footnote: \"Codes expire in 10 minutes. Requesting a new one cancels the old one.\",\n  accent: \"#0A6EFF\",\n  accentForeground: \"#FFFFFF\",\n};\n\nconst CODE_LENGTH = 6;\n\n/** Placeholder until you pass `onSubmit`: waits briefly, then succeeds. */\nconst defaultSubmit: (values: OtpVerifyValues) => Promise<void> = () =>\n  new Promise<void>((resolve) => setTimeout(resolve, 800));\n\nconst defaultResend = () => new Promise<void>((resolve) => setTimeout(resolve, 600));\n\nconst pad = (n: number) => String(n).padStart(2, \"0\");\nconst formatSeconds = (s: number) => `${Math.floor(s / 60)}:${pad(s % 60)}`;\n\n/** Counts down once per second while `seconds > 0`; setting a positive value starts the interval. */\nfunction useCooldown(initial: number) {\n  const [seconds, setSeconds] = useState(initial);\n  const running = seconds > 0;\n\n  useEffect(() => {\n    if (!running) return;\n    const timer = setInterval(() => {\n      setSeconds((current) => Math.max(0, current - 1));\n    }, 1000);\n    return () => clearInterval(timer);\n  }, [running]);\n\n  return [seconds, setSeconds] as const;\n}\n\nexport function OtpVerify({\n  brand = DEFAULTS.brand,\n  title = DEFAULTS.title,\n  destination = DEFAULTS.destination,\n  resendCooldown = 30,\n  footnote = DEFAULTS.footnote,\n  changeEmailHref = \"#\",\n  continueHref = \"#\",\n  onSubmit = defaultSubmit,\n  onResend = defaultResend,\n  accent = DEFAULTS.accent,\n  accentForeground = DEFAULTS.accentForeground,\n  className,\n  style,\n  ...props\n}: OtpVerifyProps) {\n  const id = useId();\n  const titleId = `${id}-title`;\n  const codeId = `${id}-code`;\n  const hintId = `${id}-hint`;\n  const errorId = `${id}-error`;\n\n  const codeRef = useRef<HTMLInputElement>(null);\n  const successHeadingRef = useRef<HTMLHeadingElement>(null);\n  const submittingRef = useRef(false);\n\n  const [code, setCode] = useState(\"\");\n  const [error, setError] = useState<string>();\n  const [status, setStatus] = useState<\"idle\" | \"submitting\" | \"success\">(\"idle\");\n  const [resending, setResending] = useState(false);\n  const [announcement, setAnnouncement] = useState(\"\");\n  const [cooldown, setCooldown] = useCooldown(resendCooldown);\n\n  useEffect(() => {\n    if (status === \"success\") successHeadingRef.current?.focus();\n  }, [status]);\n\n  const verify = async (value: string) => {\n    if (submittingRef.current) return;\n    if (value.length < CODE_LENGTH) {\n      setError(`Enter all ${CODE_LENGTH} digits from the email.`);\n      codeRef.current?.focus();\n      return;\n    }\n    setError(undefined);\n    setAnnouncement(\"Checking the code…\");\n    setStatus(\"submitting\");\n    submittingRef.current = true;\n    try {\n      await onSubmit({ code: value });\n      setStatus(\"success\");\n      setAnnouncement(\"Code accepted.\");\n    } catch {\n      setStatus(\"idle\");\n      setError(`That code didn’t match. Check the newest email from ${brand}, or request another code.`);\n      setAnnouncement(\"\");\n      codeRef.current?.focus();\n    } finally {\n      submittingRef.current = false;\n    }\n  };\n\n  const handleSubmit = (event: FormEvent<HTMLFormElement>) => {\n    event.preventDefault();\n    void verify(code);\n  };\n\n  const handleResend = async () => {\n    if (cooldown > 0 || resending) return;\n    setResending(true);\n    setAnnouncement(\"\");\n    try {\n      await onResend();\n      setCode(\"\");\n      setError(undefined);\n      setCooldown(resendCooldown);\n      setAnnouncement(`New code sent to ${destination}. The previous code no longer works.`);\n      codeRef.current?.focus();\n    } catch {\n      setAnnouncement(\"We couldn’t send a new code. Try again in a minute.\");\n    } finally {\n      setResending(false);\n    }\n  };\n\n  return (\n    <section\n      data-slot=\"otp-verify\"\n      aria-labelledby={titleId}\n      style={\n        {\n          \"--block-accent\": accent,\n          \"--block-accent-fg\": accentForeground,\n          ...style,\n        } as CSSProperties\n      }\n      className={cn(\"w-full bg-background text-foreground\", className)}\n      {...props}\n    >\n      <div className=\"mx-auto flex max-w-lg flex-col items-center px-6 py-16 text-center md:py-24\">\n        <span className=\"inline-flex items-center gap-2 text-base font-semibold tracking-tight\">\n          <span aria-hidden=\"true\" className=\"size-2.5 bg-[var(--block-accent)]\" />\n          {brand}\n        </span>\n\n        <span aria-hidden=\"true\" className=\"mt-10 h-12 w-px bg-border\" />\n\n        {status === \"success\" ? (\n          <div className=\"mt-10\">\n            <p className=\"text-muted-foreground font-mono text-xs tracking-wider uppercase\">\n              Verified\n            </p>\n            <h2\n              id={titleId}\n              ref={successHeadingRef}\n              tabIndex={-1}\n              className=\"mt-4 text-4xl font-semibold tracking-[-0.03em] outline-none md:text-5xl\"\n            >\n              You’re in.\n            </h2>\n            <p className=\"text-muted-foreground mx-auto mt-5 max-w-sm text-base leading-relaxed\">\n              Your email is confirmed. Your team’s notebooks are ready.\n            </p>\n            <Button\n              asChild\n              size=\"lg\"\n              className=\"mt-10 rounded-none bg-[var(--block-accent)] text-[var(--block-accent-fg)] shadow-none hover:bg-[var(--block-accent)] hover:underline hover:underline-offset-4\"\n            >\n              <a href={continueHref}>Open {brand}</a>\n            </Button>\n          </div>\n        ) : (\n          <div className=\"mt-10 flex w-full flex-col items-center\">\n            <h2\n              id={titleId}\n              className=\"text-4xl font-semibold tracking-[-0.03em] text-balance md:text-5xl\"\n            >\n              {title}\n            </h2>\n            <p id={hintId} className=\"text-muted-foreground mt-5 text-base leading-relaxed\">\n              A {CODE_LENGTH}-digit code went to{\" \"}\n              <span className=\"text-foreground font-mono text-sm\">{destination}</span>\n            </p>\n\n            <form\n              noValidate\n              onSubmit={handleSubmit}\n              aria-busy={status === \"submitting\"}\n              className=\"mt-10 flex w-full flex-col items-center\"\n            >\n              <Label htmlFor={codeId} className=\"sr-only\">\n                Verification code\n              </Label>\n              <InputOTP\n                ref={codeRef}\n                id={codeId}\n                name=\"code\"\n                maxLength={CODE_LENGTH}\n                pattern={REGEXP_ONLY_DIGITS}\n                inputMode=\"numeric\"\n                autoComplete=\"one-time-code\"\n                required\n                value={code}\n                onChange={(value) => {\n                  setCode(value);\n                  if (error) setError(undefined);\n                }}\n                onComplete={(value: string) => void verify(value)}\n                readOnly={status === \"submitting\"}\n                aria-invalid={error ? true : undefined}\n                aria-describedby={error ? `${hintId} ${errorId}` : hintId}\n                containerClassName=\"justify-center gap-3\"\n              >\n                <InputOTPGroup>\n                  {[0, 1, 2].map((index) => (\n                    <InputOTPSlot\n                      key={index}\n                      index={index}\n                      aria-invalid={error ? true : undefined}\n                      className=\"h-14 w-11 font-mono text-xl sm:w-12\"\n                    />\n                  ))}\n                </InputOTPGroup>\n                <InputOTPSeparator className=\"text-muted-foreground\" />\n                <InputOTPGroup>\n                  {[3, 4, 5].map((index) => (\n                    <InputOTPSlot\n                      key={index}\n                      index={index}\n                      aria-invalid={error ? true : undefined}\n                      className=\"h-14 w-11 font-mono text-xl sm:w-12\"\n                    />\n                  ))}\n                </InputOTPGroup>\n              </InputOTP>\n\n              <div className=\"mt-3 min-h-5\">\n                {error ? (\n                  <p id={errorId} className=\"text-destructive text-sm\">\n                    {error}\n                  </p>\n                ) : null}\n              </div>\n\n              <Button\n                type=\"submit\"\n                size=\"lg\"\n                disabled={status === \"submitting\"}\n                className=\"mt-6 w-full max-w-xs rounded-none bg-[var(--block-accent)] text-[var(--block-accent-fg)] shadow-none hover:bg-[var(--block-accent)] hover:underline hover:underline-offset-4\"\n              >\n                {status === \"submitting\" ? (\n                  <>\n                    <Loader2 aria-hidden=\"true\" className=\"animate-spin motion-reduce:animate-none\" />\n                    Checking code…\n                  </>\n                ) : (\n                  \"Verify\"\n                )}\n              </Button>\n            </form>\n\n            <div className=\"mt-10 flex w-full flex-wrap items-center justify-center gap-x-6 gap-y-3 border-t pt-6 text-sm\">\n              <button\n                type=\"button\"\n                onClick={handleResend}\n                disabled={cooldown > 0 || resending}\n                className=\"rounded-sm font-medium underline decoration-1 underline-offset-[6px] outline-none hover:decoration-2 focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-not-allowed disabled:text-muted-foreground disabled:no-underline\"\n              >\n                {resending ? (\n                  \"Sending…\"\n                ) : cooldown > 0 ? (\n                  <>\n                    Resend code in{\" \"}\n                    <span className=\"font-mono tabular-nums\">{formatSeconds(cooldown)}</span>\n                  </>\n                ) : (\n                  \"Resend code\"\n                )}\n              </button>\n              <a\n                href={changeEmailHref}\n                className=\"text-muted-foreground hover:text-foreground rounded-sm underline-offset-[6px] outline-none hover:underline focus-visible:ring-2 focus-visible:ring-ring\"\n              >\n                Use a different email\n              </a>\n            </div>\n\n            <p className=\"text-muted-foreground mt-6 max-w-xs font-mono text-[11px] leading-relaxed\">\n              <span aria-hidden=\"true\">* </span>\n              {footnote}\n            </p>\n          </div>\n        )}\n\n        <p role=\"status\" aria-live=\"polite\" className=\"sr-only\">\n          {announcement}\n        </p>\n      </div>\n    </section>\n  );\n}\n"
    }
  ],
  "dependencies": [
    "input-otp",
    "lucide-react"
  ],
  "registryDependencies": [
    "button",
    "input-otp",
    "label"
  ]
}
