PinInput
SM · 950B gzip budgetSeparate digit fields for PIN entry with auto-focus forwarding.
- Category
- Forms & input
- Budget
- sm, single-purpose control
- External runtime
- React / React DOM only
axe fixtureSSR rendersource scanbundle budget
Preview
Live fixtureTry the component here. Locale-aware formatting and direction follow the selected language; example content and labels are not automatically translated. Overlay demos use interactive launchers.
Install
npx shadcn@latest add https://gear5-ui.vercel.app/r/pin-input.jsonCopies the source into your project. Pulls in 1 shared registry item: cn.
New here? Set up Tailwind and import aliases first →Source & props
Edit on GitHub ↗Prop types and inline documentation are included below. This is repository source; the installer rewrites shared imports for your project.
"use client";
import { useId, useRef } from "react";
import { cn } from "../lib/cn";
export interface PinInputProps {
value: string;
onChange: (value: string) => void;
length?: number;
label: string;
className?: string;
}
export function PinInput({ value, onChange, length = 6, label, className }: PinInputProps) {
const id = useId();
const refs = useRef<Array<HTMLInputElement | null>>([]);
const setDigit = (index: number, digit: string) => {
const next = value.split("");
next[index] = digit;
onChange(next.join("").slice(0, length));
if (digit && index < length - 1) refs.current[index + 1]?.focus();
};
return (
<fieldset className={cn("flex flex-col gap-1.5 text-start", className)}>
<legend className="text-sm font-medium">{label}</legend>
<div className="flex gap-2">
{Array.from({ length }, (_, index) => (
<input
key={index}
ref={(node) => {
refs.current[index] = node;
}}
id={`${id}-${index}`}
inputMode="numeric"
autoComplete="one-time-code"
maxLength={1}
aria-label={`Digit ${index + 1} of ${length}`}
value={value[index] ?? ""}
onChange={(event) => setDigit(index, event.target.value.replace(/\D/g, "").slice(-1))}
onKeyDown={(event) => {
if (event.key === "Backspace" && !value[index] && index > 0) {
refs.current[index - 1]?.focus();
}
}}
onPaste={(event) => {
event.preventDefault();
onChange(event.clipboardData.getData("text").replace(/\D/g, "").slice(0, length));
}}
className="size-11 rounded-md border border-neutral-300 text-center text-lg focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 dark:border-neutral-700 dark:bg-neutral-950"
/>
))}
</div>
<p className="text-xs text-neutral-500 dark:text-neutral-400">
Enter {length} digits
</p>
</fieldset>
);
}
What CI checks
Quality contract- Bundled, minified and gzipped against its tier budget
- Audited by axe in the state previewed above
- Rendered through react-dom/server with no browser globals
- Scanned for network calls, dangerous sinks, and unguarded animation