PeakUI

InputOtp

One-time password input for verification codes

Usage

import {InputOtp} from "@peakui/react";

Anatomy

import {InputOtp} from "@peakui/react";

export default () => (
  <InputOtp maxLength={6}>
    <InputOtp.Group>
      <InputOtp.Slot index={0} />
      <InputOtp.Slot index={1} />
      {/* ...rest of the slots */}
    </InputOtp.Group>
    <InputOtp.Separator />
    <InputOtp.Group>
      <InputOtp.Slot index={3} />
      {/* ...rest of the slots */}
    </InputOtp.Group>
  </InputOtp>
);

InputOtp is built on the input-otp package, which owns the paste handling, the fake caret and the slot state; the parts only read that state and render it.

Examples

Variants

variant="primary" (default) carries the field shadow; variant="secondary" is a flat fill for surfaces.

In Surface

Use variant="secondary" inside a Surface.

Disabled State

Four Digits

maxLength decides the number of slots.

Controlled

onChange receives the value as a string; keep your own state to clear or validate it.

On Complete

onComplete fires when all slots are filled.

Form Example

A complete two-factor authentication form with validation and submission.

With Pattern

pattern restricts input; REGEXP_ONLY_CHARS and REGEXP_ONLY_DIGITS are exported for it.

With Validation

isInvalid together with validationErrors makes a FieldError inside InputOtp render.

Customization

Tailwind CSS

Global CSS

@layer components {
  .input-otp {
    @apply gap-3;
  }

  .input-otp__slot {
    @apply size-12 rounded-xl border-2 font-bold;
  }

  .input-otp__slot[data-active="true"] {
    @apply border-accent ring-2 ring-accent;
  }

  .input-otp__separator {
    @apply w-2 h-1 bg-separator rounded-full;
  }
}

Styling Reference

PeakUI follows the BEM methodology, so variants and states are plain class names you can target.

CSS Classes

The InputOtp component uses these CSS classes (view source styles):

Base Classes

  • .input-otp - The visible container that wraps the groups
  • .input-otp__group - A group of slots
  • .input-otp__slot - An individual slot box
  • .input-otp__slot-value - The character inside a slot
  • .input-otp__caret - The blinking fake caret
  • .input-otp__separator - The separator between groups
  • .input-otp__input - The hidden <input> that receives typing

Variant Classes

  • .input-otp--primary - Default variant with the field shadow
  • .input-otp--secondary - Flat fill for surfaces, without shadow

State Classes

  • .input-otp__slot[data-active="true"] - The slot the caret is in
  • .input-otp__slot[data-filled="true"] - A slot holding a character
  • .input-otp__slot[data-disabled="true"] - A slot of a disabled input
  • .input-otp__slot[data-invalid="true"] - A slot of an invalid input

Slots

  • [data-slot="input-otp"] - The hidden <input> that owns value, focus and keyboard input
  • [data-input-otp-container="true"] - The visible container; carries .input-otp and the variant class
  • [data-slot="input-otp-group"]
  • [data-slot="input-otp-slot"]
  • [data-slot="input-otp-slot-value"]
  • [data-slot="input-otp-caret"]
  • [data-slot="input-otp-separator"]

CSS Variables

VariablePurpose
--input-otp-slot-bgSlot background in the secondary variant
--input-otp-slot-bg-hoverSlot background in the secondary variant when hovered
--input-otp-slot-bg-focusSlot background in the secondary variant when active or filled

Interactive States

  • Hover: the slot's :hover or [data-hovered="true"]
  • Active: the slot's [data-active="true"] — the slot the caret is in
  • Filled: the slot's [data-filled="true"]
  • Disabled: the hidden input's or slot's [data-disabled="true"]
  • Invalid: the input's or slot's [data-invalid="true"]

API Reference

InputOtp

Built on the input-otp package, with validation state and field styling added.

Base Props

PropTypeDefaultDescription
maxLengthnumber-Required. Number of input slots.
valuestring-The controlled value.
defaultValuestring-The initial value when uncontrolled.
onChange(value: string) => void-Handler called when the value changes.
onComplete(value: string) => void-Handler called when all slots are filled.
classNamestring-Classes for the visible container.
inputClassNamestring-Classes for the hidden <input> element.
variant"primary" | "secondary""primary"Visual variant. primary carries the field shadow; secondary is a flat fill for surfaces.
childrenReact.ReactNode-InputOtp.Group, InputOtp.Slot and InputOtp.Separator parts.

Validation Props

PropTypeDefaultDescription
isDisabledbooleanfalseWhether the input is disabled.
isInvalidbooleanfalseWhether the input is invalid; also marks every slot.
validationErrorsstring[]-Validation messages, shown by a FieldError inside InputOtp.
validationDetailsValidityState-HTML5 validation details.

Input Props

PropTypeDefaultDescription
patternstring-Regex pattern for allowed characters, e.g. REGEXP_ONLY_DIGITS.
textAlign'left' | 'center' | 'right''left'Text alignment within the input.
inputMode'numeric' | 'text' | 'decimal' | 'tel' | 'search' | 'email' | 'url''numeric'Virtual keyboard type on mobile devices.
placeholderstring-Placeholder character shown in empty slots.
pasteTransformer(text: string) => string-Transforms pasted text, e.g. to remove hyphens.

Form Props

PropTypeDefaultDescription
namestring-Name attribute for form submission.
autoFocusboolean-Whether to focus the input on mount.
...restEvery <input> attribute is accepted

InputOtp.Group

PropTypeDefaultDescription
childrenReact.ReactNode-InputOtp.Slot components.
classNamestring-Additional classes for the group.
...restEvery <div> attribute is accepted

InputOtp.Slot

PropTypeDefaultDescription
indexnumber-Required. Zero-based index of the slot.
classNamestring-Additional classes for the slot.
...restEvery <div> attribute is accepted

InputOtp.Separator

PropTypeDefaultDescription
classNamestring-Additional classes for the separator.
...restEvery <div> attribute is accepted

Exported Patterns

import {REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS, REGEXP_ONLY_DIGITS_AND_CHARS} from "@peakui/react";

// Use with the pattern prop
<InputOtp pattern={REGEXP_ONLY_DIGITS} maxLength={6}>
  {/* ... */}
</InputOtp>
  • REGEXP_ONLY_DIGITS - Only numeric characters (0-9)
  • REGEXP_ONLY_CHARS - Only alphabetic characters (a-z, A-Z)
  • REGEXP_ONLY_DIGITS_AND_CHARS - Alphanumeric characters (0-9, a-z, A-Z)

On this page