Input OTP
Enter a one-time code, one character to a slot.
import { Div, signal, Span } from "@implementjs/core";
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
REGEXP_ONLY_DIGITS,
} from "@/lib/components/ui/input-otp";
export default function InputOTPDemo() {
const value = signal("");
return Div(
{ class: "flex flex-col items-center gap-2" },
InputOTP(
{
value,
maxLength: 6,
pattern: REGEXP_ONLY_DIGITS,
inputProps: { "aria-label": "Verification code" },
},
InputOTPGroup(...[0, 1, 2].map((index) => InputOTPSlot({ index }))),
InputOTPSeparator(),
InputOTPGroup(...[3, 4, 5].map((index) => InputOTPSlot({ index }))),
),
Span(
{ class: "text-sm text-muted-foreground" },
value.bind((v) => (v === "" ? "Enter your one-time code." : `You entered: ${v}`)),
),
);
}An input OTP draws a code as a row of slots, but the typing happens in one real input laid invisibly over them. That keeps everything a browser does for free — paste, autofill from an SMS, mobile keyboards, selection, form submission — while the slots are only a picture of what is in it.
import { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator } from "@implementjs/primitives";
InputOTP(
{ maxLength: 6 },
InputOTPGroup(...[0, 1, 2].map((index) => InputOTPSlot({ index }))),
InputOTPSeparator("-"),
InputOTPGroup(...[3, 4, 5].map((index) => InputOTPSlot({ index }))),
);
Each part accepts optional props and children — pass a props object when you need attributes, or pass children directly. See createComponent. Extra props are forwarded onto the underlying Divs; the real input takes id, name, required, disabled, inputMode, and inputProps. Slots take a zero-based index, and you lay out as many as maxLength.
Value
value defaults to "". Pass a string to seed it, or a signal to control it from outside:
const code = signal("");
InputOTP({ value: code, maxLength: 6, onComplete: (code) => verify(code) } /* slots */);
onValueChange reports the code after each change, and onComplete runs once the last slot has been filled — a natural place to submit.
Restricting characters
pattern is what the whole value must match, as a RegExp or its source. A keystroke or paste that would break it is refused and the code stays as it was. Three ready-made patterns are exported:
import { InputOTP, REGEXP_ONLY_DIGITS } from "@implementjs/primitives";
InputOTP({ maxLength: 6, pattern: REGEXP_ONLY_DIGITS } /* slots */);
REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS, and REGEXP_ONLY_DIGITS_AND_CHARS. Without a pattern anything goes. inputMode defaults to "numeric"; pass "text" for codes with letters.
Forms
Pass name and the code submits with its form like any input. The input has autocomplete="one-time-code", so a phone can offer the code it just received.
Keyboard
The slot the next character lands in is the active one, and the input keeps its selection there: clicking or arrowing never strands the cursor in the middle of the code. Typing fills slots left to right, Backspace removes the last character, and once the code is full, typing replaces the last slot.
Accessibility
The real input is the one control, so a screen reader meets a text field holding the code. Slots are aria-hidden. The default aria-label is "One-time code"; pass your own through inputProps, or give the input an id and point a label at it.
Styling
The root sets data-input-otp-root (and data-focused, data-disabled). A slot sets data-input-otp-slot with data-index, data-active, data-filled, and data-disabled. Inside it sit a data-input-otp-char and a data-input-otp-caret whose data-state is "visible" only while the slot is active and empty — style that to draw your own caret.
API Reference
InputOTP
The root. Draws the slots and renders one real input over them, which takes the typing, pasting, autofill, and form submission. Renders a Div; extra props are forwarded onto it.
| Prop | Type | Default | Description |
|---|---|---|---|
value | Signal<string> | string | "" | The code typed so far. Pass a signal to control it from outside; a string seeds uncontrolled state. |
onValueChange | (value: string) => void | — | Runs whenever the code changes. |
onComplete | (value: string) => void | — | Runs once the last slot has been filled. |
maxLength | number | 6 | How many characters the code has. |
pattern | RegExp | string | — | What the whole value must match. A change that would break it is refused. REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS, and REGEXP_ONLY_DIGITS_AND_CHARS cover the usual cases. |
disabled | Readable<boolean> | boolean | false | Disables the input. |
name | string | — | Submits the code with a form under this name. |
required | boolean | — | Marks the input required for form validation. |
inputMode | "numeric" | "text" | "decimal" | "tel" | "search" | "email" | "url" | "none" | "numeric" | Which keyboard a touch device shows. |
inputProps | ElementProps<"input"> | — | Props for the real input — an aria-label, aria-invalid, or an event handler. It defaults to aria-label "One-time code". |
id | string | — | Set on the real input, so a label's `for` reaches it. |
| Data attribute | Value |
|---|---|
[data-input-otp-root] | Present |
[data-focused] | Present while the input has focus |
[data-disabled] | Present when disabled |
InputOTPGroup
A run of slots. Sets role="group". Renders a Div; extra props are forwarded onto it.
| Data attribute | Value |
|---|---|
[data-input-otp-group] | Present |
InputOTPSlot
One character of the code. Draws the character and a caret; children render after them. Hidden from assistive tech — the real input carries the value. Renders a Div; extra props are forwarded onto it.
| Prop | Type | Default | Description |
|---|---|---|---|
index* | number | — | Zero-based position of the character the slot shows. |
| Data attribute | Value |
|---|---|
[data-input-otp-slot] | Present |
[data-index] | The slot's index |
[data-active] | Present while it is the slot the next character lands in |
[data-filled] | Present when it holds a character |
[data-disabled] | Present when disabled |
InputOTPSeparator
A divider between groups. Sets role="separator". Renders a Div; extra props are forwarded onto it.
| Data attribute | Value |
|---|---|
[data-input-otp-separator] | Present |