OTP verification
Six-digit verification code card with auto-advancing numeric inputs, clipboard paste support, arrow navigation and resend control.
pnpm dlx shadcn@latest add @haze/otp-verification---
import { CheckCircle2, KeyRound, RotateCw } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface Props {
id?: string;
email?: string;
class?: string;
}
const { id = "haze-otp-verification", email = "nina@tidewell.dev", class: className } = Astro.props;
const slots = [0, 1, 2, 3, 4, 5];
---
<section aria-labelledby={`${id}-title`} data-haze-otp-block class={cn("bg-background px-4 py-10 sm:px-6 sm:py-14", className)}>
<div class="mx-auto max-w-md rounded-2xl border border-border bg-card p-6 text-center sm:p-8">
<span class="mx-auto grid size-11 place-items-center rounded-xl border border-frame bg-muted text-muted-foreground" aria-hidden="true">
<KeyRound class="size-5" />
</span>
<h2 id={`${id}-title`} class="mt-4 text-xl/7 font-semibold tracking-tight text-foreground">Enter verification code</h2>
<p class="mt-1.5 text-xs/5 text-muted-foreground">We sent a 6-digit code to <strong class="text-foreground">{email}</strong>. Paste or type below.</p>
<form data-otp-form class="mt-6 space-y-5">
<div class="flex justify-center gap-2" role="group" aria-label="6-digit verification code">
{
slots.map((i) => (
<input
type="text"
inputmode="numeric"
maxlength="1"
data-otp-slot={String(i)}
aria-label={`Digit ${i + 1} of 6`}
class="h-12 w-10 rounded-xl border border-frame-input bg-field text-center font-mono text-lg font-semibold text-foreground transition-[color,background-color,border-color,box-shadow] duration-150 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/24 focus-visible:outline-hidden sm:w-11 transition-[color,background-color,border-color,box-shadow] duration-150 hover:border-frame-foreground"
/>
))
}
</div>
<button type="submit" class="inline-flex h-10 w-full cursor-pointer items-center justify-center gap-1.5 rounded-lg bg-primary text-sm font-medium text-primary-foreground hover:opacity-90 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
<CheckCircle2 class="size-4" aria-hidden="true" />
Verify code
</button>
</form>
<div class="mt-4 flex items-center justify-center gap-2 text-xs text-muted-foreground">
<span>Did not receive it?</span>
<button type="button" data-otp-resend class="inline-flex h-7 cursor-pointer items-center gap-1 rounded-md px-2 font-medium text-foreground hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
<RotateCw class="size-3" aria-hidden="true" />
Resend code
</button>
</div>
<p data-otp-status role="status" class="mt-3 min-h-4 text-xs/4 text-muted-foreground"></p>
</div>
</section>
<script>
function initOtpBlocks() {
document.querySelectorAll<HTMLElement>("[data-haze-otp-block]").forEach((root) => {
if (root.hasAttribute("data-enhanced")) return;
root.setAttribute("data-enhanced", "");
const inputs = Array.from(root.querySelectorAll<HTMLInputElement>("[data-otp-slot]"));
const status = root.querySelector<HTMLElement>("[data-otp-status]");
inputs.forEach((inp, i) => {
inp.addEventListener("input", () => {
inp.value = inp.value.replace(/\D/g, "").slice(0, 1);
if (inp.value && i < inputs.length - 1) inputs[i + 1].focus();
});
inp.addEventListener("keydown", (ev) => {
if (ev.key === "Backspace" && !inp.value && i > 0) inputs[i - 1].focus();
if (ev.key === "ArrowLeft" && i > 0) inputs[i - 1].focus();
if (ev.key === "ArrowRight" && i < inputs.length - 1) inputs[i + 1].focus();
});
inp.addEventListener("paste", (ev) => {
const digits = (ev.clipboardData?.getData("text") ?? "").replace(/\D/g, "").slice(0, 6);
if (!digits) return;
ev.preventDefault();
digits.split("").forEach((d, idx) => { if (inputs[idx]) inputs[idx].value = d; });
inputs[Math.min(digits.length, inputs.length - 1)].focus();
});
});
root.querySelector<HTMLFormElement>("[data-otp-form]")?.addEventListener("submit", (ev) => {
ev.preventDefault();
const code = inputs.map((i) => i.value).join("");
if (status) status.textContent = code.length === 6 ? `Verified code ${code}.` : "Enter all 6 digits to verify.";
});
root.querySelector<HTMLButtonElement>("[data-otp-resend]")?.addEventListener("click", () => {
inputs.forEach((i) => (i.value = ""));
inputs[0]?.focus();
if (status) status.textContent = "Sent a fresh 6-digit verification code.";
});
});
}
initOtpBlocks();
document.addEventListener("astro:page-load", initOtpBlocks);
</script>When to use
- Building a standalone 6-digit email or SMS verification screen during sign-in
- Supporting full-code clipboard paste and Backspace/Arrow navigation across digit slots
When not to use
- You only need the bare OTP input primitive inside a larger custom form; use otp-input
Installation
Add the Haze registry to components.json once, then install OTP verification with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/otp-verificationnpx shadcn@latest add @haze/otp-verificationyarn dlx shadcn@latest add @haze/otp-verificationbunx --bun shadcn@latest add @haze/otp-verificationFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | "haze-otp-verification" | Unique prefix for section heading IDs. |
| string | "nina@tidewell.dev" | Masked or full destination address shown under the heading. | |
| class | string | – | Extra classes on the outer section. |
Keyboard interactions
- 0-9Backspace
- Enter digits with automatic focus advance and retreat
- ArrowLeftArrowRight
- Move directly between adjacent digit slots
Accessibility
- Each numeric input carries an explicit aria-label (Digit 1 of 6 through Digit 6 of 6) inside a labelled group.
- Verification and resend actions announce feedback via a polite role="status" message.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI block "OTP verification" with `npx shadcn@latest add @haze/otp-verification`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import OtpVerification from "@/components/haze/blocks/otp-verification.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/blocks/otp-verification.md
Customize with real content
Adapt the Haze otp-verification block for our product. Replace the demo values of these props with our real content: id, email, class. Keep the layout, the heading level rules and these accessibility details: Each numeric input carries an explicit aria-label (Digit 1 of 6 through Digit 6 of 6) inside a labelled group. Verification and resend actions announce feedback via a polite role="status" message. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using otp-verification, check it fits: use it when building a standalone 6-digit email or sms verification screen during sign-in; supporting full-code clipboard paste and backspace/arrow navigation across digit slots. Avoid it when you only need the bare otp input primitive inside a larger custom form; use otp-input. Alternatives: @haze/otp-input, @haze/auth-login.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.