A one-time code in separate slots: paste fills them all, typing moves forward, backspace moves back.
- Email sign-in code
- Two-factor check
- Confirm an invite
- Approve an agent action
Examples
Installation
npx knowns-ui@latest add otp-inputPaste this into your coding agent (Claude Code, Cursor, Codex). It checks your project, sets up the registry, runs the install and verifies it.
Install OTP input from Knowns UI into this project. Work step by step, show me each change, and ask before you edit files outside this project.
Context: Knowns UI (https://ui.knowns.dev) is a shadcn registry. Items land in the project as source files. Docs: https://ui.knowns.dev/docs/otp-input
1. Check the project first. Stop and tell me if any of these is missing:
- React 19 and Tailwind CSS v4.
- A components.json at the project root. If there is none, ask me before running `npx knowns-ui@latest init --no-key` (it runs shadcn init when needed, then adds the registries).
2. Run:
```bash
npx knowns-ui@latest add otp-input
```
It writes these files:
- lib/knowns/run.ts
- lib/knowns/json.tsx
- lib/knowns/status.ts
- lib/knowns/tone.ts
- lib/knowns/types.ts
- lib/knowns/use-abortable.ts
- lib/knowns/utils.ts
- lib/knowns/motion.tsx
- lib/knowns/motion-utils.ts
- components/ui/knowns/otp-input.tsx
and installs these npm packages: clsx@^2.1.1, tailwind-merge@^3.7.0.
3. While it runs:
- It also adds a few @keyframes (all named kn-*) to the global CSS file (globals.css or the file components.json points at). Let it: they only add animations.
- If it asks to overwrite files under lib/knowns/: these are shared by every Knowns UI item. List them, say whether I changed them (check git), and ask me before you answer.
- If it asks to overwrite any other file, show me the diff and ask me.
4. Verify:
- Every file in the list above exists.
- The project type-checks (for example `npx tsc --noEmit`) and the dev build has no new errors.
5. Use it: follow the examples and props on https://ui.knowns.dev/docs/otp-input, and ask me if anything about my data is unclear.
If the Knowns UI MCP server is connected (https://ui.knowns.dev/docs/ai-and-mcp), use get_component({ name: "otp-input" }) or search_docs for details.
Finish with a short summary: the files you added or changed, the packages you installed, and anything I still need to do.Props
- length
number- Default
6 - Number of slots.
- value / defaultValue / onValueChange
string- Default
"" - The code so far.
- onComplete
(code) => void- Fires when every slot is filled.
- onVerify
(code, signal) => boolean|Promise<boolean>- Checks a complete code. While it runs the slots slide together into one pill; true turns it green with a check and the label; false or a throw shakes it once and splits it back with the code cleared and marked invalid. The signal aborts when the code changes or the input unmounts.
- status / onStatusChange
"idle"|"verifying"|"verified"|"error"- Default
"idle" - The verify state, to control it yourself or to follow it (for a message under the field).
- verifiedLabel
string- Default
"Verified" - Words in the green pill.
- mode
"numeric"|"alphanumeric"- Default
"numeric" - Accepted characters; letters are upper-cased. Sets inputMode for the right mobile keyboard.
- mask
boolean- Default
false - Dots instead of characters.
- groups
number[]- Gaps after these slot counts, e.g. [3].
- invalid / disabled
boolean- Default
false - Red ring / not editable. Inside a Field with an error, invalid is set for you.
- size
"md"|"lg"- Default
"md" - 40 or 48px slots.
- Motion
-- A typed character drops 6px into its slot (180ms); one focus tint slides to the active slot (120ms); a caret blinks in the empty active slot; merging, the slots slide together and square their inner corners (180ms); an error shakes once (280ms). Reduced motion: all instant, the caret steady.
- id / name
string- id goes on the first slot (for Field); name adds a hidden input with the whole code.
| Prop | Type | Default | Description |
|---|---|---|---|
| length | number | 6 | Number of slots. |
| value / defaultValue / onValueChange | string | "" | The code so far. |
| onComplete | (code) => void | - | Fires when every slot is filled. |
| onVerify | (code, signal) => boolean | Promise<boolean> | - | Checks a complete code. While it runs the slots slide together into one pill; true turns it green with a check and the label; false or a throw shakes it once and splits it back with the code cleared and marked invalid. The signal aborts when the code changes or the input unmounts. |
| status / onStatusChange | "idle" | "verifying" | "verified" | "error" | "idle" | The verify state, to control it yourself or to follow it (for a message under the field). |
| verifiedLabel | string | "Verified" | Words in the green pill. |
| mode | "numeric" | "alphanumeric" | "numeric" | Accepted characters; letters are upper-cased. Sets inputMode for the right mobile keyboard. |
| mask | boolean | false | Dots instead of characters. |
| groups | number[] | - | Gaps after these slot counts, e.g. [3]. |
| invalid / disabled | boolean | false | Red ring / not editable. Inside a Field with an error, invalid is set for you. |
| size | "md" | "lg" | "md" | 40 or 48px slots. |
| Motion | - | - | A typed character drops 6px into its slot (180ms); one focus tint slides to the active slot (120ms); a caret blinks in the empty active slot; merging, the slots slide together and square their inner corners (180ms); an error shakes once (280ms). Reduced motion: all instant, the caret steady. |
| id / name | string | - | id goes on the first slot (for Field); name adds a hidden input with the whole code. |
Keyboard: type to fill and advance, Backspace clears and steps back, ← → Home End move. Pasting anywhere fills from that slot (a full-length code fills from the first). The first slot has autocomplete="one-time-code", so phones offer the code from SMS.
What it handles
- 01Splits a code across length slots (6 by default), each a real input that takes one character and moves on.
- 02Fills every slot from one paste, starting at the first slot when the pasted text is as long as the whole code.
- 03Backspace clears the slot, or the one before when it is already empty; Delete, ArrowLeft, ArrowRight, Home and End move around the row.
- 04Accepts digits in numeric mode and letters plus digits in alphanumeric mode, upper-casing the latter and dropping anything else.
- 05Calls onComplete once, when the last slot fills, and sets one-time-code autocomplete on the first slot so a phone can offer the SMS code.
- 06Masks characters with mask, draws a gap after the slot counts in groups, and marks every slot invalid together through invalid.
States
- First render
- Nothing. The slots paint empty or with the value
- none
- Focus
- The slot gets a ring and its text is selected
- 180ms, ease out
- Typing a character
- It lands in the slot and focus moves to the next one
- none, at once
- Invalid
- Every slot swaps its ring to red
- 180ms, ease out
- Reduced motion
- The ring still changes colour, with no other motion to remove
- none
| State | What moves | Timing |
|---|---|---|
| First render | Nothing. The slots paint empty or with the value | none |
| Focus | The slot gets a ring and its text is selected | 180ms, ease out |
| Typing a character | It lands in the slot and focus moves to the next one | none, at once |
| Invalid | Every slot swaps its ring to red | 180ms, ease out |
| Reduced motion | The ring still changes colour, with no other motion to remove | none |
Works with
When to use
When to use
- One-time codes from email or SMS, with paste, auto-advance and an error state.
When not to use
- Passwords or long tokens: use Password Field.
Using with AI
Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:
- Add the Knowns UI OTP Input to my app and wire it to my own data.
- I need one-time codes from email or SMS, with paste, auto-advance and an error state. Set that up with the Knowns UI OTP Input.
- Which OTP Input props do I need for a minimal setup? Show me a short example.
With the server connected, your assistant calls get_component({ name: "otp-input" }) and search_docs({ query: "otp input" }).