implement
Input OTP

Input OTP

A one-time code field with a slot per character.

Enter your one-time code.
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}`)),
		),
	);
}

Installation

npx jsrepo add @implementjs/ui/input-otp

It installs @implementjs/lucide at the same time.

Usage

Lay out a slot for every character, in as many groups as you like. The caret blinks with Tailwind's built-in animate-pulse, so there are no keyframes to add.

import { signal } from "@implementjs/core";
import {
	InputOTP,
	InputOTPGroup,
	InputOTPSeparator,
	InputOTPSlot,
	REGEXP_ONLY_DIGITS,
} from "@/lib/components/ui/input-otp";

const value = signal("");

InputOTP(
	{ value, maxLength: 6, pattern: REGEXP_ONLY_DIGITS, onComplete: (code) => verify(code) },
	InputOTPGroup(...[0, 1, 2].map((index) => InputOTPSlot({ index }))),
	InputOTPSeparator(),
	InputOTPGroup(...[3, 4, 5].map((index) => InputOTPSlot({ index }))),
);

Invalid

Pass aria-invalid to the real input and the slots take the destructive border:

InputOTP({ inputProps: { "aria-invalid": true } } /* slots */);

API Reference

Every prop the styling does not consume is forwarded to the Input OTP primitive, so the tables below are the whole surface.

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.

PropTypeDefaultDescription
valueSignal<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.
maxLengthnumber6How many characters the code has.
patternRegExp | 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.
disabledReadable<boolean> | booleanfalseDisables the input.
namestring—Submits the code with a form under this name.
requiredboolean—Marks the input required for form validation.
inputMode"numeric" | "text" | "decimal" | "tel" | "search" | "email" | "url" | "none""numeric"Which keyboard a touch device shows.
inputPropsElementProps<"input">—Props for the real input — an aria-label, aria-invalid, or an event handler. It defaults to aria-label "One-time code".
idstring—Set on the real input, so a label's `for` reaches it.
Data attributeValue
[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 attributeValue
[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.

PropTypeDefaultDescription
index*number—Zero-based position of the character the slot shows.
Data attributeValue
[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 minus icon unless you pass children of your own. Renders a Div; extra props are forwarded onto it.

Data attributeValue
[data-input-otp-separator]Present