OTP Input
OTP Input for a useful, keyboard-friendly product interface.
Make it yours.
Install the editable source and its dependencies into your React + Tailwind project.
Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/otp-input.jsonUse it
Scroll horizontally for long lines
"use client";
import { OtpInput } from "@/components/jez-ui/ui/otp-input";
export default function Example() {
return (
<>
<div className="w-full max-w-sm">
<div className="mb-7">
<h3 className="text-2xl tracking-tight">A quick check.</h3>
<p className="mt-2 text-sm leading-relaxed text-muted-foreground">
Enter the six-digit code to continue this demo.
</p>
</div>
<OtpInput />
</div>
</>
);
}
Props & types
Scroll horizontally for long lines
Omit<
React.ComponentProps<"label">,
keyof {
label?: string;
inputLabel?: string;
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
length?: number;
className?: string;
}
> & {
label?: string;
inputLabel?: string;
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
length?: number;
className?: string;
}Source
View 3 source files
registry/ui/otp-input.tsx
Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
import { useControllable } from "./use-controllable";
export function OtpInput({
children,
label = "Verification code",
inputLabel,
value,
defaultValue = "",
onValueChange,
length = 6,
className,
...rootProps
}: Omit<
React.ComponentProps<"label">,
keyof {
label?: string;
inputLabel?: string;
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
length?: number;
className?: string;
}
> & {
label?: string;
inputLabel?: string;
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
length?: number;
className?: string;
}) {
const [code, setCode] = useControllable(value, defaultValue, onValueChange);
return (
<label {...rootProps} className={cn("grid gap-2 text-sm", className)}>
{children !== undefined ? (
children
) : (
<>
{label}
<input
aria-label={inputLabel ?? `${length}-digit verification code`}
inputMode="numeric"
autoComplete="one-time-code"
pattern={`[0-9]{${length}}`}
maxLength={length}
value={code}
onChange={(e) =>
setCode(e.target.value.replace(/\D/g, "").slice(0, length))
}
className="w-full max-w-72 rounded-xl border border-border bg-background px-4 py-3 text-center font-mono text-2xl tracking-[.4em]"
/>
</>
)}
</label>
);
}
registry/ui/utils.ts
Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
return twMerge(clsx(values));
}
registry/ui/use-controllable.ts
Scroll horizontally for long lines
"use client";
import { useRef, useState, type SetStateAction } from "react";
/** Controlled or instance-local state. Functional updates also work before a rerender. */
export function useControllable<T>(
value: T | undefined,
initial: T,
onChange?: (value: T) => void,
) {
const [local, setLocal] = useState(initial);
const current = useRef(value === undefined ? local : value);
current.current = value === undefined ? local : value;
return [
current.current,
(action: SetStateAction<T>) => {
const next =
typeof action === "function"
? (action as (previous: T) => T)(current.current)
: action;
current.current = next;
if (value === undefined) setLocal(next);
onChange?.(next);
},
] as const;
}
Dependencies
clsx@2.1.1 · tailwind-merge@3.5.0
Accessibility & behaviour
Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.
Read the accessibility and performance guide →