OTP Input
One-time-code input: ruled digit squares (split 3+3 for six digits) that turn yellow and press in on focus; auto-advance, backspace and arrow navigation, paste of the full code, completion callback and error state.
brutalist/ui/otp-inputSource
"use client";
import { useId, useRef, useState, type ClipboardEvent, type KeyboardEvent } from "react";
import { cn } from "@/lib/utils";
export interface OtpInputProps {
label?: string;
length?: number;
/** Called once every digit is filled. */
onComplete?: (code: string) => void;
error?: string;
className?: string;
}
export function OtpInput({ label = "Verification code", length = 6, onComplete, error, className }: OtpInputProps) {
const [digits, setDigits] = useState<string[]>(() => Array.from({ length }, () => ""));
const refs = useRef<(HTMLInputElement | null)[]>([]);
const groupId = useId();
function update(next: string[]) {
setDigits(next);
if (next.every((d) => d !== "")) onComplete?.(next.join(""));
}
function setAt(i: number, value: string) {
const v = value.replace(/\D/g, "").slice(-1);
const next = [...digits];
next[i] = v;
update(next);
if (v && i < length - 1) refs.current[i + 1]?.focus();
}
function onKeyDown(i: number, e: KeyboardEvent<HTMLInputElement>) {
if (e.key === "Backspace" && !digits[i] && i > 0) refs.current[i - 1]?.focus();
if (e.key === "ArrowLeft" && i > 0) refs.current[i - 1]?.focus();
if (e.key === "ArrowRight" && i < length - 1) refs.current[i + 1]?.focus();
}
function onPaste(e: ClipboardEvent<HTMLInputElement>) {
const pasted = e.clipboardData.getData("text").replace(/\D/g, "").slice(0, length);
if (!pasted) return;
e.preventDefault();
const next = Array.from({ length }, (_, i) => pasted[i] ?? "");
update(next);
refs.current[Math.min(pasted.length, length - 1)]?.focus();
}
return (
<div className={cn("flex flex-col gap-3", className)}>
<p id={`${groupId}-label`} className="font-da-mono text-xs font-bold tracking-da-label uppercase">
{label}
</p>
<div role="group" aria-labelledby={`${groupId}-label`} aria-describedby={error ? `${groupId}-error` : undefined} className="flex gap-2 sm:gap-3">
{digits.map((d, i) => (
<input
key={i}
ref={(el) => {
refs.current[i] = el;
}}
value={d}
onChange={(e) => setAt(i, e.target.value)}
onKeyDown={(e) => onKeyDown(i, e)}
onPaste={onPaste}
onFocus={(e) => e.target.select()}
inputMode="numeric"
autoComplete={i === 0 ? "one-time-code" : "off"}
aria-label={`Digit ${i + 1} of ${length}`}
aria-invalid={error ? true : undefined}
className={cn(
"da-stroke da-transition size-12 bg-da-input text-center font-da-display text-2xl text-da-fg shadow-da-sm outline-none sm:size-14",
"focus:translate-x-[3px] focus:translate-y-[3px] focus:bg-da-primary focus:text-da-primary-fg focus:shadow-none",
d && "bg-da-surface",
error && "[--da-border:var(--da-danger)]",
length === 6 && i === 2 && "mr-2 sm:mr-4",
)}
/>
))}
</div>
{error && (
<p id={`${groupId}-error`} className="text-sm font-bold">
! {error}
</p>
)}
</div>
);
}
export default OtpInput;
modules/brutalist/ui/otp-input/index.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | "Verification code" | Label of the group. |
| length | number | 6 | Number of digits. |
| onComplete | (code: string) => void | — | Called when all digits are filled. |
| error | string | — | Error message. |
| className | string | — | Classes on the wrapper. |
Other input variants in Brutalist
Input
Labelled text field with hint, error state, leading icon, suffix slot and two sizes. The field presses into its shadow on focus; errors switch to a red stroke with an explicit message.
Number Stepper
Numeric input flanked by chunky yellow − / + buttons (disabled at bounds), big display-font value, optional unit suffix; clamps typed values to min/max.
Textarea
Labelled multi-line field with a live character counter chip that turns orange near the limit, hint and error states; presses into its shadow on focus.
Input in other art directions
Combobox
Autocomplete select: a glass pill input that filters a frosted listbox as you type, with arrow-key highlight, Enter to choose, Escape to close, hints and a check on the selection.
Input
Frosted text field with label, hint, error state, leading icon, trailing slot (button, kbd, unit), rounded or pill shape and two sizes. Focus brightens the glass and adds a soft violet halo.
Range Slider
Glass slider on Radix Slider: frosted track, primary→accent range, glowing gradient thumbs with value bubbles on hover/focus and tick labels; single value or range.
Tag Input
Glass chip input: type and press Enter or comma to add gradient chips, Backspace removes the last, × removes one; quick-add suggestion chips and a max counter.
Input
Hairline text field with label (auto "Optional" marker), hint, error with icon, leading icon, text prefix, trailing slot (unit, kbd) and indigo focus ring. Two sizes.
Search Input
Search field with leading icon, “/” keycap hint that focuses the field, clear button (Escape clears too) and a loading spinner state. Two sizes, controlled or uncontrolled.
Select Menu
Labelled select built on Radix Select: bordered trigger with chevrons, portalled popover with optional icons, descriptions, groups, separators, disabled items and a check on the selected one.
Switch Field
Settings row with label and description on the left and a Radix Switch on the right; plain (for divided lists) or bordered card that highlights when on. Disabled state.
Checkbox Group
Settings checkboxes as ruled rows: square ink check boxes (native inputs), label and muted description; the whole row is clickable.
Input
Underline field: mono uppercase label, a borderless input on a single bottom hairline that turns ink (2px) on focus, optional prefix, hint or red error.
Search Input
Search field with live results: hairline-bordered input with icon and ⌘K hint; matches appear in a ruled list below with the query highlighted and a mono kind label; empty state included.
Select Field
Native select styled as an underline field with a mono label and a custom chevron — reliable on every device and screen reader.
Currency Input
Amount field with currency symbol prefix and an attached currency select; formats with thousands separators on blur, right-aligned mono digits.
File Upload
Receipt/document dropzone: dashed bordered area with upload icon, click-or-drag, file list with size, per-file size validation errors and remove buttons.
Input
Form field: label (with optional marker), bordered 40px input with leading/trailing adornments, blue focus ring, hint text or red error with aria-invalid.
Select Field
Labelled Radix Select styled as a bordered field: grouped options (e.g. GL accounts) with group headings, check on the selected item, portalled popover with data-da.
Chip Select
Multi-select as soft pill checkboxes: unselected sand pills, selected sage pills with a check that pops in; native checkboxes in a fieldset.
Input
Soft filled text field: rounded sand-filled input that turns cream with a sage ring on focus, optional leading icon and trailing unit, hint or gentle clay error message.
Target Slider
Reduction target picker: a thick rounded sage range (0–60%) with tick labels and a “science-aligned” marker, showing the resulting tonnes and a gentle verdict.
Textarea
Soft note field: rounded sand textarea that auto-grows (field-sizing), with a live character counter that turns clay near the limit.
Checkbox List
Round-checkbox to-do list built on native checkboxes: bouncy mint ticks, strike-through when done, meta line per item and an “x of y” counter with a progress bar.
Day Picker
Week-strip date picker: prev/next week arrows and seven rounded day pills (weekday + number) with task-load dots (red when full); the selected day fills periwinkle.
Input
Soft text field: rounded tinted well that turns white with a thick periwinkle ring on focus, label, optional leading icon and suffix, hint or red error message.
Radio Cards
Native radio group rendered as big pastel cards (emoji, title, text); the chosen card lifts with a periwinkle ring and a filled radio dot.