Preview
"use client";
import { useState } from "react";
import { OTPInput, type OTPStatus } from "@/components/motion/otp-input";
const CODE = "123456";
export function OTPInputPreview() {
const [value, setValue] = useState("");
const [status, setStatus] = useState<OTPStatus>("idle");
return (
<div className="flex flex-col items-center gap-4">
<OTPInput
label="Verification code"
hint={`Enter ${CODE} to verify.`}
successMessage="Verified."
errorMessage="Wrong code, try again."
value={value}
status={status}
onChange={(v) => {
setValue(v);
if (status !== "idle") setStatus("idle");
}}
onComplete={(v) => setStatus(v === CODE ? "success" : "error")}
/>
</div>
);
}
"use client";
// beui.dev/components/blocks/otp-input
import {
AnimatePresence,
animate,
motion,
useReducedMotion,
} from "motion/react";
import { useEffect, useId, useRef, useState } from "react";
import { EASE_OUT, SPRING_PRESS } from "@/lib/ease";
import { useMessages } from "@/lib/i18n";
import { cn } from "@/lib/utils";
/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */
export const otpInputMessages = {
label: "One-time passcode",
progress: "{entered} of {total} digits entered.",
entered: "{digit}. {progress}",
maskedDigit: "Digit",
deleted: "Deleted. {progress}",
position: "{slot}, digit {position} of {total}.",
filled: "Filled",
empty: "Empty",
verified: "Verified",
};
export type OTPInputMessages = typeof otpInputMessages;
export type OTPStatus = "idle" | "error" | "success";
export interface OTPInputProps {
/** Number of slots. Default 6. */
length?: number;
/** The entered digits, for controlled use. */
value?: string;
/** The initial digits when uncontrolled. Default: "" */
defaultValue?: string;
/** Called with the entered digits whenever a slot changes. */
onChange?: (value: string) => void;
/** Fires once every slot is filled. */
onComplete?: (value: string) => void;
/** Optional label rendered above the slots. */
label?: string;
/** Helper text shown below the slots while idle. */
hint?: string;
/** Message shown below the slots when status is "success". */
successMessage?: string;
/** Message shown below the slots when status is "error". */
errorMessage?: string;
/** External validation feedback. "error" shakes, "success" draws a check. */
status?: OTPStatus;
/** Render dots instead of the typed digits. */
mask?: boolean;
/** Block typing and dim the slots. Default: false */
disabled?: boolean;
/** Focus the input on mount. Default: false */
autoFocus?: boolean;
/** Accessible label for the underlying input. */
"aria-label"?: string;
/** Override any built-in string. */
labels?: Partial<OTPInputMessages>;
/** Classes merged onto the root element. */
className?: string;
}
export function OTPInput({
length = 6,
value: controlledValue,
defaultValue = "",
onChange,
onComplete,
label,
hint,
successMessage,
errorMessage,
status = "idle",
mask = false,
disabled = false,
autoFocus = false,
"aria-label": ariaLabelProp,
labels,
className,
}: OTPInputProps) {
const t = useMessages("otpInput", otpInputMessages, labels);
const ariaLabel = ariaLabelProp ?? t("label");
const uid = useId();
const reduce = useReducedMotion();
const inputRef = useRef<HTMLInputElement>(null);
const slotsRef = useRef<HTMLDivElement>(null);
const controlled = controlledValue !== undefined;
// Source of truth is a fixed-length array, so a cleared middle slot stays an
// in-place hole instead of collapsing the digits after it to the left.
const [slots, setSlots] = useState<string[]>(() =>
toSlots(controlled ? controlledValue : defaultValue, length),
);
const [focused, setFocused] = useState(false);
const [active, setActive] = useState(0);
// The native input holds no value, so screen readers cannot echo what was
// typed. This polite region speaks each change instead; the alternating
// trailing space makes a repeated phrase count as new text.
const [announcement, setAnnouncement] = useState("");
const announce = (text: string) =>
setAnnouncement((previous) =>
previous === text ? `${text}\u00a0` : text,
);
const spokenSlot = (char: string) =>
char ? (mask ? t("filled") : char) : t("empty");
const joined = slots.join("");
const joinedRef = useRef(joined);
useEffect(() => {
joinedRef.current = joined;
}, [joined]);
// Pull in external value changes; skip when the parent is just echoing our own
// onChange, so internal holes survive the controlled round-trip.
useEffect(() => {
if (!controlled) return;
const incoming = sanitize(controlledValue, length);
if (incoming !== joinedRef.current) setSlots(toSlots(incoming, length));
}, [controlled, controlledValue, length]);
const commit = (next: string[]) => {
const wasComplete = slots.every((c) => c !== "");
const changed = next.flatMap((c, i) => (c !== slots[i] ? [i] : []));
const progress = t("progress", {
entered: next.filter(Boolean).length,
total: length,
});
if (changed.length === 1) {
const char = next[changed[0]];
announce(
char
? t("entered", { digit: mask ? t("maskedDigit") : char, progress })
: t("deleted", { progress }),
);
} else if (changed.length > 1) {
announce(progress);
}
setSlots(next);
const str = next.join("");
onChange?.(str);
// Fire only on the empty→full transition, not on every edit of a full code.
if (!wasComplete && next.every((c) => c !== "")) onComplete?.(str);
};
const clearSlot = (idx: number) => {
const next = [...slots];
next[idx] = "";
commit(next);
};
const slotFromClientX = (clientX: number) => {
const els = slotsRef.current?.children;
if (!els) return 0;
for (let i = 0; i < els.length; i++) {
if (clientX < els[i].getBoundingClientRect().right) return i;
}
return length - 1;
};
// Single insertion path: one digit overwrites the active slot and advances; a
// multi-digit chunk (paste / SMS autofill) fills forward from the active slot.
const insert = (raw: string, from = active) => {
const digits = raw.replace(/\D/g, "");
if (!digits) return;
const next = [...slots];
let i = from;
for (const ch of digits) {
if (i >= length) break;
next[i] = ch;
i++;
}
commit(next);
setActive(Math.min(i, length - 1));
};
// The keyboard is the single source of truth: preventDefault on keydown
// reliably blocks native insertion (unlike beforeinput in React), so the hidden
// input never accumulates a competing string and slot holes survive.
const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (disabled || e.metaKey || e.ctrlKey || e.altKey) return;
const k = e.key;
if (/^[0-9]$/.test(k)) {
e.preventDefault();
insert(k);
} else if (k === "Backspace") {
e.preventDefault();
// A filled slot clears in place; an empty slot steps back and clears there.
if (slots[active]) {
clearSlot(active);
} else if (active > 0) {
clearSlot(active - 1);
setActive((current) => Math.max(current - 1, 0));
}
} else if (k === "Delete") {
e.preventDefault();
clearSlot(active);
} else if (
k === "ArrowLeft" ||
k === "ArrowRight" ||
k === "Home" ||
k === "End"
) {
e.preventDefault();
const to =
k === "ArrowLeft"
? Math.max(active - 1, 0)
: k === "ArrowRight"
? Math.min(active + 1, length - 1)
: k === "Home"
? 0
: length - 1;
setActive(to);
announce(
t("position", {
slot: spokenSlot(slots[to] ?? ""),
position: to + 1,
total: length,
}),
);
}
};
const onPaste = (e: React.ClipboardEvent<HTMLInputElement>) => {
if (disabled) return;
// preventDefault suppresses the duplicate onChange, keeping that path autofill-only.
e.preventDefault();
insert(e.clipboardData.getData("text"), active);
};
// Autofill path: SMS one-time-code arrives as a whole value in one shot.
// Keystrokes go through onKeyDown and paste through onPaste, so only autofill
// reaches here - spread it across the slots from the start.
const onChangeNative = (e: React.ChangeEvent<HTMLInputElement>) => {
const digits = sanitize(e.target.value, length);
if (!digits) return;
commit(toSlots(digits, length));
setActive(Math.min(digits.length, length - 1));
};
// Error shake - imperative so it replays on every transition into "error".
useEffect(() => {
if (status !== "error" || reduce || !slotsRef.current) return;
animate(
slotsRef.current,
{ x: [0, -5, 5, -3, 3, -1, 0] },
{ duration: 0.45, ease: EASE_OUT },
);
}, [status, reduce]);
const showSuccess = status === "success";
const activeIndex = focused ? active : -1;
const message = showSuccess
? successMessage
: status === "error"
? errorMessage
: hint;
return (
<div className={cn("inline-flex flex-col gap-2", className)}>
{label ? (
<label
htmlFor={`${uid}-input`}
className="text-sm font-medium text-foreground"
>
{label}
</label>
) : null}
<fieldset
className="relative m-0 inline-flex w-max border-0 p-0"
onMouseDown={(e) => {
if (disabled) return;
// Suppress the native click-caret; we drive the active slot ourselves.
e.preventDefault();
// Clamp to the first empty slot so a click can't jump ahead of progress.
const firstEmpty = slots.indexOf("");
const cap = firstEmpty === -1 ? length - 1 : firstEmpty;
setActive(Math.min(slotFromClientX(e.clientX), cap));
inputRef.current?.focus();
}}
>
<input
ref={inputRef}
id={`${uid}-input`}
inputMode="numeric"
autoComplete="one-time-code"
// biome-ignore lint/a11y/noAutofocus: opt-in via prop for OTP-first screens.
autoFocus={autoFocus}
disabled={disabled}
aria-label={ariaLabel}
aria-invalid={status === "error"}
// Kept empty on purpose - our state owns the digits, native holds none.
value=""
maxLength={length}
onKeyDown={onKeyDown}
onChange={onChangeNative}
onPaste={onPaste}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
// Transparent overlay owns focus, the soft keyboard, paste and autofill;
// the slots below are purely presentational.
className="absolute inset-0 z-20 h-full w-full cursor-text bg-transparent text-transparent caret-transparent opacity-0 outline-none disabled:cursor-not-allowed"
/>
<div ref={slotsRef} className="flex items-center gap-2">
{Array.from({ length }, (_, i) => {
const char = slots[i] ?? "";
const isActive = i === activeIndex;
return (
<div
// biome-ignore lint/suspicious/noArrayIndexKey: fixed-length slot grid, never reordered.
key={`${uid}-${i}`}
data-active={isActive}
data-filled={char !== ""}
className={cn(
"relative grid h-14 w-12 place-items-center overflow-hidden rounded-full border text-xl font-medium tabular-nums transition-colors duration-200",
showSuccess
? "border-emerald-500/60 text-foreground"
: status === "error"
? "border-destructive/60 text-foreground"
: char
? "border-border-strong text-foreground"
: "border-border text-muted-foreground",
// Active slot reads stronger; twMerge lets this win the border.
isActive && !showSuccess && status !== "error" && "border-foreground",
disabled && "opacity-50",
)}
>
{/* Blinking caret marks the active slot - centered when empty,
trailing the digit when the slot is already filled. */}
{isActive && !showSuccess ? (
<motion.span
aria-hidden
animate={reduce ? undefined : { opacity: [1, 1, 0, 0] }}
transition={
reduce
? undefined
: { duration: 1, repeat: Number.POSITIVE_INFINITY, ease: "linear" }
}
className={cn(
"pointer-events-none absolute top-1/2 h-6 w-px -translate-y-1/2 bg-foreground",
char ? "right-3" : "left-1/2 -translate-x-1/2",
)}
/>
) : null}
{/* Digits roll vertically. Each is absolutely centered so enter and
exit overlap in place - no in-flow reflow, no sideways drift. */}
<AnimatePresence initial={false}>
{char ? (
<motion.span
key={char}
initial={
reduce
? { opacity: 0 }
: { y: 14, opacity: 0, filter: "blur(4px)" }
}
animate={
reduce
? { opacity: 1 }
: { y: 0, opacity: 1, filter: "blur(0px)" }
}
exit={
reduce
? { opacity: 0 }
: { y: -14, opacity: 0, filter: "blur(4px)" }
}
transition={
reduce
? { duration: 0 }
: { duration: 0.22, ease: EASE_OUT }
}
className="absolute inset-0 grid place-items-center leading-none"
>
{mask ? "•" : char}
</motion.span>
) : null}
</AnimatePresence>
</div>
);
})}
</div>
<AnimatePresence>
{showSuccess ? (
<motion.span
initial={reduce ? { opacity: 0 } : { scale: 0.6, opacity: 0 }}
animate={reduce ? { opacity: 1 } : { scale: 1, opacity: 1 }}
exit={reduce ? { opacity: 0 } : { scale: 0.6, opacity: 0 }}
transition={
reduce
? { duration: 0 }
: SPRING_PRESS
}
className="pointer-events-none absolute -right-7 top-1/2 -translate-y-1/2 text-emerald-500"
aria-hidden
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={3}
strokeLinecap="round"
strokeLinejoin="round"
>
<title>{t("verified")}</title>
<motion.path
d="M5 13l4 4L19 7"
initial={reduce ? { pathLength: 1 } : { pathLength: 0 }}
animate={{ pathLength: 1 }}
transition={
reduce
? { duration: 0 }
: { duration: 0.35, ease: EASE_OUT, delay: 0.1 }
}
/>
</svg>
</motion.span>
) : null}
</AnimatePresence>
</fieldset>
<span aria-live="polite" aria-atomic="true" className="sr-only">
{announcement}
</span>
{message ? (
<p
aria-live="polite"
className={cn(
"text-sm",
showSuccess
? "text-emerald-500"
: status === "error"
? "text-destructive"
: "text-muted-foreground",
)}
>
{message}
</p>
) : null}
</div>
);
}
function sanitize(raw: string, length: number) {
return raw.replace(/\D/g, "").slice(0, length);
}
function toSlots(raw: string, length: number) {
const digits = sanitize(raw, length);
return Array.from({ length }, (_, i) => digits[i] ?? "");
}
Add it with the shadcn CLI, or copy the source manually.
npx shadcn add https://beui.dev/r/theme.json. Theme setupInstall dependencies
npm i clsx motion tailwind-mergeAdd util files
// Shared motion tokens. Easing curves mirror the CSS custom properties in
// globals.css; springs are the canonical physics used across components.
// Strong custom variants - defaults like `ease-in`/`ease-out` feel weak.
export const EASE_OUT = [0.16, 1, 0.3, 1] as const;
/** Accelerating curve for things that fall (a dropped ball, a toppled block). Not for UI entrances. */
export const EASE_IN = [0.55, 0, 1, 0.45] as const;
export const EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;
export const EASE_DRAWER = [0.32, 0.72, 0, 1] as const;
/** CSS string form of EASE_OUT for inline style transitions. */
export const EASE_OUT_CSS = "cubic-bezier(0.16, 1, 0.3, 1)";
/** Press feedback on buttons and other tappable surfaces. */
export const SPRING_PRESS = {
type: "spring",
stiffness: 500,
damping: 30,
mass: 0.6,
} as const;
/** Content swaps - label/icon slots trading places inside a control. */
export const SPRING_SWAP = {
type: "spring",
stiffness: 460,
damping: 30,
mass: 0.55,
} as const;
/** Overlay panel entrances - modals and sheets summoned by pointer. */
export const SPRING_PANEL = {
type: "spring",
stiffness: 420,
damping: 40,
mass: 0.5,
} as const;
/** Shared-layout glides - pills, indicators and panels morphing between positions. */
export const SPRING_LAYOUT = {
type: "spring",
stiffness: 360,
damping: 32,
mass: 0.6,
} as const;
/** Cursor-follow physics for decorative mouse tracking (magnetic, tilt, dock). */
export const SPRING_MOUSE = {
stiffness: 200,
damping: 15,
mass: 0.3,
} as const;
/** Dragged handles and fills (sliders) - critically damped `useSpring` config,
* so the value follows the pointer butterily and never rebounds off an end. */
export const SPRING_GLIDE = {
stiffness: 700,
damping: 50,
mass: 0.5,
} as const;
/** Shared-surface morphs: a trigger growing into its panel and back on one
* weighted spring, so opening and closing travel the same path. */
export const SPRING_MORPH = {
type: "spring",
duration: 0.5,
bounce: 0.22,
} as const;
"use client";
import {
createContext,
type ReactNode,
useCallback,
useContext,
useMemo,
} from "react";
/**
* A component's messages. Plain strings with `{name}` placeholders, so a
* dictionary stays JSON-serializable and can be passed from a server layout.
* There are no plural forms: write messages that read correctly for any count.
*/
export type Messages = Record<string, string>;
/** A language file: component namespace → any subset of that component's messages. */
export type LocaleMessages = Record<string, Partial<Messages>>;
type LocaleContextValue = {
locale?: string;
messages: LocaleMessages;
};
const NO_MESSAGES: LocaleMessages = {};
const LocaleContext = createContext<LocaleContextValue>({ messages: NO_MESSAGES });
/** Without a locale, digits stay plain so server and client render the same text. */
export function formatNumber(value: number, locale?: string) {
return locale ? new Intl.NumberFormat(locale).format(value) : String(value);
}
export interface LocaleProviderProps {
/** BCP 47 tag, e.g. `de`, `pt-BR`. Drives `Intl` number formatting. */
locale: string;
/**
* Component namespace → translated strings. Missing keys fall back to English.
* Pass a stable object (a module constant, imported JSON or a memoized value):
* a new object each render gives every translated component a new `t`.
*/
messages?: LocaleMessages;
children: ReactNode;
}
export function LocaleProvider({ locale, messages = NO_MESSAGES, children }: LocaleProviderProps) {
const value = useMemo(() => ({ locale, messages }), [locale, messages]);
return <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>;
}
export function useLocale(): { locale?: string } {
return { locale: useContext(LocaleContext).locale };
}
/**
* Resolves a component's strings: `labels` prop, then the provider's
* messages for `namespace`, then the component's English `defaults`.
* Numbers passed as values are formatted for the provider's locale.
*/
export function useMessages<T extends Messages>(
namespace: string,
defaults: T,
labels?: Partial<T>,
) {
const { locale, messages } = useContext(LocaleContext);
const provided = messages[namespace];
// Inline `labels={{ … }}` is a new object every render. Keying it by content
// keeps `t` one identity until a label changes, so effects and memos that
// depend on `t` don't re-run per render, and render-time reads never lag.
const labelsKey = labels ? JSON.stringify(labels) : "";
const stableLabels = useMemo(
() => (labelsKey ? (JSON.parse(labelsKey) as Partial<T>) : undefined),
[labelsKey],
);
return useCallback(
(key: keyof T & string, values?: Record<string, string | number>) => {
const template = stableLabels?.[key] ?? provided?.[key] ?? defaults[key];
if (!values) return template;
return template.replace(/\{(\w+)\}/g, (match, name: string) => {
const value = values[name];
if (value === undefined) return match;
return typeof value === "number" ? formatNumber(value, locale) : value;
});
},
[defaults, stableLabels, locale, provided],
);
}
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Copy the source code
"use client";
// beui.dev/components/blocks/otp-input
import {
AnimatePresence,
animate,
motion,
useReducedMotion,
} from "motion/react";
import { useEffect, useId, useRef, useState } from "react";
import { EASE_OUT, SPRING_PRESS } from "@/lib/ease";
import { useMessages } from "@/lib/i18n";
import { cn } from "@/lib/utils";
/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */
export const otpInputMessages = {
label: "One-time passcode",
progress: "{entered} of {total} digits entered.",
entered: "{digit}. {progress}",
maskedDigit: "Digit",
deleted: "Deleted. {progress}",
position: "{slot}, digit {position} of {total}.",
filled: "Filled",
empty: "Empty",
verified: "Verified",
};
export type OTPInputMessages = typeof otpInputMessages;
export type OTPStatus = "idle" | "error" | "success";
export interface OTPInputProps {
/** Number of slots. Default 6. */
length?: number;
/** The entered digits, for controlled use. */
value?: string;
/** The initial digits when uncontrolled. Default: "" */
defaultValue?: string;
/** Called with the entered digits whenever a slot changes. */
onChange?: (value: string) => void;
/** Fires once every slot is filled. */
onComplete?: (value: string) => void;
/** Optional label rendered above the slots. */
label?: string;
/** Helper text shown below the slots while idle. */
hint?: string;
/** Message shown below the slots when status is "success". */
successMessage?: string;
/** Message shown below the slots when status is "error". */
errorMessage?: string;
/** External validation feedback. "error" shakes, "success" draws a check. */
status?: OTPStatus;
/** Render dots instead of the typed digits. */
mask?: boolean;
/** Block typing and dim the slots. Default: false */
disabled?: boolean;
/** Focus the input on mount. Default: false */
autoFocus?: boolean;
/** Accessible label for the underlying input. */
"aria-label"?: string;
/** Override any built-in string. */
labels?: Partial<OTPInputMessages>;
/** Classes merged onto the root element. */
className?: string;
}
export function OTPInput({
length = 6,
value: controlledValue,
defaultValue = "",
onChange,
onComplete,
label,
hint,
successMessage,
errorMessage,
status = "idle",
mask = false,
disabled = false,
autoFocus = false,
"aria-label": ariaLabelProp,
labels,
className,
}: OTPInputProps) {
const t = useMessages("otpInput", otpInputMessages, labels);
const ariaLabel = ariaLabelProp ?? t("label");
const uid = useId();
const reduce = useReducedMotion();
const inputRef = useRef<HTMLInputElement>(null);
const slotsRef = useRef<HTMLDivElement>(null);
const controlled = controlledValue !== undefined;
// Source of truth is a fixed-length array, so a cleared middle slot stays an
// in-place hole instead of collapsing the digits after it to the left.
const [slots, setSlots] = useState<string[]>(() =>
toSlots(controlled ? controlledValue : defaultValue, length),
);
const [focused, setFocused] = useState(false);
const [active, setActive] = useState(0);
// The native input holds no value, so screen readers cannot echo what was
// typed. This polite region speaks each change instead; the alternating
// trailing space makes a repeated phrase count as new text.
const [announcement, setAnnouncement] = useState("");
const announce = (text: string) =>
setAnnouncement((previous) =>
previous === text ? `${text}\u00a0` : text,
);
const spokenSlot = (char: string) =>
char ? (mask ? t("filled") : char) : t("empty");
const joined = slots.join("");
const joinedRef = useRef(joined);
useEffect(() => {
joinedRef.current = joined;
}, [joined]);
// Pull in external value changes; skip when the parent is just echoing our own
// onChange, so internal holes survive the controlled round-trip.
useEffect(() => {
if (!controlled) return;
const incoming = sanitize(controlledValue, length);
if (incoming !== joinedRef.current) setSlots(toSlots(incoming, length));
}, [controlled, controlledValue, length]);
const commit = (next: string[]) => {
const wasComplete = slots.every((c) => c !== "");
const changed = next.flatMap((c, i) => (c !== slots[i] ? [i] : []));
const progress = t("progress", {
entered: next.filter(Boolean).length,
total: length,
});
if (changed.length === 1) {
const char = next[changed[0]];
announce(
char
? t("entered", { digit: mask ? t("maskedDigit") : char, progress })
: t("deleted", { progress }),
);
} else if (changed.length > 1) {
announce(progress);
}
setSlots(next);
const str = next.join("");
onChange?.(str);
// Fire only on the empty→full transition, not on every edit of a full code.
if (!wasComplete && next.every((c) => c !== "")) onComplete?.(str);
};
const clearSlot = (idx: number) => {
const next = [...slots];
next[idx] = "";
commit(next);
};
const slotFromClientX = (clientX: number) => {
const els = slotsRef.current?.children;
if (!els) return 0;
for (let i = 0; i < els.length; i++) {
if (clientX < els[i].getBoundingClientRect().right) return i;
}
return length - 1;
};
// Single insertion path: one digit overwrites the active slot and advances; a
// multi-digit chunk (paste / SMS autofill) fills forward from the active slot.
const insert = (raw: string, from = active) => {
const digits = raw.replace(/\D/g, "");
if (!digits) return;
const next = [...slots];
let i = from;
for (const ch of digits) {
if (i >= length) break;
next[i] = ch;
i++;
}
commit(next);
setActive(Math.min(i, length - 1));
};
// The keyboard is the single source of truth: preventDefault on keydown
// reliably blocks native insertion (unlike beforeinput in React), so the hidden
// input never accumulates a competing string and slot holes survive.
const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (disabled || e.metaKey || e.ctrlKey || e.altKey) return;
const k = e.key;
if (/^[0-9]$/.test(k)) {
e.preventDefault();
insert(k);
} else if (k === "Backspace") {
e.preventDefault();
// A filled slot clears in place; an empty slot steps back and clears there.
if (slots[active]) {
clearSlot(active);
} else if (active > 0) {
clearSlot(active - 1);
setActive((current) => Math.max(current - 1, 0));
}
} else if (k === "Delete") {
e.preventDefault();
clearSlot(active);
} else if (
k === "ArrowLeft" ||
k === "ArrowRight" ||
k === "Home" ||
k === "End"
) {
e.preventDefault();
const to =
k === "ArrowLeft"
? Math.max(active - 1, 0)
: k === "ArrowRight"
? Math.min(active + 1, length - 1)
: k === "Home"
? 0
: length - 1;
setActive(to);
announce(
t("position", {
slot: spokenSlot(slots[to] ?? ""),
position: to + 1,
total: length,
}),
);
}
};
const onPaste = (e: React.ClipboardEvent<HTMLInputElement>) => {
if (disabled) return;
// preventDefault suppresses the duplicate onChange, keeping that path autofill-only.
e.preventDefault();
insert(e.clipboardData.getData("text"), active);
};
// Autofill path: SMS one-time-code arrives as a whole value in one shot.
// Keystrokes go through onKeyDown and paste through onPaste, so only autofill
// reaches here - spread it across the slots from the start.
const onChangeNative = (e: React.ChangeEvent<HTMLInputElement>) => {
const digits = sanitize(e.target.value, length);
if (!digits) return;
commit(toSlots(digits, length));
setActive(Math.min(digits.length, length - 1));
};
// Error shake - imperative so it replays on every transition into "error".
useEffect(() => {
if (status !== "error" || reduce || !slotsRef.current) return;
animate(
slotsRef.current,
{ x: [0, -5, 5, -3, 3, -1, 0] },
{ duration: 0.45, ease: EASE_OUT },
);
}, [status, reduce]);
const showSuccess = status === "success";
const activeIndex = focused ? active : -1;
const message = showSuccess
? successMessage
: status === "error"
? errorMessage
: hint;
return (
<div className={cn("inline-flex flex-col gap-2", className)}>
{label ? (
<label
htmlFor={`${uid}-input`}
className="text-sm font-medium text-foreground"
>
{label}
</label>
) : null}
<fieldset
className="relative m-0 inline-flex w-max border-0 p-0"
onMouseDown={(e) => {
if (disabled) return;
// Suppress the native click-caret; we drive the active slot ourselves.
e.preventDefault();
// Clamp to the first empty slot so a click can't jump ahead of progress.
const firstEmpty = slots.indexOf("");
const cap = firstEmpty === -1 ? length - 1 : firstEmpty;
setActive(Math.min(slotFromClientX(e.clientX), cap));
inputRef.current?.focus();
}}
>
<input
ref={inputRef}
id={`${uid}-input`}
inputMode="numeric"
autoComplete="one-time-code"
// biome-ignore lint/a11y/noAutofocus: opt-in via prop for OTP-first screens.
autoFocus={autoFocus}
disabled={disabled}
aria-label={ariaLabel}
aria-invalid={status === "error"}
// Kept empty on purpose - our state owns the digits, native holds none.
value=""
maxLength={length}
onKeyDown={onKeyDown}
onChange={onChangeNative}
onPaste={onPaste}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
// Transparent overlay owns focus, the soft keyboard, paste and autofill;
// the slots below are purely presentational.
className="absolute inset-0 z-20 h-full w-full cursor-text bg-transparent text-transparent caret-transparent opacity-0 outline-none disabled:cursor-not-allowed"
/>
<div ref={slotsRef} className="flex items-center gap-2">
{Array.from({ length }, (_, i) => {
const char = slots[i] ?? "";
const isActive = i === activeIndex;
return (
<div
// biome-ignore lint/suspicious/noArrayIndexKey: fixed-length slot grid, never reordered.
key={`${uid}-${i}`}
data-active={isActive}
data-filled={char !== ""}
className={cn(
"relative grid h-14 w-12 place-items-center overflow-hidden rounded-full border text-xl font-medium tabular-nums transition-colors duration-200",
showSuccess
? "border-emerald-500/60 text-foreground"
: status === "error"
? "border-destructive/60 text-foreground"
: char
? "border-border-strong text-foreground"
: "border-border text-muted-foreground",
// Active slot reads stronger; twMerge lets this win the border.
isActive && !showSuccess && status !== "error" && "border-foreground",
disabled && "opacity-50",
)}
>
{/* Blinking caret marks the active slot - centered when empty,
trailing the digit when the slot is already filled. */}
{isActive && !showSuccess ? (
<motion.span
aria-hidden
animate={reduce ? undefined : { opacity: [1, 1, 0, 0] }}
transition={
reduce
? undefined
: { duration: 1, repeat: Number.POSITIVE_INFINITY, ease: "linear" }
}
className={cn(
"pointer-events-none absolute top-1/2 h-6 w-px -translate-y-1/2 bg-foreground",
char ? "right-3" : "left-1/2 -translate-x-1/2",
)}
/>
) : null}
{/* Digits roll vertically. Each is absolutely centered so enter and
exit overlap in place - no in-flow reflow, no sideways drift. */}
<AnimatePresence initial={false}>
{char ? (
<motion.span
key={char}
initial={
reduce
? { opacity: 0 }
: { y: 14, opacity: 0, filter: "blur(4px)" }
}
animate={
reduce
? { opacity: 1 }
: { y: 0, opacity: 1, filter: "blur(0px)" }
}
exit={
reduce
? { opacity: 0 }
: { y: -14, opacity: 0, filter: "blur(4px)" }
}
transition={
reduce
? { duration: 0 }
: { duration: 0.22, ease: EASE_OUT }
}
className="absolute inset-0 grid place-items-center leading-none"
>
{mask ? "•" : char}
</motion.span>
) : null}
</AnimatePresence>
</div>
);
})}
</div>
<AnimatePresence>
{showSuccess ? (
<motion.span
initial={reduce ? { opacity: 0 } : { scale: 0.6, opacity: 0 }}
animate={reduce ? { opacity: 1 } : { scale: 1, opacity: 1 }}
exit={reduce ? { opacity: 0 } : { scale: 0.6, opacity: 0 }}
transition={
reduce
? { duration: 0 }
: SPRING_PRESS
}
className="pointer-events-none absolute -right-7 top-1/2 -translate-y-1/2 text-emerald-500"
aria-hidden
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={3}
strokeLinecap="round"
strokeLinejoin="round"
>
<title>{t("verified")}</title>
<motion.path
d="M5 13l4 4L19 7"
initial={reduce ? { pathLength: 1 } : { pathLength: 0 }}
animate={{ pathLength: 1 }}
transition={
reduce
? { duration: 0 }
: { duration: 0.35, ease: EASE_OUT, delay: 0.1 }
}
/>
</svg>
</motion.span>
) : null}
</AnimatePresence>
</fieldset>
<span aria-live="polite" aria-atomic="true" className="sr-only">
{announcement}
</span>
{message ? (
<p
aria-live="polite"
className={cn(
"text-sm",
showSuccess
? "text-emerald-500"
: status === "error"
? "text-destructive"
: "text-muted-foreground",
)}
>
{message}
</p>
) : null}
</div>
);
}
function sanitize(raw: string, length: number) {
return raw.replace(/\D/g, "").slice(0, length);
}
function toSlots(raw: string, length: number) {
const digits = sanitize(raw, length);
return Array.from({ length }, (_, i) => digits[i] ?? "");
}
length?numberNumber of slots. Default 6.
6value?stringThe entered digits, for controlled use.
defaultValue?stringThe initial digits when uncontrolled. Default: ""
onChange?((value: string) => void)Called with the entered digits whenever a slot changes.
onComplete?((value: string) => void)Fires once every slot is filled.
label?stringOptional label rendered above the slots.
hint?stringHelper text shown below the slots while idle.
successMessage?stringMessage shown below the slots when status is "success".
errorMessage?stringMessage shown below the slots when status is "error".
status?"error" | "success" | "idle"External validation feedback. "error" shakes, "success" draws a check.
idlemask?booleanRender dots instead of the typed digits.
falsedisabled?booleanBlock typing and dim the slots. Default: false
falseautoFocus?booleanFocus the input on mount. Default: false
falsearia-label?stringAccessible label for the underlying input.
labels?Partial<{ label: string; progress: string; entered: string; maskedDigit: string; deleted: string; position: string; filled: string; empty: string; verified: string; }>Override any built-in string.
className?stringClasses merged onto the root element.
Feedback Widget
Corner trigger that morphs open into a feedback popup with message entry and animated sending, success and retry states.
Availability Scheduler
Weekly availability editor where each day springs between available and unavailable, time ranges add and remove with blur-slide motion, times pick from a scrollable dropdown, and a copy menu clones hours to other days.
File Upload
Two file upload patterns: an attachment workspace for mixed files, links, audio and media, plus a progress queue with retry and removal.
Some components on this site are inspired by or recreated from existing work across the web. I'm not here to take credit; just to learn, experiment, and sometimes push things a bit further. If something looks familiar and I forgot to mention you, reach out and I'll fix that right away.
Updated