Primitives
Verification Code Input
One-time-code input with auto-advance, backspace, arrow keys, and paste distribution.
Motionnone
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import { VerificationCodeInput } from "kineto/verification-code-input";Preview
tsx
"use client";
import * as React from "react";
import { cn } from "@/lib/cn";
export interface VerificationCodeInputProps
extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
length?: number;
value?: string;
onValueChange?: (value: string) => void;
onComplete?: (value: string) => void;
disabled?: boolean;
}
const VerificationCodeInput = React.forwardRef<
HTMLDivElement,
VerificationCodeInputProps
>(
(
{ className, length = 6, value, onValueChange, onComplete, disabled, ...props },
ref
) => {
const [internal, setInternal] = React.useState("");
const code = (value ?? internal).slice(0, length);
const inputs = React.useRef<Array<HTMLInputElement | null>>([]);
const chars = Array.from({ length }, (_, i) => code[i] ?? "");
const focusAt = (i: number) =>
inputs.current[Math.max(0, Math.min(length - 1, i))]?.focus();
const commit = (next: string) => {
const v = next.slice(0, length);
if (value === undefined) setInternal(v);
onValueChange?.(v);
if (v.length === length) onComplete?.(v);
};
const handleChange = (i: number, raw: string) => {
const digit = raw.replace(/\D/g, "").slice(-1);
if (!digit) return;
const arr = [...chars];
arr[i] = digit;
commit(arr.join(""));
focusAt(i + 1);
};
const handleKeyDown = (
i: number,
e: React.KeyboardEvent<HTMLInputElement>
) => {
if (e.key === "Backspace") {
e.preventDefault();
const arr = [...chars];
if (arr[i]) {
arr[i] = "";
commit(arr.join(""));
} else {
arr[i - 1] = "";
commit(arr.join(""));
focusAt(i - 1);
}
} else if (e.key === "ArrowLeft") {
focusAt(i - 1);
} else if (e.key === "ArrowRight") {
focusAt(i + 1);
}
};
const handlePaste = (e: React.ClipboardEvent<HTMLInputElement>) => {
e.preventDefault();
const text = e.clipboardData.getData("text").replace(/\D/g, "").slice(0, length);
if (text) {
commit(text);
focusAt(text.length);
}
};
return (
<div ref={ref} className={cn("flex items-center gap-2", className)} {...props}>
{chars.map((c, i) => (
<input
key={i}
ref={(el) => {
inputs.current[i] = el;
}}
type="text"
inputMode="numeric"
autoComplete={i === 0 ? "one-time-code" : "off"}
maxLength={1}
disabled={disabled}
value={c}
onChange={(e) => handleChange(i, e.target.value)}
onKeyDown={(e) => handleKeyDown(i, e)}
onPaste={handlePaste}
className={cn(
"size-12 rounded-lg border bg-card text-center text-lg font-semibold text-foreground shadow-xs outline-none transition-colors focus:border-ring focus:ring-2 focus:ring-ring/40 disabled:opacity-50",
c ? "border-brand/60" : "border-input"
)}
/>
))}
</div>
);
}
);
VerificationCodeInput.displayName = "VerificationCodeInput";
export { VerificationCodeInput };src/components/ui/verification-code-input.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| length | number | 6 | Number of cells. |
| value / onValueChange | string / (v) => void | — | Controlled value. |
| onComplete | (code: string) => void | — | Fires when all cells filled. |