Loading component…
Loading component…
The verification code field done right
To reuse this local React demo, copy all files from the Code tab into the same folder, then import OTPInput from ./otp-input.
import { useState } from "react";
import { OtpInput, type OtpStatus } from "@/components/ui/otp-input";
export function VerifyEmail() {
const [code, setCode] = useState("");
const [status, setStatus] = useState<OtpStatus>("idle");
const [triesLeft, setTriesLeft] = useState(3);
async function verify(value: string) {
setStatus("verifying");
const res = await fetch("/api/verify", { method: "POST", body: JSON.stringify({ code: value }) });
if (res.ok) {
setStatus("success");
router.push("/dashboard");
} else {
setTriesLeft((n) => n - 1);
setStatus("error");
}
}
return (
<OtpInput
value={code}
onChange={(next) => {
setCode(next);
if (status === "error" && next) setStatus("idle");
}}
onComplete={verify}
status={status}
errorMessage={`That code didn't work. ${triesLeft} tries left.`}
separator
autoFocus
onResend={() => fetch("/api/send-code", { method: "POST" })}
resendIn={30}
/>
);
}| Name | Type | Default | Description |
|---|---|---|---|
| length | number | 6 | Number of boxes |
| separator | boolean \| number | false | A small dash between the boxes – true puts it in the middle, a number after that many boxes |
| numeric | boolean | true | Digits only with the numeric keyboard on phones; false allows letters too, shown in capitals |
| value | string | The code – makes the field controlled | |
| defaultValue | string | "" | Starting code when uncontrolled |
| onChange | (code: string) => void | Every change: typing, Backspace, paste, autofill and the clearing after an error | |
| onComplete | (code: string) => void | Called once the last box is filled by the user – the place to start verifying | |
| status | "idle" \| "verifying" \| "error" \| "success" | "idle" | Verifying shimmers and locks the field, error shakes and shows the message, success morphs into the pill |
| errorMessage | ReactNode | "That code didn't work. Try again." | Shown below the boxes after an error, until the user types again |
| clearOnError | boolean | true | Clears the boxes shortly after the shake, ready for the next try |
| successLabel | string | "Verified" | Text in the pill on success |
| onResend | () => void | Shows "Didn't get a code?" below the boxes with a countdown that becomes a Resend button; clicking restarts the countdown | |
| resendIn | number | 30 | Seconds until the code can be resent |
| align | "start" \| "center" | "start" | Where the boxes and the lines below them sit |
| disabled | boolean | false | Dims the field and ignores input |
| autoFocus | boolean | false | Focuses the field on mount (without scrolling) |
| label | string | "Verification code" | Accessible name; the number of digits is added |
| className | string | Extra class on the root |
Container query: the boxes shrink to fit their container – six boxes with a separator fit a 320px phone
Inputs use 16px text under the hood, so iOS doesn't zoom in on focus
Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"
@import "tailwindcss"; in your main CSS file.