Input OTP

One-time password input with segmented fields.

Report a bug

Preview

Switch between light and dark to inspect the embedded Storybook preview.

Installation

pnpm dlx shadcn@latest add https://ui.vllnt.ai/r/input-otp.json

Storybook

Explore all variants, controls, and accessibility checks in the interactive Storybook playground.

View in Storybook

Code

"use client"; import { forwardRef, useContext } from "react"; import { OTPInput, OTPInputContext } from "input-otp"; import { Dot } from "lucide-react"; import { cn } from "../../lib/utils"; const InputOTP = forwardRef< React.ComponentRef<typeof OTPInput>, React.ComponentPropsWithoutRef<typeof OTPInput> >(({ className, containerClassName, ...props }, ref) => ( <OTPInput className={cn("disabled:cursor-not-allowed", className)} containerClassName={cn( "flex items-center gap-2 has-[:disabled]:opacity-50", containerClassName, )} ref={ref} {...props} /> )); InputOTP.displayName = "InputOTP"; const InputOTPGroup = forwardRef< React.ComponentRef<"div">, React.ComponentPropsWithoutRef<"div"> >(({ className, ...props }, ref) => ( <div className={cn("flex items-center", className)} ref={ref} {...props} /> )); InputOTPGroup.displayName = "InputOTPGroup"; const InputOTPSlot = forwardRef< React.ComponentRef<"div">, React.ComponentPropsWithoutRef<"div"> & { index: number } >(({ className, index, ...props }, ref) => { const inputOTPContext = useContext(OTPInputContext); const slot = inputOTPContext.slots[index]; if (!slot) { return null; } const { char, hasFakeCaret, isActive } = slot; return ( <div className={cn( "relative flex size-10 items-center justify-center border-y border-r border-input text-sm transition-all first:rounded-l-md first:border-l last:rounded-r-md", isActive && "z-10 ring-2 ring-ring ring-offset-background", className, )} ref={ref} {...props} > {char} {hasFakeCaret ? ( <div className="pointer-events-none absolute inset-0 flex items-center justify-center"> <div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" /> </div> ) : null} </div> ); }); InputOTPSlot.displayName = "InputOTPSlot"; const InputOTPSeparator = forwardRef< React.ComponentRef<"div">, React.ComponentPropsWithoutRef<"div"> >(({ ...props }, ref) => ( <div ref={ref} role="separator" {...props}> <Dot /> </div> )); InputOTPSeparator.displayName = "InputOTPSeparator"; export { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot };

Dependencies

  • @vllnt/ui@^0.2.1