Input OTP
⋯
InputOtp renders one native input with separate visual slots. The slots follow the Figma six-cell
row: 46px square cells, 12px gaps, and the same semantic border, type, and focus tokens as
Input.
import { InputOtp } from "@nesra/ui/input-otp";
export function CodeField() { return ( <InputOtp aria-label="Verification code" maxLength={6} name="code" required> <InputOtp.Group> {Array.from({ length: 6 }, (_, index) => ( <InputOtp.Slot key={index} index={index} /> ))} </InputOtp.Group> </InputOtp> );}InputOtp is the only component export from @nesra/ui/input-otp and @nesra/ui. Use its parts
through InputOtp.Group, InputOtp.Slot, and InputOtp.Separator. The matching prop types
(InputOtpProps, InputOtpGroupProps, InputOtpSlotProps, and InputOtpSeparatorProps) are
available from either entrypoint for typed wrappers.
Composition
Section titled “Composition”InputOtp└─ InputOtp.Group ├─ InputOtp.Slot index={0} ├─ InputOtp.Slot index={1} └─ …maxLength is the number of characters. Render one slot for each zero-based index. Groups and
separators change the visual arrangement without adding form fields.
Examples
Section titled “Examples”Default
Section titled “Default”import { InputOtp } from "@nesra/ui/input-otp";
export function InputOtpBasicExample() { return ( <InputOtp aria-label="Verification code" maxLength={6} name="code"> <InputOtp.Group> <InputOtp.Slot index={0} /> <InputOtp.Slot index={1} /> <InputOtp.Slot index={2} /> <InputOtp.Slot index={3} /> <InputOtp.Slot index={4} /> <InputOtp.Slot index={5} /> </InputOtp.Group> </InputOtp> );}States
Section titled “States”Click a cell to inspect the focus ring. aria-invalid gives every cell the destructive border and
ring, including the active cell. The caret still marks the active cell. disabled removes the input
from keyboard navigation and form submission and renders digits in text-disabled. Colors follow the
field state colors in Light and Dark.
import { InputOtp } from "@nesra/ui/input-otp";
function Code({ label, value, disabled, invalid,}: { label: string; value?: string; disabled?: boolean; invalid?: boolean;}) { return ( <div className="grid gap-nesra-padding-xl"> <span className="type-nesra-body-small-medium text-nesra-text-headline">{label}</span> <InputOtp aria-label={label} aria-invalid={invalid || undefined} maxLength={6} defaultValue={value} disabled={disabled} > <InputOtp.Group> {Array.from({ length: 6 }, (_, index) => ( <InputOtp.Slot key={index} index={index} /> ))} </InputOtp.Group> </InputOtp> </div> );}
export function InputOtpStatesExample() { return ( <div className="grid gap-nesra-spacing-2xl"> <Code label="Partially filled" value="52369" /> <Code label="Complete" value="523691" /> <Code label="Error" value="523691" invalid /> <Code label="Disabled" value="523" disabled /> </div> );}Controlled value
Section titled “Controlled value”onChange receives the complete string. onComplete fires when it first reaches maxLength.
import { useState } from "react";import { InputOtp } from "@nesra/ui/input-otp";
export function InputOtpControlledExample() { const [code, setCode] = useState("");
return ( <div className="grid gap-nesra-padding-xl"> <label htmlFor="controlled-code" className="type-nesra-body-small-medium text-nesra-text-headline" > Verification code </label> <InputOtp id="controlled-code" maxLength={6} name="code" value={code} onChange={setCode}> <InputOtp.Group> {Array.from({ length: 6 }, (_, index) => ( <InputOtp.Slot key={index} index={index} /> ))} </InputOtp.Group> </InputOtp> <p className="type-nesra-body-small-regular text-nesra-text-support"> {code.length} of 6 digits entered </p> </div> );}Grouped code
Section titled “Grouped code”import { InputOtp } from "@nesra/ui/input-otp";
export function InputOtpGroupedExample() { return ( <InputOtp aria-label="Verification code" maxLength={6}> <InputOtp.Group> <InputOtp.Slot index={0} /> <InputOtp.Slot index={1} /> <InputOtp.Slot index={2} /> </InputOtp.Group> <InputOtp.Separator /> <InputOtp.Group> <InputOtp.Slot index={3} /> <InputOtp.Slot index={4} /> <InputOtp.Slot index={5} /> </InputOtp.Group> </InputOtp> );}Alphanumeric code
Section titled “Alphanumeric code”Set pattern={REGEXP_ONLY_DIGITS_AND_CHARS} and inputMode="text" to accept letters and digits.
Import the pattern constant from @nesra/ui/input-otp.
import { InputOtp, REGEXP_ONLY_DIGITS_AND_CHARS } from "@nesra/ui/input-otp";
export function InputOtpAlphanumericExample() { return ( <InputOtp aria-label="Alphanumeric code" inputMode="text" maxLength={6} pattern={REGEXP_ONLY_DIGITS_AND_CHARS} > <InputOtp.Group> {Array.from({ length: 6 }, (_, index) => ( <InputOtp.Slot key={index} index={index} /> ))} </InputOtp.Group> </InputOtp> );}Form validation
Section titled “Form validation”The form reads one code value from FormData. It checks for six digits on submit, shows an
associated error for an incomplete code, and clears that error on the next edit. Paste accepts codes
with spaces or hyphens. This demo stops at local validation; the application must verify the code
with its server.
import { useState, type SyntheticEvent } from "react";import { Button } from "@nesra/ui/button";import { InputOtp } from "@nesra/ui/input-otp";
export function InputOtpFormExample() { const [error, setError] = useState(""); const [submitted, setSubmitted] = useState(false);
function handleSubmit(event: SyntheticEvent<HTMLFormElement>) { event.preventDefault(); const code = new FormData(event.currentTarget).get("code"); if (typeof code !== "string" || code.length !== 6) { setError("Enter all six digits."); setSubmitted(false); return; } setError(""); setSubmitted(true); }
return ( <form className="grid gap-nesra-padding-xl" noValidate onSubmit={handleSubmit}> <label htmlFor="otp-form-code" className="type-nesra-body-small-medium text-nesra-text-headline" > Verification code </label> <InputOtp id="otp-form-code" name="code" maxLength={6} required aria-describedby="otp-form-message" aria-invalid={Boolean(error) || undefined} onChange={() => { setError(""); setSubmitted(false); }} pasteTransformer={(pasted) => pasted.replace(/[^0-9]/g, "")} > <InputOtp.Group> {Array.from({ length: 6 }, (_, index) => ( <InputOtp.Slot key={index} index={index} /> ))} </InputOtp.Group> </InputOtp> <p id="otp-form-message" className={ error ? "type-nesra-body-small-regular text-nesra-text-destructive" : "type-nesra-body-small-regular text-nesra-text-support" } role={error ? "alert" : undefined} > {error || "Enter the six digits from your message."} </p> <Button type="submit" className="w-fit"> Verify code </Button> {submitted && ( <output className="type-nesra-body-small-regular text-nesra-text-body"> Code submitted in this demo. </output> )} </form> );}Accessibility
Section titled “Accessibility”- The single native input has one tab stop and accepts typing, selection, paste, and SMS autofill.
Use
aria-labelor associate a visible<label>with itsid. name,required,disabled, and other native input attributes reach the real input. Its ref points to that input. The visual slots are hidden from screen readers.- The default pattern accepts ASCII digits. Set
patternandinputMode="text"for other code formats, including alphanumeric codes. UsepasteTransformerwhen copied codes include separators. Invalid keystrokes and untransformed invalid pastes are rejected. - The consuming form owns validation timing and error text. Set
aria-invalidand an associatedaria-describedbyon the root when showing an error.maxLengthlimits entry but does not by itself reject a partial code at form submission.
API reference
Section titled “API reference”InputOtp
Section titled “InputOtp”| Prop | Type | Default | Effect |
|---|---|---|---|
maxLength |
number |
required | Number of characters in the code. |
value |
string |
— | Controlled value. |
defaultValue |
string |
— | Initial uncontrolled value. |
onChange |
(value: string) => unknown |
— | Receives the new code after an edit. |
onComplete |
(value: string) => unknown |
— | Fires when the code transitions to full length. |
pattern |
string | RegExp |
digits only | Validates each new code. |
pasteTransformer |
(value: string) => string |
— | Transforms pasted text before validation. |
containerClassName |
string |
— | Adds classes to the visible layout container. |
InputOtp inherits the input-otp root and native input props. className applies to the real
input. The ref points to that input. REGEXP_ONLY_DIGITS_AND_CHARS is exported from both
@nesra/ui/input-otp and @nesra/ui for alphanumeric codes.
InputOtp.Group
Section titled “InputOtp.Group”Accepts native div props and a ref. It arranges adjacent slots with a 12px gap.
InputOtp.Slot
Section titled “InputOtp.Slot”| Prop | Type | Default | Effect |
|---|---|---|---|
index |
number |
required | Zero-based character position. |
Accepts other native div props and a ref. It renders the character or an optional placeholder.
Empty slots stay blank until focused, when they show the caret.
InputOtp.Separator
Section titled “InputOtp.Separator”Accepts native div props, children, and a ref. It renders a minus icon by default.
Visual values come from the color roles, typography roles, layout scale, and shape and elevation tokens.