Playground

OTP input

Enter a one-time code, one digit per box, with paste support.

  • Built on native elements
  • The boxes are a group with a name; each box is labelled "Digit N of M".
  • Typing moves forward, Backspace moves back, arrows move, pasting fills all boxes.
  • inputmode="numeric" and autocomplete="one-time-code" for mobile and autofill.
  • Styles in one file: otp-input.css

Import

import '@monoline/css';
import {OtpInput} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add otp-input

Usage

import {useState} from 'react';
import {OtpInput, type Demo} from '@monoline/react';

function Demo() {
  const [done, setDone] = useState('');
  return <div style={{display: 'grid', gap: 'var(--s3)'}}><OtpInput aria-label="Verification code" onComplete={setDone} /><p className="muted small" role="status">{done ? `Entered ${done}` : 'Enter 6 digits'}</p></div>;
}

<Demo />

Examples

Six digits

Props

OtpInputProps

PropTypeDefaultDescription
aria-label requiredstring–Names the group, for example "Verification code".
lengthnumber6Number of boxes, which is the length of the code. Default 6.
valuestring–The code typed so far when controlled. Pair with `onValueChange`.
defaultValuestring''The code typed at first when uncontrolled. Default empty.
onValueChange((value: string) => void)–Called with the whole code (so far) every time a character is added or removed.
onComplete((value: string) => void)–Called once when every box is filled.
alphanumericboolean–Allow letters as well as digits.
disabledboolean–Disables every box.
invalidboolean–Marks the boxes as having an error: sets `aria-invalid` and shows the error border.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

This component sets no state attributes.

CSS classes

ClassStylesheet
.otpotp-input.css
.otp-boxotp-input.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.otp {
  border-color: var(--accent);
}

Accessibility

  • The boxes are a group with a name; each box is labelled "Digit N of M".
  • Typing moves forward, Backspace moves back, arrows move, pasting fills all boxes.
  • inputmode="numeric" and autocomplete="one-time-code" for mobile and autofill.

Keyboard

KeysAction
BackspaceMoves back, arrows move, pasting fills all boxes