OTP input
Enter a one-time code, one digit per box, with paste support.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- 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
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label required | string | – | Names the group, for example "Verification code". |
length | number | 6 | Number of boxes, which is the length of the code. Default 6. |
value | string | – | The code typed so far when controlled. Pair with `onValueChange`. |
defaultValue | string | '' | 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. |
alphanumeric | boolean | – | Allow letters as well as digits. |
disabled | boolean | – | Disables every box. |
invalid | boolean | – | 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
| Class | Stylesheet |
|---|---|
.otp | otp-input.css |
.otp-box | otp-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
| Keys | Action |
|---|---|
| Backspace | Moves back, arrows move, pasting fills all boxes |