Input OTP
A specialized input component for entering 2FA/one-time verification codes with individual slot digits and paste support.
inputOtp.tsx
Core implementation of the InputOTP component.
ReactInput OTPTailwindForms
"use client";
import * as React from "react";
import { OTPInput, OTPInputContext } from "input-otp";
import { Dot } from "lucide-react";
import { cn } from "@/lib/utils";
const InputOTP = React.forwardRef<
React.ComponentRef<typeof OTPInput>,
React.ComponentPropsWithoutRef<typeof OTPInput>
>(({ className, containerClassName, ...props }, ref) => (
<OTPInput
ref={ref}
containerClassName={cn(
"flex items-center gap-2 disabled:cursor-not-allowed",
containerClassName
)}
className={cn("disabled:cursor-not-allowed", className)}
{...props}
/>
));
InputOTP.displayName = "InputOTP";
const InputOTPGroup = React.forwardRef<
React.ElementRef<"div">,
React.ComponentPropsWithoutRef<"div">
>(({ className, ...props }, ref) => (
<div ref={ref} className={cn("flex items-center gap-1.5", className)} {...props} />
));
InputOTPGroup.displayName = "InputOTPGroup";
const InputOTPSlot = React.forwardRef<
React.ElementRef<"div">,
React.ComponentPropsWithoutRef<"div"> & { index: number }
>(({ index, className, ...props }, ref) => {
const inputOTPContext = React.useContext(OTPInputContext);
const slot = inputOTPContext.slots[index];
const { char, hasFakeCaret, isActive } = slot || {};
return (
<div
ref={ref}
className={cn(
"relative flex size-10 items-center justify-center rounded-xl border border-input bg-background text-sm font-semibold shadow-xs transition-all duration-200",
isActive && "z-10 border-primary ring-2 ring-primary/20",
className
)}
{...props}
>
{char}
{hasFakeCaret && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<div className="h-4 w-0.5 animate-caret-blink bg-foreground duration-1000" />
</div>
)}
</div>
);
});
InputOTPSlot.displayName = "InputOTPSlot";
const InputOTPSeparator = React.forwardRef<
React.ElementRef<"div">,
React.ComponentPropsWithoutRef<"div">
>(({ ...props }, ref) => (
<div ref={ref} role="separator" {...props}>
<Dot className="size-6 text-muted-foreground" />
</div>
));
InputOTPSeparator.displayName = "InputOTPSeparator";
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator };Basic Usage
6-digit verification code input.
Verification Code
API Reference
Props — InputOTP
Properties to configure the InputOTP component.
| Prop | Type | Default | Description |
|---|---|---|---|
| maxLength | number | — | Total length limit of the OTP passcode. |
| index | number | — | Zero-based slot index passed to InputOTPSlot. |