Input OTP
A one-time code, one drawn box a character: typing moves on, backspace goes back, pasting fills them all.
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from "@/components/ui/input-otp";
export default function InputOTPDemo() {
return (
<InputOTP length={6} aria-label="One-time code" validationType="numeric">
<InputOTPGroup>
<InputOTPSlot index={0} seed="otp-0" />
<InputOTPSlot index={1} seed="otp-1" />
<InputOTPSlot index={2} seed="otp-2" />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={3} seed="otp-3" />
<InputOTPSlot index={4} seed="otp-4" />
<InputOTPSlot index={5} seed="otp-5" />
</InputOTPGroup>
</InputOTP>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ input-otp"use client";
import { useId, type ComponentProps } from "react";
import { OTPField as OTPFieldPrimitive } from "@base-ui/react/otp-field";
import { cn } from "@/lib/utils";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { InkGlyph } from "@/lib/ink-glyphs";
import { InkOutline } from "@/lib/ink-outline";
/**
* A one-time code, one drawn box a character. Typing moves on to the next box,
* backspace goes back, and pasting a whole code fills them all.
*/
function InputOTP({
className,
maxLength,
length,
children,
id: idProp,
"aria-label": label,
...props
}: Omit<OTPFieldPrimitive.Root.Props, "length"> & { length?: number; maxLength?: number }) {
// The first box takes the root's id, so a <label htmlFor> names the whole field.
// `aria-label` becomes one nobody sees.
const generated = useId();
const id = idProp ?? generated;
return (
<OTPFieldPrimitive.Root data-slot="input-otp" id={id} length={length ?? maxLength ?? 6} className={cn("flex items-center gap-2 has-[:disabled]:opacity-50", className)} {...props}>
{label && (
<label htmlFor={id} className="sr-only">
{label}
</label>
)}
{children}
</OTPFieldPrimitive.Root>
);
}
function InputOTPGroup({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="input-otp-group" className={cn("flex items-center gap-1.5", className)} {...props} />;
}
/** `index` is the box's place in the code: only its screen-reader name uses it (the first box takes the field's label). */
function InputOTPSlot({
index,
className,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: OTPFieldPrimitive.Input.Props & Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & { index?: number; seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
return (
<span data-slot="input-otp-slot" className="group/otp ink-within relative inline-flex size-11 items-center justify-center">
<InkOutline
pen={pen}
seed={seed}
estimate={[44, 44]}
focusPass
maxRadius={14}
className={cn(
"text-ink-3 transition-colors duration-(--dur-hover) group-hover/otp:text-ink group-focus-within/otp:text-ink",
"group-has-[[aria-invalid=true]]/otp:text-destructive group-has-[[data-invalid]]/otp:text-destructive",
)}
/>
<OTPFieldPrimitive.Input
aria-label={index ? `Character ${index + 1}` : undefined}
className={cn("relative size-full bg-transparent text-center text-xl text-foreground caret-ink outline-none disabled:cursor-not-allowed", className as string)}
{...props}
/>
</span>
);
}
function InputOTPSeparator({ className, ...props }: ComponentProps<"div">) {
return (
<div data-slot="input-otp-separator" role="separator" className={cn("flex items-center text-ink-3", className)} {...props}>
<InkGlyph name="minus" />
</div>
);
}
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator };Usage
import { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from "@/components/ui/input-otp"
<InputOTP length={6}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>Examples
Verifying a code
Controlled, with an invalid state when the code is wrong.
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field";
import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/input-otp";
export default function InputOTPVerify() {
const [code, setCode] = useState("");
const [status, setStatus] = useState<"idle" | "ok" | "wrong">("idle");
return (
<form
className="flex w-full max-w-sm flex-col gap-4"
onSubmit={(event) => {
event.preventDefault();
setStatus(code === "123456" ? "ok" : "wrong");
}}
>
<Field>
<FieldLabel htmlFor="otp-verify">Verification code</FieldLabel>
<InputOTP id="otp-verify" value={code} onValueChange={(next) => (setCode(next), setStatus("idle"))} length={6} validationType="numeric">
<InputOTPGroup>
{Array.from({ length: 6 }, (_, i) => (
<InputOTPSlot key={i} index={i} seed={`otpv-${i}`} aria-invalid={status === "wrong"} />
))}
</InputOTPGroup>
</InputOTP>
<FieldDescription>Enter the 6-digit code we sent you. (Try 123456.)</FieldDescription>
</Field>
<div className="flex items-center gap-4">
<Button type="submit" disabled={code.length < 6} seed="otpv-go">
Verify
</Button>
<p role="status" className={status === "wrong" ? "text-destructive" : "text-ink-2"}>
{status === "ok" ? "That's the one." : status === "wrong" ? "That code isn't right." : ""}
</p>
</div>
</form>
);
}API reference
- length (or maxLength)default
6 numberHow many characters. Add one InputOTPSlot for each.
- value / defaultValue
stringThe code so far, controlled or not.
- onValueChange / onValueComplete
(value: string) => voidCalled as it changes, and when every box is filled.
- validationTypedefault
"numeric" "numeric" | "alphanumeric" | "none"Which characters a box accepts.
- maskdefault
false booleanHide the characters as they are typed.
- seed
string | numberPins the drawing.
Every other prop goes to Base UI's OTP Field.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.