OTP Input

Code entry with a gliding focus ring and digits that roll in.

Preview

"use client";

import { useState } from "react";
import { OTPInput, type OTPStatus } from "@/components/motion/otp-input";

const CODE = "123456";

export function OTPInputPreview() {
  const [value, setValue] = useState("");
  const [status, setStatus] = useState<OTPStatus>("idle");

  return (
    <div className="flex flex-col items-center gap-4">
      <OTPInput
        label="Verification code"
        hint={`Enter ${CODE} to verify.`}
        successMessage="Verified."
        errorMessage="Wrong code, try again."
        value={value}
        status={status}
        onChange={(v) => {
          setValue(v);
          if (status !== "idle") setStatus("idle");
        }}
        onComplete={(v) => setStatus(v === CODE ? "success" : "error")}
      />
    </div>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/otp-input

API Reference

length?
number

Number of slots. Default 6.

Default6
value?
string

The entered digits, for controlled use.

defaultValue?
string

The initial digits when uncontrolled. Default: ""

onChange?
((value: string) => void)

Called with the entered digits whenever a slot changes.

onComplete?
((value: string) => void)

Fires once every slot is filled.

label?
string

Optional label rendered above the slots.

hint?
string

Helper text shown below the slots while idle.

successMessage?
string

Message shown below the slots when status is "success".

errorMessage?
string

Message shown below the slots when status is "error".

status?
"error" | "success" | "idle"

External validation feedback. "error" shakes, "success" draws a check.

Defaultidle
mask?
boolean

Render dots instead of the typed digits.

Defaultfalse
disabled?
boolean

Block typing and dim the slots. Default: false

Defaultfalse
autoFocus?
boolean

Focus the input on mount. Default: false

Defaultfalse
aria-label?
string

Accessible label for the underlying input.

labels?
Partial<{ label: string; progress: string; entered: string; maskedDigit: string; deleted: string; position: string; filled: string; empty: string; verified: string; }>

Override any built-in string.

className?
string

Classes merged onto the root element.

Also on React Native. Digits roll into slots, with SMS autofill.

Keep in mind

Some components on this site are inspired by or recreated from existing work across the web. I'm not here to take credit; just to learn, experiment, and sometimes push things a bit further. If something looks familiar and I forgot to mention you, reach out and I'll fix that right away.

Updated