opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Otp
  4. Otp Boxed Input
Back to Otp

Components / Otp

Otp Boxed Input

Free Otp React component — OtpBoxedInput. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Full email verification card with icon header, destination text, animated square cells, progress dots, shake-on-error, and resend countdown.

Preview

Check your email

We sent a 6-digit code to s•••@icloud.com

Paste from clipboard or type each digit

Didn't receive it? Resend in 30s

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/otp/otp-boxed-input.tsx in your project.

  4. 4

    Import and render:

    Example

    import { OtpBoxedInput } from "@/components/otp/otp-boxed-input";

    <OtpBoxedInput destination="you@email.com" error={invalid} onComplete={verify} onResend={sendAgain} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/otp/otp-boxed-input.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
"use client";
 
import {
forwardRef,
useCallback,
useEffect,
useId,
useRef,
useState,
type ClipboardEvent,
type ComponentPropsWithoutRef,
type FormEvent,
type KeyboardEvent,
} from "react";
 
import { Mail } from "lucide-react";
 
import { cn } from "@/lib/cn";
 
type OtpInputMode = "numeric" | "alphanumeric";
 
type OtpFieldOptions = Readonly<{
length: number;
value: string | undefined;
defaultValue: string;
disabled: boolean;
autoFocus: boolean;
inputMode: OtpInputMode;
onChange?: (value: string) => void;
onComplete?: (value: string) => void;
}>;
 
function sanitizeChar(char: string, mode: OtpInputMode): string | null {
if (!char) return null;
const next = char.slice(0, 1);
if (mode === "numeric") return /^\d$/.test(next) ? next : null;
return /^[a-zA-Z0-9]$/.test(next) ? next.toUpperCase() : null;
}
 
function sanitizeValue(raw: string, length: number, mode: OtpInputMode): string {
const filtered =
mode === "numeric"
? raw.replace(/\D/g, "")
: raw.replace(/[^a-zA-Z0-9]/g, "").toUpperCase();
return filtered.slice(0, length);
}
 
function useOtpField({
length,
value,
defaultValue,
disabled,
autoFocus,
inputMode,
onChange,
onComplete,
}: OtpFieldOptions) {
const isControlled = value !== undefined;
const [internal, setInternal] = useState(() =>
sanitizeValue(defaultValue, length, inputMode),
);
const current = isControlled
? sanitizeValue(value, length, inputMode)
: internal;
 
const digits = Array.from({ length }, (_, index) => current[index] ?? "");
const inputRefs = useRef<Array<HTMLInputElement | null>>([]);
const groupId = useId();
const completedRef = useRef("");
 
const focusAt = useCallback(
(index: number) => {
const clamped = Math.max(0, Math.min(length - 1, index));
const node = inputRefs.current[clamped];
if (!node) return;
node.focus();
node.select();
},
[length],
);
 
const commit = useCallback(
(next: string) => {
const sanitized = sanitizeValue(next, length, inputMode);
if (!isControlled) setInternal(sanitized);
onChange?.(sanitized);
 
if (sanitized.length === length && sanitized !== completedRef.current) {
completedRef.current = sanitized;
onComplete?.(sanitized);
}
 
if (sanitized.length < length) completedRef.current = "";
},
[inputMode, isControlled, length, onChange, onComplete],
);
 
useEffect(() => {
if (!autoFocus || disabled) return;
const frame = requestAnimationFrame(() => focusAt(0));
return () => cancelAnimationFrame(frame);
}, [autoFocus, disabled, focusAt]);
 
const setDigit = useCallback(
(index: number, char: string | null) => {
const next = [...digits];
next[index] = char ?? "";
commit(next.join(""));
},
[commit, digits],
);
 
const handleChange = useCallback(
(index: number, event: FormEvent<HTMLInputElement>) => {
if (disabled) return;
 
const raw = event.currentTarget.value;
if (raw.length > 1) {
const sanitized = sanitizeValue(raw, length, inputMode);
commit(sanitized);
focusAt(Math.min(sanitized.length, length - 1));
return;
}
 
const char = sanitizeChar(raw, inputMode);
if (!char) {
event.currentTarget.value = digits[index] ?? "";
return;
}
 
setDigit(index, char);
if (index < length - 1) focusAt(index + 1);
},
[commit, digits, disabled, focusAt, inputMode, length, setDigit],
);
 
const handleKeyDown = useCallback(
(index: number, event: KeyboardEvent<HTMLInputElement>) => {
if (disabled) return;
 
const key = event.key;
 
if (key === "ArrowLeft") {
event.preventDefault();
focusAt(index - 1);
return;
}
 
if (key === "ArrowRight") {
event.preventDefault();
focusAt(index + 1);
return;
}
 
if (key === "Home") {
event.preventDefault();
focusAt(0);
return;
}
 
if (key === "End") {
event.preventDefault();
focusAt(length - 1);
return;
}
 
if (key === "Backspace") {
event.preventDefault();
if (digits[index]) {
setDigit(index, null);
return;
}
if (index > 0) {
setDigit(index - 1, null);
focusAt(index - 1);
}
return;
}
 
if (key === "Delete") {
event.preventDefault();
setDigit(index, null);
return;
}
 
if (key.length === 1 && digits[index]) {
const char = sanitizeChar(key, inputMode);
if (char) {
event.preventDefault();
setDigit(index, char);
if (index < length - 1) focusAt(index + 1);
}
}
},
[digits, disabled, focusAt, inputMode, length, setDigit],
);
 
const handlePaste = useCallback(
(index: number, event: ClipboardEvent<HTMLInputElement>) => {
if (disabled) return;
event.preventDefault();
 
const pasted = event.clipboardData.getData("text");
const sanitized = sanitizeValue(pasted, length - index, inputMode);
if (!sanitized) return;
 
const next = [...digits];
for (let offset = 0; offset < sanitized.length; offset += 1) {
next[index + offset] = sanitized[offset] ?? "";
}
commit(next.join("").slice(0, length));
focusAt(Math.min(index + sanitized.length, length - 1));
},
[commit, digits, disabled, focusAt, inputMode, length],
);
 
const handleFocus = useCallback((index: number) => {
inputRefs.current[index]?.select();
}, []);
 
const setInputRef = useCallback((index: number) => {
return (node: HTMLInputElement | null) => {
inputRefs.current[index] = node;
};
}, []);
 
return {
digits,
groupId,
length,
handleChange,
handleKeyDown,
handlePaste,
handleFocus,
setInputRef,
};
}
 
function useResendTimer(initialSeconds: number, onResend?: () => void) {
const [remaining, setRemaining] = useState(initialSeconds);
const canResend = remaining <= 0;
 
useEffect(() => {
if (canResend) return;
const timer = window.setInterval(() => {
setRemaining((prev) => Math.max(0, prev - 1));
}, 1000);
return () => window.clearInterval(timer);
}, [canResend]);
 
const resend = useCallback(() => {
if (!canResend) return;
onResend?.();
setRemaining(initialSeconds);
}, [canResend, initialSeconds, onResend]);
 
return { remaining, canResend, resend };
}
 
export type OtpBoxedInputProps = Readonly<
{
length?: number;
value?: string;
defaultValue?: string;
disabled?: boolean;
autoFocus?: boolean;
inputMode?: OtpInputMode;
error?: boolean;
label?: string;
hint?: string;
destination?: string;
errorMessage?: string;
resendCooldown?: number;
onResend?: () => void;
onChange?: (value: string) => void;
onComplete?: (value: string) => void;
} & Omit<ComponentPropsWithoutRef<"div">, "onChange" | "defaultValue">
>;
 
export const OtpBoxedInput = forwardRef<HTMLDivElement, OtpBoxedInputProps>(
function OtpBoxedInput(
{
className,
length = 6,
value,
defaultValue = "",
disabled = false,
autoFocus = false,
inputMode = "numeric",
error = false,
label = "Check your email",
hint,
destination = "j•••@gmail.com",
errorMessage = "That code didn't match. Try again or request a new one.",
resendCooldown = 30,
onResend,
onChange,
onComplete,
...props
},
ref,
) {
const [activeIndex, setActiveIndex] = useState<number | null>(null);
const resend = useResendTimer(resendCooldown, onResend);
 
const otp = useOtpField({
length,
value,
defaultValue,
disabled,
autoFocus,
inputMode,
onChange,
onComplete,
});
 
const filledCount = otp.digits.filter(Boolean).length;
const isComplete = filledCount === otp.length;
 
return (
<div
ref={ref}
data-slot="otp-boxed-input"
data-error={error || undefined}
data-complete={isComplete || undefined}
className={cn(
"w-full max-w-md px-6 py-7 font-sans",
className,
)}
{...props}
>
<div className="mb-6 flex flex-col items-center text-center">
<div
className={cn(
"mb-4 flex size-12 items-center justify-center rounded-full transition-colors duration-300",
error
? "text-rose-500"
: isComplete
? "text-emerald-600"
: "text-neutral-700",
)}
>
<Mail size={20} strokeWidth={1.75} aria-hidden />
</div>
 
<p
id={`${otp.groupId}-label`}
className="font-serif text-2xl text-neutral-900"
>
{label}
</p>
<p
id={`${otp.groupId}-hint`}
className="mt-2 max-w-xs text-sm leading-relaxed text-neutral-500"
>
{hint ?? (
<>
We sent a {otp.length}-digit code to{" "}
<span className="font-medium text-neutral-700">{destination}</span>
</>
)}
</p>
</div>
 
<div
role="group"
aria-labelledby={`${otp.groupId}-label`}
aria-describedby={`${otp.groupId}-hint${error ? ` ${otp.groupId}-error` : ""}`}
className={cn(
"flex justify-center gap-2 md:gap-2.5",
error && "rounded-xl border border-rose-200 px-1 py-1",
)}
>
{otp.digits.map((digit, index) => (
<div
key={`${otp.groupId}-${index}`}
className={cn(
"relative transition-transform duration-200 ease-out",
activeIndex === index && "z-10 scale-105",
)}
>
<input
ref={otp.setInputRef(index)}
type="text"
inputMode={inputMode === "numeric" ? "numeric" : "text"}
autoComplete={index === 0 ? "one-time-code" : "off"}
name={index === 0 ? "one-time-code" : undefined}
pattern={inputMode === "numeric" ? "[0-9]*" : "[A-Za-z0-9]*"}
maxLength={otp.length}
value={digit}
disabled={disabled}
aria-label={`Digit ${index + 1} of ${otp.length}`}
aria-invalid={error || undefined}
onChange={(event) => otp.handleChange(index, event)}
onKeyDown={(event) => otp.handleKeyDown(index, event)}
onPaste={(event) => otp.handlePaste(index, event)}
onFocus={() => {
setActiveIndex(index);
otp.handleFocus(index);
}}
onBlur={() => setActiveIndex(null)}
className={cn(
"size-12 rounded-xl border-2 bg-neutral-50/30 text-center font-mono text-2xl font-semibold text-neutral-900 outline-none ring-0 transition-[border-color,background-color,transform,color] duration-200 focus:ring-0 md:size-14",
error
? "border-rose-200 bg-rose-50/40 focus:border-rose-400"
: "border-neutral-100 focus:border-neutral-900 focus:bg-white",
digit && !error && "border-neutral-900 bg-white",
disabled && "cursor-not-allowed bg-neutral-50 text-neutral-400",
)}
/>
{activeIndex === index && !digit ? (
<span
aria-hidden
className="pointer-events-none absolute inset-x-0 top-1/2 mx-auto h-6 w-0.5 -translate-y-1/2 animate-pulse bg-neutral-400"
/>
) : null}
</div>
))}
</div>
 
<div className="mt-4 flex justify-center gap-1.5" aria-hidden>
{otp.digits.map((digit, index) => (
<span
key={`${otp.groupId}-dot-${index}`}
className={cn(
"size-1.5 rounded-full transition-all duration-300",
digit ? "scale-125 bg-neutral-900" : "bg-neutral-200",
activeIndex === index && "scale-150 bg-neutral-900",
error && digit && "bg-rose-400",
isComplete && "bg-emerald-500",
)}
/>
))}
</div>
 
<div className="mt-6 space-y-2 text-center">
{error ? (
<p
id={`${otp.groupId}-error`}
role="alert"
className="text-sm text-rose-600"
>
{errorMessage}
</p>
) : isComplete ? (
<p className="text-sm font-medium text-emerald-600">
Code entered — verifying…
</p>
) : (
<p className="text-sm text-neutral-500">
Paste from clipboard or type each digit
</p>
)}
 
<p className="text-sm text-neutral-500">
Didn&apos;t receive it?{" "}
{resend.canResend ? (
<button
type="button"
disabled={disabled}
onClick={resend.resend}
className="font-medium text-neutral-900 underline-offset-2 transition-opacity hover:underline disabled:cursor-not-allowed disabled:opacity-50"
>
Resend code
</button>
) : (
<span className="tabular-nums text-neutral-400">
Resend in {resend.remaining}s
</span>
)}
</p>
</div>
</div>
);
},
);
 
OtpBoxedInput.displayName = "OtpBoxedInput";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/otp/otp-boxed-input.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.