OTP Input React component for Tailwind CSS v4
v1.0.1Updated in 1.2.1
A React OTP input for one-time verification codes and PINs, with digits that roll into place and a caret that slides between slots.
Type to fill each slot and move to the next one. Backspace clears a slot in place, then steps back on the next press. Arrow keys move between slots, and a caret slides along with you. Pasting a code, or letting the phone autofill one from a text message, drops it straight in. Set the status to turn the slots green, or shake them red on a wrong code.
OTP Input is a free React component in the React input components category, written in TypeScript for React 19 and Tailwind CSS v4. It installs as source with npx wingo-ui@latest add otp-input, together with UIProvider and the npm package motion.
Add to a project
Requirements: React 19 and Tailwind CSS v4, in Next.js (App Router or Pages), Vite or React Router. Installation guide · Next.js setup · Vite setup
npx wingo-ui@latest add otp-inputUsage
Import OtpInput from @/components/ui/otp-input.
import { useState } from "react"
import OtpInput, { type OtpStatus } from "@/components/ui/otp-input"
export function Demo() {
const [status, setStatus] = useState<OtpStatus>("idle")
return (
<OtpInput
length={6}
size="md"
status={status}
onChange={() => setStatus("idle")}
onComplete={(code) => setStatus(checkCode(code) ? "success" : "error")}
/>
)
}Props
lengthdefault6numberHow many boxes to render, so a 4 digit code is length={4}. Any count works.
sizedefault"md""sm" | "md" | "lg"Overall scale of the boxes. Maps to 40px (sm, 44px on touch screens), 48px (md), and 56px (lg), and carries the text, caret, and gaps with it. In a container narrower than the row, the boxes shrink evenly and stay square, so 8 slots still fit a 360px phone.
typedefault"numbers""numbers" | "letters" | "both"Which characters a slot accepts. Anything else is ignored, including on paste.
statusdefault"idle""idle" | "success" | "error"Drives the feedback state. Success traces a green ring around each box in turn, error rings them red and shakes the row once.
radiusdefault"md""none" | "sm" | "md" | "lg" | "full"Slot corners as a share of the slot, so they shrink with it and match the success ring: square, half, the original 20 / 25 / 29% per size, rounder, or circles.
surfacedefault""stringAny CSS color for the slot fill. Empty uses #F4F4F9 light, #262626 dark.
textColordefault""stringAny CSS color for the characters and the caret. Empty is black on light pages, white on dark ones.
successColordefault"#34C759"stringAny CSS color for the ring drawn around each slot on success.
errorColordefault"#FF3B30"stringAny CSS color for the ring shown on error.
maskdefaultfalsebooleanHides the characters, like a password field.
disableddefaultfalsebooleanBlocks input and dims every slot.
autoFocusdefaultfalsebooleanFocuses the first slot on mount.
slotLabeldefault""stringAccessible name template for each slot; {index} and {length} fill in, e.g. "Cifra {index} din {length}". Empty reads "Digit 1 of 6" (or "Character" for letters).
valuestringThe current code. Pass it to control the input yourself; leave it out to let the component track its own state.
defaultValuedefault""stringStarting code when the input is uncontrolled.
onChange(value: string) => voidFires on every edit with the full code so far.
onComplete(value: string) => voidFires once the last slot is filled.
classNamestringExtra classes for the row that wraps the slots.
slotClassNamestringExtra classes for each slot, for sizing and colors.
Examples
Verify a phone number
A sign-in step with a controlled value, a Verify button, a resend link and a live message.
Sizes
sm, md and lg. On a narrow screen the slots shrink evenly and stay square.
Success, error and disabled
status rings each slot green or red; error also shakes the row once.
Masked PIN
mask hides the digits, radius full makes round slots, slotLabel names each one for screen readers.
Backup code
type both accepts letters and digits, here in eight small slots.
Files
- components/ui/otp-input.tsx
FAQ
Is the React OTP Input component free?
Yes. The source is free and needs no account: copy it from the Code tab or install it with the CLI.
How do I install OTP Input?
Run npx wingo-ui@latest add otp-input. It also adds the Wingo UI item it builds on (UIProvider) and the npm package motion. With shadcn, run npx shadcn@latest add https://wingo-ui.com/r/otp-input.json.
See also
Used in
More React input components
Articles using this component
- Shadcn Login Page: Magic Link, Codes, 2FA and Recovery
Build it: screens and apps11 min read
- React Form Components: Accessible Fields, Zod and Mobile
Component guides26 min read
- React OTP Input: Paste, SMS Autofill and a Resend Timer
Component guides11 min read