No results found

One-Time Password Input

Securely collect and validate numeric codes with automatic field navigation and keyboard support.

Information Circle

Two-step verification

A verification code has been sent to your email. Please enter the code below to verify this device.

Features

  • One-Time Password (OTP) input pattern
  • Auto-advance through fields when typing
  • Keyboard navigation with arrow keys
  • Selection management across multiple fields
  • Backspace navigation between fields
  • Input validation with custom patterns
  • Password manager compatibility
  • Focus and caret indication
  • Custom numeric input mode
  • Range selection support
  • Completion event handling
  • Value binding and control
  • Reactive disabled state
  • Individual item customization
  • Visual highlight state management

Anatomy

PartDescription
<Otp.Root>Here's a comment for you!
<Otp.Item>Individual item component for displaying a single OTP digit
<Otp.HiddenInput>Hidden input component that handles OTP input interactions and validation
<Otp.Caret>Component that renders a caret for OTP input focus indication

Examples

Basic Usage

The OTP input can be initialized with a default value using the value prop.

1
2
3
4

This example demonstrates:

Visual Features

The OTP input can be disabled to prevent user interaction.

This example shows:

Advanced Usage

Controlled Input

The OTP input can be controlled using signals with bind:value.

This example demonstrates:

Event Handling

The OTP input supports completion and change events.

This example shows:

Change Events

Monitor input changes with the onChange$ handler.

This example demonstrates:

Component State

Using Component State

The OTP component provides several ways to control its state through props:

  1. Initial Value As shown in the initial example above, you can set an initial uncontrolled value using the value prop on <Otp.Root />:
<Otp.Root value="1234">
  {/* OTP items */}
</Otp.Root>
  1. Controlled Value For controlled state management, use the bind:value prop as demonstrated in the reactive example above. This allows you to programmatically update the OTP value.
  2. Disabled State The disabled example shows how to disable the entire OTP input using the disabled prop:
<Otp.Root disabled={true}>
  {/* OTP items */}
</Otp.Root>

State Interactions

  1. Change Events As shown in the change example, use the onChange$ prop to listen for value changes:
<Otp.Root 
  onChange$={(value) => {
    // Handle value change
  }}
>
  {/* OTP items */}
</Otp.Root>
  1. Completion Events The complete example demonstrates using onComplete$ to handle when all OTP digits are filled:
<Otp.Root
  onComplete$={() => {
    // Handle OTP completion
  }}
>
  {/* OTP items */}
</Otp.Root>
  1. Password Manager Integration Control password manager suggestion positioning with the shiftPWManagers prop (enabled by default):
<Otp.Root shiftPWManagers={false}>
  {/* OTP items */}
</Otp.Root>

The component maintains internal state for:

Based on the provided examples and API documentation, I'll document the configuration options for the OTP component.

Core Configuration

Input Pattern

By default, the OTP component only accepts numeric input (0-9). You can customize the input validation pattern through the pattern prop on <Otp.HiddenInput />. The default pattern is ^[0-9]*$.

Initial Value

The OTP component supports setting an initial uncontrolled value. As shown in the initial example above, you can pass the value prop to <Otp.Root /> to set the initial value.

Password Manager Integration

The OTP component includes built-in support for password managers. By default, password manager suggestions are shifted to the right of the OTP input. This behavior can be controlled via the shiftPWManagers prop on <Otp.Root />:

<Otp.Root shiftPWManagers={false}>
  {/* ... */}
</Otp.Root>

Input Length

The number of OTP digits is determined by the number of <Otp.Item /> components rendered. The component automatically manages the maximum input length based on this count.

Advanced Configuration

Value Binding

For reactive control over the OTP value, the component supports value binding through the bind:value prop. As shown in the reactive example above, this allows two-way binding with a signal.

Completion Handling

The component provides completion detection through the onComplete$ prop. As demonstrated in the complete example above, this handler is called when all OTP digits are filled.

Change Detection

For granular control over value changes, use the onChange$ prop. As shown in the change example above, this handler is called on every value change, receiving the current OTP value as an argument.

Disabled State

The entire OTP input can be disabled through the disabled prop on <Otp.Root />. As shown in the disabled example above, this affects all child components and prevents user interaction.

Note: The disabled state is reflected through the data-disabled attribute on both root and individual items, allowing for consistent styling across states.

API Reference

Otp Root

Inherits from: <div />

Props

PropTypeDefaultDescription
"bind:value"Signal<string>-
_numItemsnumber-
autoCompleteHTMLInputAutocompleteAttribute-
onComplete$QRL<() => void>-
onChange$QRL<(value: string) => void>-
valuestring-
disabledbooleanfalse
shiftPWManagersbooleantrue

Data Attributes

AttributeDescription
data-disabledIndicates if the entire OTP input is disabled

Otp Item

Inherits from: <div />

Props

PropTypeDefaultDescription
_indexnumber0
indexnumber-

Data Attributes

AttributeDescription
data-highlightedIndicates if the OTP item is currently highlighted
data-disabledIndicates if the OTP item is disabled

Otp Hidden Input

Inherits from: <input />

Props

PropTypeDefaultDescription
patternstring | null-

Data Attributes

AttributeDescription
data-shiftIndicates whether password manager suggestions should be shifted

Otp Caret

Inherits from: <span />

Accessibility

Keyboard Interactions

KeyDescription
ArrowLeftWhen focus is on the hidden input, moves focus to the previous OTP item. Prevents skipping over filled slots when moving from empty slots
ArrowRightWhen focus is on the hidden input, moves focus to the next OTP item
BackspaceWhen focus is on the hidden input, deletes the previous character and moves focus backwards
0-9When focus is on the hidden input, enters a numeric digit and moves focus to the next item
ShiftWhen held down, allows range selection of multiple OTP items using arrow keys
TabMoves focus into and out of the OTP input component
Any non-numericWhen focus is on the hidden input, input is prevented if it doesn't match the pattern (defaults to numbers only)