OTP Input
An OTP (One-Time Password) input component.
source cached 2026-10-19
src/components/ui/OTPinput.tsx
"use client";
import * as React from "react";
import { OTPInput, OTPInputContext } from "input-otp";
import { cn } from "@/stories/lib/utils";
import { cva, type VariantProps } from "class-variance-authority";
// Variant classes for OTP input slots
const inputOTPSlotVariants = cva(
"border border-input rounded-md relative flex items-center justify-center outline-none text-lg font-medium text-black dark:text-white border-b border-gray-300 transition-colors duration-200 underline decoration-cyan-400 underline-offset-4", // base styles
{
variants: {
Otpvariant: {
sm: "w-8 h-8 text-sm",
md: "w-10 h-10 text-base",
lg: "w-12 h-12 text-lg",
xl: "w-14 h-14 text-xl",
},
},
defaultVariants: {
Otpvariant: "md",
},
},
);
// Wrapper around OTPInput to apply common classes
function InputOTP({
className,
containerClassName,
...props
}: React.ComponentProps<typeof OTPInput> & {
containerClassName?: string;
}) {
return (
<OTPInput
data-slot="input-otp"
containerClassName={cn(
"flex items-center gap-2 has-disabled:opacity-50",
containerClassName,
)}
className={cn("disabled:cursor-not-allowed", className)}
{...props}
/>
);
}
// Group wrapper for slots
function InputOTPGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="input-otp-group"
className={cn("flex items-center space-x-2", className)}
{...props}
/>
);
}
// Slot component with support for variants
type InputOTPSlotProps = React.ComponentProps<"div"> &
VariantProps<typeof inputOTPSlotVariants> & {
index: number;
};
function InputOTPSlot({
index,
className,
Otpvariant,
...props
}: InputOTPSlotProps) {
const inputOTPContext = React.useContext(OTPInputContext);
const { char, hasFakeCaret, isActive } = inputOTPContext?.slots[index] ?? {};
const hasValue = !!char;
return (
<div
data-slot="input-otp-slot"
data-active={isActive}
className={cn(
inputOTPSlotVariants({ Otpvariant }),
className,
// Always apply base underline style
"border-b border-gray-300",
hasValue ? "border-b-1 border-cyan-400" : "border-b border-gray-300",
// Apply active underline when focused/active
isActive && "border-b-[1.5px] border-cyan-400",
)}
{...props}
>
{char}
{hasFakeCaret && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center ">
<div className="animate-caret-blink bg-black h-4 w-px " />
</div>
)}
</div>
);
}
export { InputOTP, InputOTPGroup, InputOTPSlot };
No selection tags yet — empty means no evidence, not lack of support.