"use client";
import { motion } from "motion/react";
import { useEffect, useRef, useState } from "react";
import { Pause, Play } from "lucide-react";
import { cn } from "@/lib/utils";
/**
* Step Player — an original take inspired by bencho.dev's "Step player".
* A row of dots where the current step stretches into a bar that fills over
* the step's time. When it's full the bar hands its width to the next dot,
* and (with wake on) every dot it passes gives a small hop.
*/
const DOT = 8;
const BAR = 36;
const GAP = 8;
export function StepPlayer({
steps = 5,
pace = 3,
wake = true,
outlined = false,
onStep,
}: {
/** Number of steps (3–8). */
steps?: number;
/** Seconds each step plays for. */
pace?: number;
/** Dots hop as the bar travels past them. */
wake?: boolean;
outlined?: boolean;
onStep?: (index: number) => void;
}) {
const n = Math.max(3, Math.min(8, Math.round(steps)));
const [step, setStep] = useState(0);
const [playing, setPlaying] = useState(false);
const [fill, setFill] = useState(0);
const [hop, setHop] = useState<{ from: number; to: number; at: number } | null>(null);
const fillRef = useRef(0);
const go = (to: number) => {
const t = ((to % n) + n) % n;
setHop({ from: step, to: t, at: performance.now() });
setStep(t);
fillRef.current = 0;
setFill(0);
onStep?.(t);
};
const goRef = useRef(go);
goRef.current = go;
useEffect(() => {
if (step >= n) setStep(0);
}, [n, step]);
useEffect(() => {
if (!playing) return;
let raf = 0;
let last = performance.now();
const tick = (now: number) => {
fillRef.current += (now - last) / 1000 / pace;
last = now;
if (fillRef.current >= 1) {
goRef.current(stepRef.current + 1);
} else setFill(fillRef.current);
raf = requestAnimationFrame(tick);
};
raf = requestAnimationFrame(tick);
return () => cancelAnimationFrame(raf);
}, [playing, pace]);
const stepRef = useRef(step);
stepRef.current = step;
const trackW = BAR + (n - 1) * DOT + (n - 1) * GAP + 28;
return (
<div className="flex items-center gap-[8px]">
<div
role="slider"
tabIndex={0}
aria-label="Step"
aria-valuemin={1}
aria-valuemax={n}
aria-valuenow={step + 1}
aria-valuetext={`Step ${step + 1} of ${n}`}
onKeyDown={(e) => {
const k = e.key;
if (k === "ArrowRight" || k === "ArrowDown") go(step + 1);
else if (k === "ArrowLeft" || k === "ArrowUp") go(step - 1);
else if (k === "Home") go(0);
else if (k === "End") go(n - 1);
else return;
e.preventDefault();
}}
className={cn(
"flex h-[36px] items-center justify-center rounded-full bg-[var(--b-surface)] outline-none focus-visible:ring-2 focus-visible:ring-[var(--b-fg)]/30",
outlined && "ring-1 ring-[var(--b-line)]"
)}
style={{ width: trackW, gap: GAP }}
>
{Array.from({ length: n }).map((_, i) => {
const active = i === step;
const lo = hop ? Math.min(hop.from, hop.to) : -1;
const hi = hop ? Math.max(hop.from, hop.to) : -1;
const crossed = wake && hop && i > lo && i < hi;
const dist = hop ? Math.abs(i - hop.from) : 0;
return (
<motion.button
key={i}
type="button"
tabIndex={-1}
aria-label={`Go to step ${i + 1}`}
onClick={() => go(i)}
className="relative h-[8px] shrink-0 overflow-hidden rounded-full"
style={{ background: "color-mix(in oklab, var(--b-fg) 28%, transparent)" }}
initial={false}
animate={{
width: active ? BAR : DOT,
scaleY: crossed ? [1, 1.7, 1] : 1,
}}
transition={{
width: { duration: 0.52, ease: [0.45, 0, 0.15, 1] },
scaleY: { duration: 0.36, delay: dist * 0.06, ease: "easeOut" },
}}
>
{active && (
<span
className="absolute inset-y-0 left-0 rounded-full bg-[var(--b-fg)]"
style={{ width: `${Math.max(fill * 100, fill > 0 ? 22 : 0)}%` }}
/>
)}
</motion.button>
);
})}
</div>
<motion.button
type="button"
whileTap={{ scale: 0.9 }}
onClick={() => setPlaying((p) => !p)}
aria-label={playing ? "Pause" : "Play"}
aria-pressed={playing}
className={cn(
"flex size-[36px] items-center justify-center rounded-full bg-[var(--b-surface)] text-[var(--b-fg)] outline-none focus-visible:ring-2 focus-visible:ring-[var(--b-fg)]/30",
outlined && "ring-1 ring-[var(--b-line)]"
)}
>
{playing ? <Pause className="size-[14px] fill-current" /> : <Play className="ml-[1px] size-[14px] fill-current" />}
</motion.button>
</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;
}