仕組み
The slots are only visual (aria-hidden). Input goes to one transparent <input autocomplete="one-time-code"> that spans the row, so SMS autofill, paste, screen readers and form submission stay native. Typing on a filled slot selects and overwrites it; the arrow keys and clicks move the selection, invalid characters are filtered, and a full pasted code replaces everything. When the value is cleared from outside (for example after a wrong code), the caret returns to the first empty slot.
向いている場面
- Emailed or texted codes for sign-in, sign-up, changing a phone number and password recovery.
- A second check before payments or withdrawals, with mask on.
- Short fixed-length codes such as invitations or vouchers (type alphanumeric).
向かない場面
- Codes of variable length or more than 8 characters: use a normal text field.
- Account passwords: use a password field instead of slots.
- A PIN the user is setting: it also needs a confirmation step; this component only handles input.
使い方
import { useState } from 'react'
import { OtpInput } from './OtpInput.jsx'
export function VerifyCode() {
const [code, setCode] = useState('')
const [status, setStatus] = useState('default') // 'default' | 'error' | 'success'
return (
<OtpInput
length={6}
value={code}
status={status}
onChange={(v) => {
setCode(v)
setStatus('default')
}}
onComplete={async (v) => {
const ok = await api.verify(v)
setStatus(ok ? 'success' : 'error')
}}
/>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
length | number | 6 | Number of digits. |
value | string | — | Controlled value. |
defaultValue | string | '' | Initial value when uncontrolled. |
onChange | (value) => void | — | Called on change, with invalid characters filtered out and the value cut to length. |
onComplete | (value) => void | — | Called when the code is full, and again if a digit is edited afterwards, so you can validate again. |
type | 'numeric' | 'alphanumeric' | 'numeric' | numeric accepts digits and shows the number keyboard; alphanumeric accepts letters and digits in upper case. |
status | 'default' | 'error' | 'success' | 'default' | error turns red and shakes once; success turns green with a staggered bounce. While it stays error, each refill shakes again. For async checks, reset to default in onChange and set error when the result arrives. |
mask | boolean | false | Hides typed characters behind dots. |
groupSize | number | 0 | Adds a short dash after every this many slots; 3 suits six-digit codes. |
labels | object | — | Replaces built-in text (label: (length) => string, the input's accessible name). An aria-label prop also overrides it. |
disabled | boolean | false | Disables the field. |
readOnly | boolean | false | Read only, for example locked after success without greying out. |
ref | Ref<HTMLInputElement> | — | Forwarded to the real <input>, so you can call focus() and blur(). |
...rest | InputHTMLAttributes | — | className and style go to the outer element; other native attributes (name, autoFocus, aria-describedby) go to the real <input>. |
アクセシビリティ
- There is one real <input>, named “Verification code, N digits” by default; override it with labels, aria-label or aria-labelledby. The slots are aria-hidden.
- autocomplete="one-time-code" lets iOS and Android suggest the code from a message above the keyboard.
- inputmode="numeric" shows the number keyboard, and 16px text stops iOS zooming on focus.
- status="error" sets aria-invalid; put the message in a role="status" element and link it with aria-describedby.
- Arrow keys, Home / End, Backspace, Delete, paste and select all work. With reduced motion there is no pop-in or shake, only colour changes.
モーション
The active slot's ring grows in from scale 0.92 over 240ms ease-out while the slot turns white in 160ms. Characters pop in over 240ms with a spring (from scale 0.6, 6px lower); the error shake takes 420ms; success bounces slot by slot (360ms, 40ms apart); the caret blinks on a 1.1s loop.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
OtpInput.jsxotp-input.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。





