INFINITYUI
HomeComponentsDocsTemplates
Star

Getting Started

  • Introduction
  • Installation
  • CLI
  • Audit

Navigation

  • Spotlight Navbar
  • Glass DockNEW
  • Animated Tab Bar
  • Circle Menu
  • Magnet Tabs
  • Animated Sidebar
  • Apple Spotlight
  • Page TOC RailNEW

Text

  • Flip Text
  • Glitch Text
  • Liquid Text
  • Flip Fade Text
  • Mask Cursor Effect

Cards

  • Glow Border Card
  • Testimonials Card
  • Interactive Book
  • Trading CardsNEW
  • Hover Image
  • Chain of ThoughtNEW
  • Masonry Grid
  • Image Pile
  • Staggered Grid

Inputs

  • AI InputNEW
  • OTP Input
  • Leave Rating

Buttons

  • Social Flip Button
  • Creepy Button

Loaders

  • Jelly Loader
  • Rolling Ball Scroll
  • Glowing Scroll

Backgrounds

  • Light Lines
  • Perspective Grid
  • Liquid Ocean
  • Eagle Vision
  • Flow Scroll
  • Horizontal Scroll

Overlays

  • PersonaNEW
  • Infinite Moving Cards
  • Masked Avatars
  • Stacked Logos
  • Icon Wheel
  • Pixelated CarouselNEW
  • Pixelated Image Trail
  • Flip Scroll
  • Interactive Folder
  • Animated Folder IconNEW
  • Stack Scroll
  • Rubik Cube
Inputs

OTP Input

#input#otp#form

Enter your code

Check your inbox for the 6-digit code

Try pasting: 123456

implementedInfinityUI component

This component is fully implemented in InfinityUI and wired into the docs and registry flow.

Installation

Use the registry command to add the component source, and install any package dependencies if needed.

Infinity Registry

Install the component source into your project with the shadcn CLI.

npx shadcn@latest add https://infinityui-pearl.vercel.app/r/otp-input
Package Dependencies

Install the npm packages used by this component source.

No additional npm packages required.

Component Code

Copy and paste this code into your component file.

tsx
"use client";

import { useRef, useState, KeyboardEvent, ClipboardEvent } from "react";

interface OTPInputProps {
    length?: number;
    onComplete?: (otp: string) => void;
    className?: string;
}

export function OTPInput({ length = 6, onComplete, className = "" }: OTPInputProps) {
    const [otp, setOtp] = useState<string[]>(Array(length).fill(""));
    const [error, setError] = useState(false);
    const inputs = useRef<(HTMLInputElement | null)[]>([]);

    const handleChange = (index: number, value: string) => {
        if (!/^\d*$/.test(value)) return;
        const newOtp = [...otp];
        newOtp[index] = value.slice(-1);
        setOtp(newOtp);
        setError(false);

        if (value && index < length - 1) {
            inputs.current[index + 1]?.focus();
        }
        if (newOtp.every(Boolean)) {
            onComplete?.(newOtp.join(""));
        }
    };

    const handleKeyDown = (index: number, e: KeyboardEvent<HTMLInputElement>) => {
        if (e.key === "Backspace" && !otp[index] && index > 0) {
            inputs.current[index - 1]?.focus();
        }
    };

    const handlePaste = (e: ClipboardEvent<HTMLInputElement>) => {
        e.preventDefault();
        const pasted = e.clipboardData.getData("text").replace(/\D/g, "").slice(0, length);
        const newOtp = [...otp];
        pasted.split("").forEach((char, i) => { newOtp[i] = char; });
        setOtp(newOtp);
        inputs.current[Math.min(pasted.length, length - 1)]?.focus();
        if (pasted.length === length) onComplete?.(pasted);
    };

    const isComplete = otp.every(Boolean);

    return (
        <div className={`flex flex-col items-center gap-4 ${className}`}>
            <div className="flex items-center gap-2">
                {otp.map((val, i) => (
                    <input
                        key={i}
                        ref={(el) => { inputs.current[i] = el; }}
                        type="text"
                        inputMode="numeric"
                        maxLength={1}
                        value={val}
                        onChange={(e) => handleChange(i, e.target.value)}
                        onKeyDown={(e) => handleKeyDown(i, e)}
                        onPaste={handlePaste}
                        aria-label={`Digit ${i + 1} of ${length}`}
                        className={`w-12 h-14 text-center text-xl font-bold rounded-xl border-2 bg-zinc-900 text-white transition-all duration-200 outline-none focus:scale-105 caret-transparent ${isComplete
                                ? "border-emerald-500 text-emerald-400 shadow-[0_0_15px_rgba(52,211,153,0.3)]"
                                : val
                                    ? "border-indigo-500 shadow-[0_0_10px_rgba(99,102,241,0.3)]"
                                    : "border-zinc-700 hover:border-zinc-500 focus:border-indigo-500"
                            }`}
                    />
                ))}
            </div>

            {isComplete && (
                <p className="text-emerald-400 text-sm font-medium animate-in fade-in slide-in-from-bottom-2 duration-300">
                    ✓ Code verified
                </p>
            )}
        </div>
    );
}