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-otpand 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
| Variable | Purpose |
|---|---|
--input-otp-slot-bg | Slot background in the secondary variant |
--input-otp-slot-bg-hover | Slot background in the secondary variant when hovered |
--input-otp-slot-bg-focus | Slot background in the secondary variant when active or filled |
Interactive States
- Hover: the slot's
:hoveror[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
| Prop | Type | Default | Description |
|---|---|---|---|
maxLength | number | - | Required. Number of input slots. |
value | string | - | The controlled value. |
defaultValue | string | - | 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. |
className | string | - | Classes for the visible container. |
inputClassName | string | - | Classes for the hidden <input> element. |
variant | "primary" | "secondary" | "primary" | Visual variant. primary carries the field shadow; secondary is a flat fill for surfaces. |
children | React.ReactNode | - | InputOtp.Group, InputOtp.Slot and InputOtp.Separator parts. |
Validation Props
| Prop | Type | Default | Description |
|---|---|---|---|
isDisabled | boolean | false | Whether the input is disabled. |
isInvalid | boolean | false | Whether the input is invalid; also marks every slot. |
validationErrors | string[] | - | Validation messages, shown by a FieldError inside InputOtp. |
validationDetails | ValidityState | - | HTML5 validation details. |
Input Props
| Prop | Type | Default | Description |
|---|---|---|---|
pattern | string | - | 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. |
placeholder | string | - | Placeholder character shown in empty slots. |
pasteTransformer | (text: string) => string | - | Transforms pasted text, e.g. to remove hyphens. |
Form Props
| Prop | Type | Default | Description |
|---|---|---|---|
name | string | - | Name attribute for form submission. |
autoFocus | boolean | - | Whether to focus the input on mount. |
...rest | Every <input> attribute is accepted |
InputOtp.Group
| Prop | Type | Default | Description |
|---|---|---|---|
children | React.ReactNode | - | InputOtp.Slot components. |
className | string | - | Additional classes for the group. |
...rest | Every <div> attribute is accepted |
InputOtp.Slot
| Prop | Type | Default | Description |
|---|---|---|---|
index | number | - | Required. Zero-based index of the slot. |
className | string | - | Additional classes for the slot. |
...rest | Every <div> attribute is accepted |
InputOtp.Separator
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional classes for the separator. |
...rest | Every <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)