Input OTP
Takes a short one-time code, such as a six-digit verification code, in separate boxes that fill as you type or paste.
Enter the code we sent to giulia@brevia.example.
Installation
pnpm dlx shadcn@latest add @silkware/input-otpTo copy the component by hand, install its dependencies:
pnpm add input-otp cn lucide-reactThen copy the source into components/ui/input-otp.tsx:
"use client"
import * as React from "react"import { cn } from "cn"import { OTPInput, OTPInputContext } from "@animate-ui/react/primitives/input-otp"import { MinusIcon } from "lucide-react"
function InputOTP({ className, containerClassName, ...props}: React.ComponentProps<typeof OTPInput> & { containerClassName?: string}) { return ( <OTPInput data-slot="input-otp" containerClassName={cn( "cn-input-otp flex items-center has-disabled:opacity-50", containerClassName )} spellCheck={false} className={cn("disabled:cursor-not-allowed", className)} {...props} /> )}
function InputOTPGroup({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="input-otp-group" className={cn( "flex items-center rounded-md has-aria-invalid:border-destructive has-aria-invalid:ring-3 has-aria-invalid:ring-destructive/20 dark:has-aria-invalid:ring-destructive/40", className )} {...props} /> )}
function InputOTPSlot({ index, className, ...props}: React.ComponentProps<"div"> & { index: number}) { const inputOTPContext = React.useContext(OTPInputContext) const { char, hasFakeCaret, isActive } = inputOTPContext?.slots[index] ?? {}
return ( <div data-slot="input-otp-slot" data-active={isActive} className={cn( "relative flex size-9 items-center justify-center border-y border-r border-input text-sm shadow-xs transition-all outline-none first:rounded-l-md first:border-l last:rounded-r-md aria-invalid:border-destructive data-[active=true]:z-10 data-[active=true]:border-ring data-[active=true]:ring-3 data-[active=true]:ring-ring/50 data-[active=true]:aria-invalid:border-destructive data-[active=true]:aria-invalid:ring-destructive/20 dark:bg-input/30 dark:data-[active=true]:aria-invalid:ring-destructive/40", className )} {...props} > {char} {hasFakeCaret && ( <div className="pointer-events-none absolute inset-0 flex items-center justify-center"> <div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" /> </div> )} </div> )}
function InputOTPSeparator({ ...props }: React.ComponentProps<"div">) { return ( <div data-slot="input-otp-separator" className="flex items-center [&_svg:not([class*='size-'])]:size-4" role="separator" {...props} > <MinusIcon /> </div> )}
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator }Not a React Aria component
Input OTP is built on the input-otp package. React Aria has no one-time code
field, so the props below are those of that package, not React Aria props. The
page links to input-otp as its primitive.
Usage
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot,} from "@/components/ui/input-otp"<InputOTP maxLength={6} aria-label="Verification code"> <InputOTPGroup> <InputOTPSlot index={0} /> <InputOTPSlot index={1} /> <InputOTPSlot index={2} /> </InputOTPGroup> <InputOTPSeparator /> <InputOTPGroup> <InputOTPSlot index={3} /> <InputOTPSlot index={4} /> <InputOTPSlot index={5} /> </InputOTPGroup></InputOTP>There is one real <input> behind the boxes, so paste, autofill from an SMS and the password manager all work. Draw exactly maxLength slots, numbered from 0. The index of each slot picks the character it shows.
Examples
Single group
One group of slots, with no separator. Use it for short codes.
Four digits
Set maxLength to the length of the code. The input asks touch screens for the number pad by default.
Pattern
pattern restricts the characters the input accepts. Set inputMode="text" when the code can hold letters, because the default is numeric. The package exports REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS and REGEXP_ONLY_DIGITS_AND_CHARS, or pass your own regular expression.
Letters and digits only.
Controlled
Use value and onChange to hold the code in state. onComplete runs once when all the slots are full, which is the moment to verify the code.
Enter the six-digit approval code.
Disabled
disabled stops input and dims the slots.
Invalid
Set aria-invalid on InputOTP and on each slot. The slots draw the destructive border, and the input tells assistive technology that the value is wrong. Explain the problem in a FieldError.
In a form with validation
Clear the error when the person edits the code, and allow submit only when every slot is full. This form accepts one code so you can try both outcomes.
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus to the code input, and away from it. |
| 0-9 | Types a character in the active slot and moves to the next one. |
| Backspace | Deletes the character before the cursor and moves back one slot. |
| Left | Moves the active slot one place back. |
| Right | Moves the active slot one place forward. |
The input-otp package provides:
- A single native input behind the slots, so copy, paste, autofill and the browser's code suggestions work.
autocomplete="one-time-code"andinputMode="numeric"by default, so browsers can offer a code received by SMS and touch screens show the number pad.- A caret that blinks in the active slot, drawn as a fake caret.
You provide:
- A label: a
FieldLabelthat matches theidonInputOTP, or anaria-label. inputMode="text"when the code holds letters, and apatternwhen some characters are not allowed.aria-invalidand a visible, specific error message.- A way to ask for a new code.
Guidelines
When to use
- For a short code that the person receives elsewhere: a verification code, an approval code, a site PIN.
- When the length is fixed and known: four to eight characters.
When not to use
- For identifiers that people choose or look up, such as an order number. Use Input.
- For long strings. A row of boxes does not fit narrow screens.
- For a password. Use an input with
type="password".
Content
- Say where the code was sent: "Enter the code we sent to giulia@brevia.example."
- Group the boxes in threes or twos to match how the code is read aloud.
- Error text says what to do: "That code is wrong or has expired. Request a new one."
- Offer a way to request a new code next to the field.
Length
Keep to six characters or fewer, split into groups.
Don’t use boxes for a long code or an identifier. Use an input.
API reference
InputOTP
Renders the OTPInput of the input-otp package. It accepts the props of that package and the attributes of an HTML input, such as id, aria-label, disabled, inputMode and autoComplete. Those attributes go on the hidden input.
| Prop | Type | Default | Description |
|---|---|---|---|
maxLength | number | None | The number of characters in the code. Draw the same number of slots. |
value? | string | None | The code, for a controlled input. |
onChange? | (value: string) => void | None | Called each time the code changes. |
onComplete? | (value: string) => void | None | Called when the code reaches maxLength. |
pattern? | string | None | A regular expression source that each character must match, such as REGEXP_ONLY_DIGITS. |
pasteTransformer? | (pasted: string) => string | None | Cleans pasted text before it is used, for example to remove spaces and dashes. |
textAlign? | "left" | "center" | "right" | "left" | The alignment of the hidden input text. |
inputMode? | string | "numeric" | The virtual keyboard to request. Use text for codes with letters. |
disabled? | boolean | false | Stops input and dims the slots. |
containerClassName? | string | None | Classes for the element that wraps the slots. |
className? | string | None | Classes for the hidden input. |
InputOTPGroup
Renders a div that joins slots into one bordered group. It accepts every div prop.
InputOTPSlot
Renders one box of the code. It shows the character at its index, the active ring and the fake caret. It accepts every div prop and the prop below. Set aria-invalid on it for the error style.
| Prop | Type | Default | Description |
|---|---|---|---|
index | number | None | The position of the character this slot shows, starting at 0. |
InputOTPSeparator
Renders a dash with role="separator" between two groups. It accepts every div prop.