Input OTP
A one-time code field with a slot per character.
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.
Copy the file below to src/lib/components/ui/input-otp.ts. It imports cn from @implementjs/cn. Then, on top of @implementjs/core and @implementjs/primitives:
npm install @implementjs/lucide
import { type Child, type ComponentProps } from "@implementjs/core";
import { MinusIcon } from "@implementjs/lucide";
import {
InputOTP as InputOTPPrimitive,
InputOTPGroup as InputOTPGroupPrimitive,
InputOTPSlot as InputOTPSlotPrimitive,
InputOTPSeparator as InputOTPSeparatorPrimitive,
} from "@implementjs/primitives";
import { createComponent } from "@implementjs/primitives";
import { cn } from "@implementjs/cn";
export {
REGEXP_ONLY_DIGITS,
REGEXP_ONLY_CHARS,
REGEXP_ONLY_DIGITS_AND_CHARS,
} from "@implementjs/primitives";
export type InputOTPProps = ComponentProps<typeof InputOTPPrimitive>;
export type InputOTPGroupProps = ComponentProps<typeof InputOTPGroupPrimitive>;
export type InputOTPSlotProps = ComponentProps<typeof InputOTPSlotPrimitive>;
export type InputOTPSeparatorProps = ComponentProps<typeof InputOTPSeparatorPrimitive>;
export const InputOTP = createComponent(function InputOTP(
{ class: className, ...props }: InputOTPProps,
...children: Child[]
) {
return InputOTPPrimitive(
{
...props,
"data-slot": "input-otp",
class: cn("group/otp flex items-center gap-2 data-[disabled]:opacity-50", className),
},
...children,
);
});
export const InputOTPGroup = createComponent(function InputOTPGroup(
{ class: className, ...props }: InputOTPGroupProps,
...children: Child[]
) {
return InputOTPGroupPrimitive(
{ ...props, "data-slot": "input-otp-group", class: cn("flex items-center", className) },
...children,
);
});
export const InputOTPSlot = createComponent(function InputOTPSlot(
{ class: className, ...props }: InputOTPSlotProps,
...children: Child[]
) {
return InputOTPSlotPrimitive(
{
...props,
"data-slot": "input-otp-slot",
class: cn(
"relative flex size-9 items-center justify-center border-y border-r border-input text-sm shadow-xs transition-all outline-none first:rounded-l-md first:border-l last:rounded-r-md",
"data-[active]:z-10 data-[active]:border-ring data-[active]:ring-[3px] data-[active]:ring-ring/50",
"group-has-[input[aria-invalid=true]]/otp:border-destructive group-has-[input[aria-invalid=true]]/otp:data-[active]:ring-destructive/20",
"[&>[data-input-otp-caret]]:pointer-events-none [&>[data-input-otp-caret]]:absolute [&>[data-input-otp-caret]]:h-4 [&>[data-input-otp-caret]]:w-px [&>[data-input-otp-caret]]:animate-pulse [&>[data-input-otp-caret]]:bg-foreground [&>[data-input-otp-caret][data-state=hidden]]:hidden",
"dark:bg-input/30",
className,
),
},
...children,
);
});
export const InputOTPSeparator = createComponent(function InputOTPSeparator(
{ class: className, ...props }: InputOTPSeparatorProps,
...children: Child[]
) {
return InputOTPSeparatorPrimitive(
{ ...props, "data-slot": "input-otp-separator", class: cn(className) },
...(children.length > 0 ? children : [MinusIcon({ "aria-hidden": true, class: "size-4" })]),
);
});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.
| 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 minus icon unless you pass children of your own. Renders a Div; extra props are forwarded onto it.
| Data attribute | Value |
|---|---|
[data-input-otp-separator] | Present |