"use client";
import { AnimatePresence, motion, useAnimationControls } from "motion/react";
import { useEffect, useId, useRef, useState } from "react";
import { Check } from "lucide-react";
import { cn } from "@/lib/utils";
/**
* One-time Code — an original take inspired by bencho.dev's "One-time code".
* One real input drives a row of cells. Digits rise in out of a blur; a good
* code pulls the cells together until they melt into a "Verified" capsule, a
* bad one turns red, shakes, and drains away right to left.
*/
type Verdict = "accept" | "reject";
const CELL_W = 36;
const CELL_H = 42;
const GAP = 7;
const RED = "#f05252";
export function OneTimeCode({
length = 4,
corner = 12,
verify = "accept",
outlined = false,
onComplete,
}: {
/** Number of digits, 4 or 6. */
length?: 4 | 6;
/** Cell corner radius in px. */
corner?: number;
/** "accept", "reject", or a function that checks the code. */
verify?: Verdict | ((code: string) => boolean | Promise<boolean>);
outlined?: boolean;
onComplete?: (code: string, ok: boolean) => void;
}) {
const [code, setCode] = useState("");
const [focused, setFocused] = useState(false);
const [state, setState] = useState<"edit" | "fuse" | "done" | "bad">("edit");
const input = useRef<HTMLInputElement>(null);
const shake = useAnimationControls();
const gooId = useId().replace(/:/g, "");
// a new length starts over
useEffect(() => {
setCode("");
setState("edit");
}, [length]);
const settle = async (full: string) => {
const ok = typeof verify === "function" ? await verify(full) : verify === "accept";
onComplete?.(full, ok);
if (ok) {
// let the last digit land before the cells pull together
await new Promise((r) => setTimeout(r, 260));
setState("fuse");
window.setTimeout(() => setState("done"), 380);
window.setTimeout(() => {
setState("edit");
setCode("");
}, 2600);
} else {
setState("bad");
await shake.start({
x: [0, -5, 5, -4, 4, -2.5, 2.5, -1, 0],
transition: { duration: 0.45, ease: "linear" },
});
// drain right to left, one digit at a time
for (let i = full.length - 1; i >= 0; i--) {
await new Promise((r) => setTimeout(r, 60));
setCode(full.slice(0, i));
}
setState("edit");
window.setTimeout(() => input.current?.focus(), 0);
}
};
const onChange = (v: string) => {
if (state !== "edit") return;
const next = v.replace(/\D/g, "").slice(0, length);
setCode(next);
if (next.length === length) void settle(next);
};
const cells = Array.from({ length });
const total = length * CELL_W + (length - 1) * GAP;
const caret = focused && state === "edit" ? Math.min(code.length, length - 1) : -1;
const fusing = state === "fuse" || state === "done";
return (
<div
className="relative flex items-center justify-center"
style={{ width: total + 40, height: CELL_H + 40 }}
onPointerDown={(e) => {
e.preventDefault();
input.current?.focus();
}}
>
<svg width="0" height="0" className="absolute" aria-hidden>
<filter id={gooId}>
<feGaussianBlur in="SourceGraphic" stdDeviation="5" result="b" />
<feColorMatrix in="b" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 22 -9" result="g" />
<feComposite in="SourceGraphic" in2="g" operator="atop" />
</filter>
</svg>
<input
ref={input}
value={code}
onChange={(e) => onChange(e.target.value)}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
onKeyDown={(e) => {
if (e.key === "Escape") {
setCode("");
input.current?.blur();
}
}}
inputMode="numeric"
autoComplete="one-time-code"
pattern="[0-9]*"
maxLength={length}
aria-label={`${length}-digit verification code`}
disabled={state !== "edit"}
className="absolute inset-0 z-10 cursor-text opacity-0"
style={{ caretColor: "transparent" }}
/>
{/* cells: under the goo filter only while they melt together */}
<motion.div
animate={shake}
className="relative flex items-center"
style={{ gap: GAP, filter: fusing ? `url(#${gooId})` : undefined }}
aria-hidden
>
{cells.map((_, i) => {
const ch = code[i];
const toCentre = (total / 2 - (i * (CELL_W + GAP) + CELL_W / 2));
return (
<motion.div
key={i}
className={cn(
"relative flex items-center justify-center overflow-hidden bg-[var(--b-surface)] text-[20px] font-medium",
outlined && "ring-1 ring-[var(--b-line)]"
)}
style={{ width: CELL_W, height: CELL_H, borderRadius: corner }}
initial={false}
animate={
fusing
? { x: toCentre * 0.78, opacity: state === "done" ? 0 : 1, scale: 1.04 }
: { x: 0, opacity: 1, scale: ch ? [1, 1.06, 1] : 1 }
}
transition={
fusing
? { x: { type: "spring", stiffness: 260, damping: 26 }, opacity: { duration: 0.15 } }
: { duration: 0.28, ease: [0.3, 0, 0.2, 1] }
}
>
<AnimatePresence initial={false}>
{ch && (
<motion.span
key={ch + i}
className="bencho-tabular absolute"
style={{ color: state === "bad" ? RED : "var(--b-fg)" }}
initial={{ y: "45%", scale: 0.86, opacity: 0, filter: "blur(3px)" }}
animate={{ y: "0%", scale: 1, opacity: 1, filter: "blur(0px)" }}
exit={{ y: "-35%", scale: 0.9, opacity: 0, filter: "blur(3px)" }}
transition={{ duration: 0.26, ease: [0.2, 0.8, 0.2, 1] }}
>
{ch}
</motion.span>
)}
</AnimatePresence>
{caret === i && (
<motion.span
className="absolute h-[18px] w-[2px] rounded-full bg-[var(--b-fg)]"
animate={{ opacity: [1, 1, 0, 0] }}
transition={{ duration: 1, repeat: Infinity, times: [0, 0.5, 0.55, 1] }}
/>
)}
</motion.div>
);
})}
</motion.div>
{/* the capsule the cells become */}
<AnimatePresence>
{state === "done" && (
<motion.div
initial={{ width: 72, opacity: 0.6 }}
animate={{ width: 125, opacity: 1 }}
exit={{ opacity: 0, scale: 0.94, filter: "blur(4px)" }}
transition={{ type: "spring", stiffness: 300, damping: 24 }}
className="absolute flex items-center justify-center gap-[6px] overflow-hidden rounded-full bg-[var(--b-surface)] text-[14px] font-medium text-[var(--b-fg)]"
style={{ height: CELL_H }}
role="status"
>
<motion.span
className="flex items-center gap-[6px] whitespace-nowrap"
initial={{ opacity: 0, filter: "blur(4px)", y: 4 }}
animate={{ opacity: 1, filter: "blur(0px)", y: 0 }}
transition={{ delay: 0.12, duration: 0.25 }}
>
<Check className="size-[15px]" strokeWidth={2.4} />
Verified
</motion.span>
</motion.div>
)}
</AnimatePresence>
<span className="sr-only" aria-live="polite">
{state === "bad" ? "Code rejected" : state === "done" ? "Code verified" : ""}
</span>
</div>
);
}
"use client";
import type { ReactNode } from "react";
import { cn } from "@/lib/utils";
/**
* Neutral stage the Bencho Originals sit on. The block renders at its
* natural size and is scaled as a whole (like a device preview), so
* strokes, radii and type keep their proportions at any stage size.
*/
export function BenchoStage({
children,
scale = 1.25,
className,
}: {
children: ReactNode;
scale?: number;
className?: string;
}) {
return (
<div
className={cn(
"bencho bencho-font relative flex items-center justify-center overflow-hidden rounded-2xl text-[var(--b-fg)]",
"bg-[var(--b-stage)]",
className
)}
>
<div style={{ transform: `scale(${scale})` }} className="origin-center">
{children}
</div>
</div>
);
}
/* Bencho Originals — shared surface tokens (light / dark). */
.bencho {
--b-stage: #f1f1f1;
--b-surface: #ffffff;
--b-surface-2: #f1f1f1;
--b-fg: #171717;
--b-fg-muted: rgb(23 23 23 / 0.5);
--b-ink: #171717;
--b-invert: #171717;
--b-invert-fg: #ffffff;
--b-line: rgb(0 0 0 / 0.06);
--b-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 0 0 1px rgb(0 0 0 / 0.04);
}
.dark .bencho {
--b-stage: #171717;
--b-surface: #262626;
--b-surface-2: #333333;
--b-fg: #ffffff;
--b-fg-muted: rgb(255 255 255 / 0.55);
--b-ink: #ffffff;
--b-invert: #ffffff;
--b-invert-fg: #262626;
--b-line: rgb(255 255 255 / 0.06);
--b-shadow: none;
}
.bencho-font {
font-family: var(--font-bencho-inter), var(--font-geist-sans), ui-sans-serif, system-ui, sans-serif;
font-feature-settings: "cv11", "ss01";
-webkit-font-smoothing: antialiased;
}
.bencho-tabular { font-variant-numeric: tabular-nums; }
/* Code panel: Shiki dual-theme output, with line numbers */
.bencho-code pre.shiki { background: transparent !important; margin: 0; }
.bencho-code .shiki span { color: var(--shiki-light); }
.dark .bencho-code .shiki span { color: var(--shiki-dark); }
.bencho-code .shiki code { counter-reset: line; display: block; }
.bencho-code .shiki .line::before {
counter-increment: line;
content: counter(line);
display: inline-block;
width: 2.2em;
margin-right: 1.1em;
text-align: right;
opacity: 0.32;
}