Skip to content

React OTP Input

A React OTP input with one slot per digit, paste and SMS autofill, and Motion feedback: digits pop in, errors shake and success ripples.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/otp-input.json

Verify your number

We sent a code to the phone ending in 47.

Basic

Enter the code we sent you.

Files to copy

5 folders · 7 files · 4 packages
From theme/src/
  • components/otp-input/
  • hooks/{use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, form.ts, state-light.tsx}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/otp-input.jsonRegistry item: otp-input. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Status feedback

The correct code is 123456. Anything else shakes the field.

Success effect

Type any 6 digits. "seal" (the default) scans the slots with light, folds them into one glass seal and draws a check; "ring" ripples a ring round each slot; "none" only recolours them. Reduced motion fades the seal in and skips the rest.

Masked, 4 digits

Props

PropTypeDefaultDescription
onBlurFocusEventHandler<HTMLElement>NoneCalled when focus leaves the control (not when it moves between the control's own parts).
namestringNoneForm field name (the joined code is submitted under it).
requiredbooleanNoneMark the field as required for form validation.
disabledbooleanNoneDisables every digit slot of the field.
aria-invalidboolean | "true" | "false"NoneMarks the field as invalid (also set automatically when error is shown).
aria-describedbystringNoneIds of elements that describe the field, merged with the control's own description and error ids.
size"sm" | "md" | "lg""md"Slot size, in step with the other controls: "sm", "md" (default) or "lg".
lengthnumber6Number of digits the code has.
valuestringNoneSets the code so far when controlled. Digits only.
defaultValuestringNoneSets the starting code when uncontrolled.
onValueChange((value: string) => void)NoneCalled on every change with the digits entered so far.
onComplete((value: string) => void)NoneFires once every slot is filled.
statusOtpStatus"idle""error" shakes the field and glows red; "success" plays successEffect. Set back to "idle" before re-triggering.
successEffectOtpSuccessEffect"seal"What "success" plays: "seal" (the slots fold into one glass seal with a drawn check), "ring" (a ring ripples round each slot) or "none" (colour only). Reduced motion fades the seal in and skips the rest.
stateLightbooleantrueLights the row by status: a red aura and lit edge for "error", green for "success" (data-state on the row, drawn on its own layer). The status is always written as text and border colour too.
labelReactNodeNoneVisible text above the slots; it names the first slot.
labelsPartial<OtpInputLabels>defaultOtpInputLabelsEvery string, for translation: { name: "Verification code" } (the default accessible name). Every other string, for translation: labels={{ name: "…" }}; omitted keys keep the English of defaultOtpInputLabels. Its only key is listed in the key table under the props table.
aria-labelstringlabels?.name ?? defaultOtpInputLabels.nameAccessible name when no visible label is shown.
aria-labelledbystringNoneId of an element that names the field (instead of label / aria-label).
descriptionReactNodeNoneMuted helper text under the slots.
errorMessageReactNodeNoneShown and announced while status is "error".
successMessageReactNodeNoneShown and announced while status is "success".
digitLabel((position: number, length: number) => string)(position, total) => `Digit ${position} of ${total}`Accessible name of slots 2…n. Translate it with the rest of your UI.
groupedbooleanfalseVisually split the slots into two groups (e.g. 3 + 3).
maskbooleanfalseHide the digits like a password field.
autoFocusbooleanNoneFocus the first slot on mount. Use it only when the code is the one thing on the screen.
idstringgeneratedId of the first slot; a generated one is used when omitted.
classNamestringNoneClass for the outer wrapper (label + slots + messages).
OtpInputLabels keys and English defaults (1)
Pass any key in labels; the rest keep the defaults of defaultOtpInputLabels.
KeyEnglish default
nameVerification code

Keyboard

KeysAction
TabFocus the first empty slot.
0-9 (or letters, for alphanumeric)Fill the slot and advance.
BackspaceClear the slot and move back.
Arrow Left/Arrow RightMove between slots.
PasteFill every slot at once.

Accessibility

  • The first slot is named by the label and the others "Digit n of N"; autoComplete="one-time-code" lets phones suggest the code.
  • The error and success messages sit in a polite live region; the seal and its sparks are decorative (aria-hidden), so the announcement is the same for every successEffect.
  • Slots are at least 44 px wide and 48 px tall with a 3:1 border (WCAG 1.4.11).

All exports on this page

  • OtpInput — One-time code input: a row of single-digit slots for SMS, email and authenticator codes.
  • defaultOtpInputLabels — The English OtpInputLabels used when labels is omitted; a partial labels prop is merged over it.

When to use the OTP input

  • Use OtpInput as a verification code input in React on a two-factor sign-in or phone verification screen, where an SMS or authenticator code of fixed length arrives and people type or paste it. Treat it as an input-otp alternative when you want styled, labeled code slots instead of a headless primitive.
  • Use onComplete to verify the code the moment the last digit lands, and set status to error or success to shake the field or play a soft ripple with a message.
  • Use grouped for a six digit code shown as 3 + 3, or mask when the code is sensitive and should not be readable over someone's shoulder.
  • Skip it when the value is not a fixed-length numeric code. Use Input for an email or a recovery phrase, including a password with a show button in its trailing slot.
  • Pair it with Button for a resend action, Alert for a rate-limit message, and the OTP verification block, which already assembles these pieces on one screen.

SheenUI OtpInput or input-otp?

input-otp is a headless one-time-code library. SheenUI's OtpInput is built on Base UI's OTP field and arrives styled: glass slots, paste and SMS autofill, grouped and mask options, and error and success feedback through the status prop. Use input-otp if you want to draw every slot yourself; use this one if you want a finished, accessible field to copy.

Frequently asked questions

67 components pages in all: browse them on All components.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .