Input OTP
npx shadcn add @klarhimmel/input-otpCopy for AIExamples
Basic
States
Form
DetailsAbout, usage, props and more
A code split into boxes that behave as one field: paste fills them all, Backspace goes back, autofill works.
When to use
Use for
A short code that was sent to the person: six digits by email or text, a booking reference.
Not for
A long secret or a password. Use Input.
Pairs with
Button, a resend link, a status line.
Preview
Enter the six-digit code we emailed you
0 of 6 digits.
Import
Import
- Files
- input-otp.tsx, in components/klarhimmel/ui/. With it: theme, utils.
- Render in
- Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
length | number | 6 | How many characters. Default 6. |
value | string | - | Controlled or uncontrolled. The value is one string without gaps. |
defaultValue | string | '' | Controlled or uncontrolled. The value is one string without gaps. |
onValueChange | (value: string) => void | - | Controlled or uncontrolled. The value is one string without gaps. |
onComplete | (value: string) => void | - | Called once every box is filled. |
type | 'numeric' | 'alphanumeric' | 'numeric' | Digits only, or letters and digits. |
name | string | - | Submits the whole code with a form (a hidden input). |
disabled | boolean | - | Error styling on every box; switches them off. |
readOnly | boolean | - | Error styling on every box; switches them off. |
invalid | boolean | - | Error styling on every box; switches them off. |
aria-label | string | - | Names the group. Required unless aria-labelledby points at a label. |
aria-labelledby | string | - | Required: names the group. |
aria-describedby | string | - | |
boxLabel | (n: number, of: number) => string | - | The name of box `n` of `of`: default "Digit 1 of 6" (or "Character" for alphanumeric). Pass it translated. |
className | string | - | Extra classes, merged after the component's own. |
Accessibility
| Key | Does |
|---|---|
Type | Fills the box and moves to the next; letters are ignored in numeric mode |
Paste | Fills from the box you pasted into, ignoring spaces |
Backspace | Clears the box; in an empty box removes the one before and moves back |
Delete | Removes the character in the box |
Left / Right / Home / End | Move between boxes |
The first box carries autocomplete one-time-code, so phones offer the code from a message. Each box is named "Digit n of N".
Accessibility