Skip to content

OTP input

Accepts a short code with one box for each character.

Enter the code we sent to your phone.
otp-input-demo.tsx

Installation

The CLI adds the files, packages, theme variables, and Tailwind plugin that this component uses. Before you run it, set up your project.

Usage

verification-code.tsx

Composition

  • OtpInput

    Owns the value. Filters input with pattern and calls onComplete when the code is full.

    • OtpInputLabel
    • OtpInputSlots

      Without children, it draws one slot for each character.

      • OtpInputGroupOptionalRepeats
        • OtpInputSlotRepeats
      • OtpInputSeparatorOptional
    • OtpInputDescriptionOptional
    • OtpInputErrorOptional

OtpInputContext Renders a function with the pin-input api. useOtpInput returns the api and the value for a custom row.

Groups

Use OtpInputGroup and OtpInputSeparator to split the code.

otp-input-groups-demo.tsx

Invalid

Also install Button.

Enter the 6-digit code from your app.
otp-input-invalid-demo.tsx

Letters and numbers

Set pattern to "alphanumeric" for recovery codes.

Letters and numbers only.
otp-input-alphanumeric-demo.tsx

Length

Enter the 4-digit PIN for your card.
otp-input-length-demo.tsx

Sizes

otp-input-sizes-demo.tsx

Disabled

This phone number is managed by your organization.
otp-input-disabled-demo.tsx

Clear button

OtpInputContext gives the machine to custom UI, such as a clear button. Also install Button.

Sent to ••• 0134.
otp-input-context-demo.tsx

API reference

vip/ui adds or changes the props below. For all other props, see React Aria TextField (opens in a new tab).

OtpInput

length: number = 6
Sets the number of characters and boxes.
pattern: "digits" | "alphanumeric" = "digits"
Keeps digits only, or letters and numbers. Letters are uppercased.
size: "sm" | "md" | "lg" = "lg"
Sets the height of each box: 36px, 44px, or 48px.
value: string
The code value, controlled.
defaultValue: string
The code value at first, uncontrolled.
placeholder: string
The placeholder shown in each empty box.
isDisabled: boolean
Disables every box and the hidden input.
isReadOnly: boolean
Keeps the value but stops changes.
isRequired: boolean
Requires a value before the owning form submits.
isInvalid: boolean
Marks the boxes and the error message as invalid.
children: ReactNode
Draws a custom label, control, description, or error. Without it, the field draws the label, one row of boxes, and the hidden input.
getRootNode: () => ShadowRoot | Document | Node
Resolves the document in custom environments such as iframes.
onComplete: (value: string) => void
Called once each time the value reaches the full length.
onChange: (value: string) => void
Called with the filtered value after each change.
name: string
Sets the form field name on the hidden input.
form: string
Associates the hidden input with a form.
autoFocus: boolean
Focuses the first box on mount.
autoSubmit: boolean
Submits the owning form when the code is full.
blurOnComplete: boolean
Blurs the boxes when the code is full.
mask: boolean
Masks each box like a password input.
selectOnFocus: boolean = true
Selects the value of a box when it receives focus.
otp: boolean = true
Signals autocomplete="one-time-code" to the browser.
sanitizeValue: (value: string) => string
Strips formatting before the pattern filter runs, such as dashes from a pasted code.
onValueInvalid: (details: { value: string; index: number }) => void
Called when a character does not match the pattern.
translations: { inputLabel?: (index: number, length: number) => string }
Localizes the accessible label of each box.
dir: "ltr" | "rtl"
Sets the writing direction. By default, it comes from the page.
ids: ElementIds
Sets the ids of the label, control, and boxes.

OtpInputSlots

Draws the row of boxes. Without children, it draws one box for each character.

children: ReactNode
Draws a custom run of boxes with `OtpInputGroup`, `OtpInputSlot`, and `OtpInputSeparator`.

OtpInputContext

Renders no DOM. Its children receive the pin-input api, including value, valueAsString, complete, setValue, clearValue, focus, and the part props.

children: (api: PinInputApi) => ReactNodeRequired
Draws custom content inside OtpInput.

OtpInputSlot

Renders one input. It takes the typing, paste, autofill, and arrow keys for its position.

index: numberRequired
Sets the character position that the input shows.

OtpInputGroup

Groups boxes in a run.

OtpInputSeparator

Shows a short dash between groups.

OtpInputLabel

Names the field. A press on the label focuses the input.

OtpInputDescription

Shows help text below the boxes.

OtpInputError

Shows the error below the boxes and announces it once when it appears.