"use client";
import { motion } from "motion/react";
import { useEffect, useRef, useState } from "react";
import { RollingDigits } from "./rolling-digits";
/**
* Rolling Counter — an original take inspired by bencho.dev's "Rolling counter".
* A stepper whose digits roll like an odometer. Press and hold the buttons to
* repeat, or drag the number up / down to scrub it.
*/
export function RollingCounter({
defaultValue = 95,
min = 0,
max = 99999,
step = 1,
}: {
defaultValue?: number;
min?: number;
max?: number;
step?: number;
}) {
const [value, setValue] = useState(defaultValue);
const clamp = (n: number) => Math.max(min, Math.min(max, n));
const hold = useRef<{ t?: number; i?: number }>({});
const drag = useRef<{ y: number; v: number } | null>(null);
const stop = () => {
window.clearTimeout(hold.current.t);
window.clearInterval(hold.current.i);
};
useEffect(() => stop, []);
const press = (d: number) => {
setValue((v) => clamp(v + d * step));
stop();
hold.current.t = window.setTimeout(() => {
hold.current.i = window.setInterval(() => setValue((v) => clamp(v + d * step)), 90);
}, 420);
};
return (
<div className="flex h-[56px] items-center gap-0 rounded-[16px] bg-[var(--b-surface)] p-[5px] shadow-[var(--b-shadow)]">
<StepButton d={-1} label="Less" onPress={press} onStop={stop} onKey={(d) => setValue((v) => clamp(v + d * step))} />
<motion.div
layout
role="spinbutton"
tabIndex={0}
aria-valuenow={value}
aria-valuemin={min}
aria-valuemax={max}
aria-label="Count. Drag up or down to change it."
className="flex h-[46px] min-w-[66px] cursor-ns-resize touch-none select-none items-center justify-center px-[10px] text-[30px] font-medium tracking-[-0.01em] text-[var(--b-fg)] outline-none"
onKeyDown={(e) => {
if (e.key === "ArrowUp") setValue((v) => clamp(v + step));
if (e.key === "ArrowDown") setValue((v) => clamp(v - step));
}}
onPointerDown={(e) => {
e.currentTarget.setPointerCapture(e.pointerId);
drag.current = { y: e.clientY, v: value };
}}
onPointerMove={(e) => {
if (!drag.current) return;
const steps = Math.trunc((drag.current.y - e.clientY) / 9);
setValue(clamp(drag.current.v + steps * step));
}}
onPointerUp={() => (drag.current = null)}
>
<RollingDigits value={value} group={false} />
</motion.div>
<StepButton d={1} label="More" onPress={press} onStop={stop} onKey={(d) => setValue((v) => clamp(v + d * step))} />
</div>
);
}
function StepButton({
d,
label,
onPress,
onStop,
onKey,
}: {
d: number;
label: string;
onPress: (d: number) => void;
onStop: () => void;
onKey: (d: number) => void;
}) {
return (
<motion.button
type="button"
aria-label={label}
whileTap={{ scale: 0.92 }}
transition={{ type: "spring", stiffness: 600, damping: 30 }}
onPointerDown={() => onPress(d)}
onPointerUp={onStop}
onPointerLeave={onStop}
onPointerCancel={onStop}
onKeyDown={(e) => (e.key === "Enter" || e.key === " ") && (e.preventDefault(), onKey(d))}
className="flex h-[46px] w-[40px] items-center justify-center rounded-[11px] bg-[var(--b-surface-2)] text-[var(--b-fg)] outline-none focus-visible:ring-2 focus-visible:ring-[var(--b-fg)]/30"
>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" aria-hidden>
<path d="M5 12h14" />
{d > 0 && <path d="M12 5v14" />}
</svg>
</motion.button>
);
}
"use client";
import { AnimatePresence, motion } from "motion/react";
import { useState } from "react";
/**
* Per-digit odometer. Only the digits that change move: going up, the old
* digit rolls out the top while the new one rises in from below (and the
* reverse going down), with a touch of overshoot and motion blur.
*/
export function RollingDigits({
value,
className,
digitClassName,
group = true,
tabular = true,
format,
}: {
value: number;
/** Custom text for the value (e.g. 175.6K). Digits still roll by place. */
format?: (value: number) => string;
className?: string;
digitClassName?: string;
group?: boolean;
/** Equal-width digits (steppers). Turn off for inline counts. */
tabular?: boolean;
}) {
const [track, setTrack] = useState({ value, dir: 1 });
if (track.value !== value) setTrack({ value, dir: value > track.value ? 1 : -1 });
const dir = track.value !== value ? (value > track.value ? 1 : -1) : track.dir;
const text = format ? format(value) : group ? value.toLocaleString("en-US") : String(value);
const chars = text.split("");
// Key digits by their place value (counted from the right) so a carry
// only animates the places that actually changed.
const total = chars.filter((c) => /\d/.test(c)).length;
let seen = 0;
const keyed = chars.map((c) =>
/\d/.test(c)
? { c, key: `d${total - 1 - seen++}` }
: { c, key: `s${total - seen}` }
);
return (
<span className={`${tabular ? "bencho-tabular " : ""}relative inline-flex overflow-hidden ${className ?? ""}`} style={{ lineHeight: 1 }}>
<AnimatePresence initial={false} mode="popLayout">
{keyed.map(({ c, key }) =>
/\d/.test(c) ? (
<motion.span key={key} layout="position" className="relative inline-block" transition={{ layout: { type: "spring", stiffness: 500, damping: 40 } }}>
<AnimatePresence initial={false} mode="popLayout" custom={dir}>
<motion.span
key={c}
custom={dir}
className={`inline-block ${digitClassName ?? ""}`}
variants={{
enter: (d: number) => ({ y: d > 0 ? "70%" : "-70%", opacity: 0, filter: "blur(2px)" }),
center: { y: "0%", opacity: 1, filter: "blur(0px)" },
exit: (d: number) => ({ y: d > 0 ? "-70%" : "70%", opacity: 0, filter: "blur(2px)" }),
}}
initial="enter"
animate="center"
exit="exit"
transition={{ type: "spring", stiffness: 280, damping: 24, mass: 0.8, opacity: { duration: 0.22 } }}
>
{c}
</motion.span>
</AnimatePresence>
</motion.span>
) : (
<motion.span key={key} layout="position" className="inline-block" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>
{c}
</motion.span>
)
)}
</AnimatePresence>
</span>
);
}
"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;
}