Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/input-otp.jsonFirst component? Set up your project first
Frameworks
The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.
"use client";
import * as React from "react";
import { motion, useReducedMotion } from "motion/react";
import { cn } from "@/lib/utils";
export type InputOTPProps = {
length?: number;
defaultValue?: string;
onChange?: (value: string) => void;
onComplete?: (value: string) => void;
className?: string;
};
const pop = { type: "spring", stiffness: 500, damping: 24 } as const;
export function InputOTP({
length = 6,
defaultValue = "",
onChange,
onComplete,
className,
}: InputOTPProps) {
const [values, setValues] = React.useState<string[]>(() =>
Array.from({ length }, (_, index) => defaultValue[index] ?? ""),
);
const refs = React.useRef<(HTMLInputElement | null)[]>([]);
const reduce = useReducedMotion();
const commit = (next: string[]) => {
setValues(next);
const value = next.join("");
onChange?.(value);
if (value.length === length) onComplete?.(value);
};
const write = (index: number, text: string) => {
const chars = text.replace(/\D/g, "").slice(0, length - index).split("");
if (!chars.length) return;
const next = [...values];
chars.forEach((char, offset) => {
next[index + offset] = char;
});
commit(next);
const focus = Math.min(length - 1, index + chars.length);
refs.current[focus]?.focus();
};
return (
<div className={cn("flex items-center gap-2", className)} role="group" aria-label="One-time code">
{values.map((value, index) => (
<motion.input
key={index}
ref={(node) => {
refs.current[index] = node;
}}
value={value}
inputMode="numeric"
autoComplete="one-time-code"
maxLength={1}
aria-label={`Digit ${index + 1}`}
initial={false}
animate={{ scale: reduce ? 1 : value ? 1 : 0.97 }}
transition={pop}
onChange={(event) => {
const digits = event.target.value.replace(/\D/g, "");
if (!digits) {
const next = [...values];
next[index] = "";
commit(next);
return;
}
write(index, digits);
const input = event.target;
requestAnimationFrame(() => input.blur());
}}
onKeyDown={(event) => {
if (event.key === "Backspace") {
event.preventDefault();
const next = [...values];
if (next[index]) {
next[index] = "";
commit(next);
} else if (index > 0) {
next[index - 1] = "";
commit(next);
refs.current[index - 1]?.focus();
}
}
}}
onPaste={(event) => {
event.preventDefault();
write(index, event.clipboardData.getData("text"));
}}
className="size-10 rounded-lg border bg-background text-center font-mono text-[15px] outline-none focus-visible:ring-2 focus-visible:ring-ring"
/>
))}
</div>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/input-otp.jsonDependencies
motion lucide-reactThe registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.
Usage
Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.
import { InputOTP } from "@/components/prequelui/input-otp";
export default function Demo() {
return (
<InputOTP>
{/* compose the parts exactly like the underlying primitive */}
</InputOTP>
);
}import { InputOTP } from "@/components/prequelui/input-otp";
export function Variant() {
return (
<InputOTP tempo={1.2}>
…
</InputOTP>
);
}Animation
This primitive keeps the API and semantics of the underlying component and adds motion at the surface edges. Entrances are slower and more settled than exits, so closing never feels like waiting.
- Springs for position, scale and size; tweens for colour and opacity.
- The exit is shorter than the entrance, and keeps the current position as its starting point.
- All measurements are taken again when content or font metrics change.
- With reduced motion, the surface fades in place and never travels.
API Reference
Every part takes exactly the props of the Input OTP primitive. The animated surface adds only motion props, so the accessibility tree and keyboard behaviour stay the same.
Input OTP
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | — |
| disabled | boolean | false |
| transition | Transition | spring.press |
| className | string | — |