Skip to main content
Kobalte

OTP Field

Since v2.0.0@kobalte/core/otp-fieldSource

An accessible one-time password input built from a hidden native <input> and fully customizable visual slots.

Import

import { OTPField } from "@kobalte/core/otp-field";
// or
import { Root, Input, Label, Description, ErrorMessage } from "@kobalte/core/otp-field";

Features

  • Built with a hidden native <input> that handles all keyboard interaction and browser autofill.
  • Visual slots are fully customizable — build any design using the provided context hook.
  • Active slot and caret position tracking exposed via context.
  • Supports custom character patterns via a regex pattern prop.
  • Syncs with form reset events.
  • Can be controlled or uncontrolled.
  • Shifts password manager icons to avoid overlapping the slots.
  • Integrates with Kobalte's form control system — supports validationState, required, disabled, and readOnly with associated Label, Description, and ErrorMessage sub-components.

Anatomy

The OTP field consists of:

  • OTPField: The root container that manages value state and provides form control context.
  • OTPField.Label: A label that gives the user information on the OTP field.
  • OTPField.Input: The hidden native <input> that handles keyboard events, autofill, and selection tracking.
  • OTPField.Description: A description that gives the user more information on the OTP field.
  • OTPField.ErrorMessage: An error message that gives the user information on how to fix a validation error.
  • Slot (user-defined): A visual character cell built with useOTPFieldContext.
<OTPField>
<OTPField.Label />
<OTPField.Input />
{/* user-defined slot elements */}
<OTPField.Description />
<OTPField.ErrorMessage />
</OTPField>

Example

import { OTPField, useOTPFieldContext } from "@kobalte/core/otp-field";
import { For } from "solid-js";
import "./style.css";
function makeSlots(n: number) {
return Array.from({ length: n }, (_, i) => i);
}
function OtpSlot(props: { index: number }) {
const context = useOTPFieldContext();
const char = () => context.value()[props.index];
const isActive = () => context.activeSlots().includes(props.index);
const showCaret = () => context.isInserting() && context.activeSlots()[0] === props.index;
return (
<div class="otp-field__slot" data-active={isActive() ? "" : undefined}>
{showCaret() ? <div class="otp-field__caret" /> : char()}
</div>
);
}
function App() {
return (
<OTPField maxLength={6} class="otp-field">
<OTPField.Input />
<For each={makeSlots(6)}>{i => <OtpSlot index={i} />}</For>
</OTPField>
);
}
.otp-field {
display: flex;
gap: 8px;
align-items: center;
}
.otp-field__slot {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 44px;
height: 52px;
border-radius: 8px;
border: 1.5px solid hsl(240 6% 90%);
font-size: 20px;
font-weight: 600;
color: hsl(240 4% 16%);
transition:
border-color 150ms,
box-shadow 150ms;
user-select: none;
}
.otp-field__slot[data-active] {
border-color: hsl(200 98% 39%);
box-shadow: 0 0 0 3px hsl(200 98% 39% / 0.15);
}
.otp-field__caret {
width: 1.5px;
height: 24px;
background-color: hsl(200 98% 39%);
border-radius: 999px;
animation: otp-caret-blink 1.2s ease-out infinite;
}
@keyframes otp-caret-blink {
0%,
70%,
100% {
opacity: 1;
}
20%,
50% {
opacity: 0;
}
}

Usage

Default value

An initial, uncontrolled value can be provided using the defaultValue prop.

1
2
3
<OTPField maxLength={6} defaultValue="123">
<OTPField.Input />
<For each={makeSlots(6)}>{i => <OtpSlot index={i} />}</For>
</OTPField>

Controlled value

The value and onChange props can be used to make the value controlled.

Value:

import { createSignal } from "solid-js";
function ControlledExample() {
const [value, setValue] = createSignal("");
return (
<>
<OTPField maxLength={6} value={value()} onChange={setValue}>
<OTPField.Input />
<For each={makeSlots(6)}>{i => <OtpSlot index={i} />}</For>
</OTPField>
<p>Value: {value()}</p>
</>
);
}

Completion callback

The onComplete prop fires once when all slots are filled.

Enter your 6-digit code.

<OTPField maxLength={6} onComplete={v => console.log("Complete:", v)}>
<OTPField.Input />
<For each={makeSlots(6)}>{i => <OtpSlot index={i} />}</For>
</OTPField>

Custom character pattern

By default the input only accepts digits (^\d*$). Pass a pattern prop on OTPField.Input to allow other characters.

<OTPField maxLength={4}>
<OTPField.Input pattern="^[a-zA-Z]*$" />
<For each={makeSlots(4)}>{i => <OtpSlot index={i} />}</For>
</OTPField>

Pass pattern={null} to disable validation entirely and allow any character.

Animated slots

Render the caret and character exclusively (not overlaid) so the character <span> mounts fresh on every keystroke. The CSS animation property fires on mount, producing a pop-in effect with no JavaScript.

function AnimatedOtpSlot(props: { index: number }) {
const context = useOTPFieldContext();
const char = () => context.value()[props.index];
const isActive = () => context.activeSlots().includes(props.index);
const showCaret = () => context.isInserting() && context.activeSlots()[0] === props.index;
return (
<div class="otp-field__slot" data-active={isActive() ? "" : undefined}>
{showCaret() ? (
<div class="otp-field__caret" />
) : (
<span class="otp-field__char">{char()}</span>
)}
</div>
);
}
.otp-field__char {
display: inline-block;
animation: otp-slip-in 0.5s cubic-bezier(0.16, 1, 0.3, 1) both;
}
@keyframes otp-slip-in {
from {
transform: translateY(8px);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}

Validation

Use validationState, OTPField.Label, OTPField.Description, and OTPField.ErrorMessage to integrate with Kobalte's form control system. OTPField.ErrorMessage only renders when validationState is "invalid".

Enter the 6-digit code sent to your device.
function ValidationExample() {
const [value, setValue] = createSignal("");
const isInvalid = () => value().length > 0 && value().length < 6;
return (
<OTPField
maxLength={6}
value={value()}
onChange={setValue}
validationState={isInvalid() ? "invalid" : "valid"}
>
<OTPField.Label>Verification code</OTPField.Label>
<OTPField.Input />
{/* ...slots... */}
<OTPField.Description>Enter the 6-digit code sent to your device.</OTPField.Description>
<OTPField.ErrorMessage>Please enter all 6 digits.</OTPField.ErrorMessage>
</OTPField>
);
}

HTML forms

Add a name prop to OTPField.Input to include the value in form submissions. The field value resets automatically when the parent form is reset.

<form onSubmit={handleSubmit}>
<OTPField maxLength={6}>
<OTPField.Input name="otp" />
<For each={makeSlots(6)}>{i => <OtpSlot index={i} />}</For>
</OTPField>
</form>

API Reference

OTPField

OTPField is equivalent to the Root import from @kobalte/core/otp-field.

PropDescription
maxLengthnumber
The maximum number of characters the OTP field accepts.
valuestring
The controlled value of the OTP field.
defaultValuestring
The default value when initially rendered. Useful when you do not need to control the value.
onChange(value: string) => void
Event handler called when the value changes.
onComplete(value: string) => void
Event handler called once when all slots are filled.
shiftPWManagersboolean
Whether to shift password manager icons to the right to avoid overlapping slots. Defaults to true.
idstring
A unique identifier. Used to generate ids for nested Label, Description, and ErrorMessage. Auto-generated if not provided.
validationState'valid' | 'invalid'
Whether the OTP field should display its "valid" or "invalid" visual styling.
requiredboolean
Whether the user must fill the OTP field before the form can be submitted.
disabledboolean
Whether the OTP field is disabled.
readOnlyboolean
Whether the OTP field is read only.

OTPField.Label

PropDescription
idstring
The id of the label element. Auto-generated from the root id if not provided.

OTPField.Description

PropDescription
idstring
The id of the description element. Auto-generated from the root id if not provided.

OTPField.ErrorMessage

PropDescription
forceMountboolean
Used to force mounting when more control is needed. Useful when controlling animation with SolidJS animation libraries.

OTPField.Input

PropDescription
patternstring | null
Regex pattern for allowed characters. Defaults to '^\d*$' (digits only). Pass null to allow any character.
noScriptCSSFallbackstring | null
CSS injected via <noscript> for no-JS environments. Pass null to disable. Defaults to a built-in fallback.

OTPField.Input also accepts all native <input> attributes. Use name for form submission.

Data attributeDescription
data-kb-otp-field-inputPresent on the native input element.

useOTPFieldContext

Returns the OTPFieldContextValue object. Must be called inside an OTPField.

PropertyTypeDescription
valueAccessor<string>The current OTP value.
isFocusedAccessor<boolean>Whether the hidden input is currently focused.
isHoveredAccessor<boolean>Whether the pointer is hovering over the root element.
isInsertingAccessor<boolean>Whether the caret is at the end of the value (insertion position).
maxLengthAccessor<number>The configured maximum length.
activeSlotsAccessor<number[]>Indices of the currently active (selected/focused) slots.
rootHeightAccessor<number | null>Measured height of the root element, used for input font-size scaling.
generateId(part: string) => stringGenerates a namespaced id from the root id. Useful when building custom sub-components.

Rendered elements

ComponentDefault rendered element
OTPFielddiv
OTPField.Labellabel
OTPField.Descriptiondiv
OTPField.ErrorMessagediv
OTPField.Inputinput

Last updated: 11/28/26, 9:28 PM