VayuUI

OTP Input

A fully accessible input component for one-time passwords and verification codes.

Installation

npx vayu-ui init #one time only
npx vayu-ui add otp-input
npx vayu-ui add -t otp-input #with test case needs

Usage

OTP Input Example

Default (6-digit)

Value:

Grouped with Separator

With Error State

Invalid code. Please try again.

Disabled

<OTPInput maxLength={6}>
  <OTPInput.Group>
    <OTPInput.Slot index={0} />
    <OTPInput.Slot index={1} />
    <OTPInput.Slot index={2} />
  </OTPInput.Group>
  <OTPInput.Separator />
  <OTPInput.Group>
    <OTPInput.Slot index={3} />
    <OTPInput.Slot index={4} />
    <OTPInput.Slot index={5} />
  </OTPInput.Group>
</OTPInput>

Anatomy

<OTPInput>
  <OTPInput.Group>
    <OTPInput.Slot />
  </OTPInput.Group>
  <OTPInput.Separator />
</OTPInput>

Accessibility

  • Keyboard support: Full native keyboard interaction via a hidden input element.
  • ARIA attributes: Uses role="group" and aria-label for grouping, with aria-invalid for error states.
  • Focus behavior: Automatically manages focus state across visual slots when the hidden input is focused.

Screen reader behavior

The component is announced as a single input group with a descriptive label. When focused, screen readers announce it as an editable text field or a group. Character entries are announced as they are typed.

Component Folder Structure

OTPInput/
├── OTPInput.tsx
├── OTPInputGroup.tsx
├── OTPInputSlot.tsx
├── OTPSeparator.tsx
├── index.ts
└── types.ts

Props

OTPInput

PropTypeDescription
valuestringControlled value of the input.
onChange(value: string) => voidCallback triggered when value changes.
maxLengthnumberMaximum number of characters.
onComplete(code: string) => voidCallback triggered when code is fully entered.
labelstringAccessible label for the input group.
hasErrorbooleanWhether the input is in an error state.
errorMessageIdstringID of the element containing the error message.
autoFocusbooleanWhether to focus the input on mount.
disabledbooleanWhether the input is disabled.

OTPInput.Group

PropTypeDescription
childrenReactNodeContent of the group, typically Slots.
classNamestringCustom styles for the group container.

OTPInput.Slot

PropTypeDescription
indexnumberThe index of the character this slot represents.
classNamestringCustom styles for the slot.

OTPInput.Separator

PropTypeDescription
childrenReactNodeCustom separator content (defaults to a dot).
classNamestringCustom styles for the separator.

On this page