"use client";
import { useEffect, useRef, useState } from "react";
/**
* Carousel — an original take inspired by bencho.dev's "Carousel".
* A short deck of photo cards fanned in depth. Drag (or flick) to turn the
* deck, let go and it springs to the nearest card. Cards drift gently while
* idle, and the front card leans away from the pointer as if pressed.
*/
export type Slide = { src: string; alt: string };
const SLIDES: Slide[] = [
{ src: "/bencho/peak.webp", alt: "A snowy peak above a green ridge" },
{ src: "/bencho/portrait.webp", alt: "A portrait in dappled green light" },
{ src: "/bencho/procession.webp", alt: "Figures in red walking up a green hillside" },
{ src: "/bencho/bicycle.webp", alt: "A bicycle leaning against a sunlit wall" },
{ src: "/bencho/meadow.webp", alt: "A misty hillside with grazing animals" },
];
export function Carousel({
slides = SLIDES,
defaultIndex = 1,
spread = 100,
float = true,
tilt = 60,
corner = 20,
onChange,
}: {
slides?: Slide[];
defaultIndex?: number;
/** Horizontal distance between neighbouring cards, px. */
spread?: number;
/** Idle drift. */
float?: boolean;
/** 0–100: how far the front card leans under the pointer. */
tilt?: number;
corner?: number;
onChange?: (index: number) => void;
}) {
const n = slides.length;
const [index, setIndex] = useState(Math.min(defaultIndex, n - 1));
const cards = useRef<(HTMLDivElement | null)[]>([]);
const root = useRef<HTMLDivElement>(null);
const s = useRef({ p: index, v: 0, target: index, drag: null as null | { x0: number; p0: number; lastX: number; lastT: number; vel: number; moved: boolean }, lean: { x: 0, y: 0, tx: 0, ty: 0 } });
const opts = useRef({ spread, float, tilt });
opts.current = { spread, float, tilt };
const idx = useRef(index);
const goTo = (i: number) => {
const t = Math.max(0, Math.min(n - 1, i));
s.current.target = t;
if (t !== idx.current) {
idx.current = t;
setIndex(t);
onChange?.(t);
}
};
useEffect(() => {
let raf = 0;
let last = performance.now();
const t0 = last;
const frame = (now: number) => {
const dt = Math.min(0.033, (now - last) / 1000);
last = now;
const st = s.current;
const { spread: sp, float: fl, tilt: tl } = opts.current;
if (!st.drag) {
const k = 170;
const c = 2 * 0.78 * Math.sqrt(k);
st.v += (k * (st.target - st.p) - c * st.v) * dt;
st.p += st.v * dt;
}
st.lean.x += (st.lean.tx - st.lean.x) * Math.min(1, dt * 10);
st.lean.y += (st.lean.ty - st.lean.y) * Math.min(1, dt * 10);
const time = (now - t0) / 1000;
cards.current.forEach((el, i) => {
if (!el) return;
const o = i - st.p;
const a = Math.abs(o);
const f = Math.max(0, 1 - Math.min(a, 1)); // 1 at front, 0 one step away
const x = Math.sign(o) * (Math.min(a, 1) * sp + Math.max(0, a - 1) * sp * 0.35);
const scale = 1 - Math.min(a, 1.6) * 0.17;
const y = -(1 - f) * 8 + (fl ? Math.sin(time * 1.1 + i * 1.7) * 2.2 : 0);
const ry = -Math.max(-1.3, Math.min(1.3, o)) * 30;
const front = f > 0.5 ? (f - 0.5) * 2 : 0;
const lx = st.lean.x * front * (tl / 100);
const ly = st.lean.y * front * (tl / 100);
const op = a < 1.15 ? 1 : Math.max(0, 1 - (a - 1.15) / 0.45);
el.style.transform = `translate3d(${x.toFixed(2)}px, ${y.toFixed(2)}px, 0) perspective(560px) rotateY(${(ry + lx * 10).toFixed(2)}deg) rotateX(${(-ly * 8).toFixed(2)}deg) scale(${(scale * (1 - front * 0.025 * Math.hypot(lx, ly))).toFixed(4)})`;
el.style.zIndex = String(100 - Math.round(a * 10));
el.style.opacity = op.toFixed(3);
el.style.filter = `brightness(${(1 - Math.min(a, 1) * 0.16).toFixed(3)})`;
el.style.pointerEvents = op > 0.2 ? "auto" : "none";
});
raf = requestAnimationFrame(frame);
};
raf = requestAnimationFrame(frame);
return () => cancelAnimationFrame(raf);
}, []);
const scaleOf = () => {
const el = root.current;
return el && el.offsetWidth ? el.getBoundingClientRect().width / el.offsetWidth || 1 : 1;
};
return (
<div
ref={root}
role="region"
aria-roledescription="carousel"
aria-label="Photos"
tabIndex={0}
onKeyDown={(e) => {
if (e.key === "ArrowRight") goTo(idx.current + 1);
else if (e.key === "ArrowLeft") goTo(idx.current - 1);
else if (e.key === "Home") goTo(0);
else if (e.key === "End") goTo(n - 1);
else return;
e.preventDefault();
}}
onPointerDown={(e) => {
e.currentTarget.setPointerCapture(e.pointerId);
const st = s.current;
st.drag = { x0: e.clientX, p0: st.p, lastX: e.clientX, lastT: performance.now(), vel: 0, moved: false };
st.v = 0;
}}
onPointerMove={(e) => {
const st = s.current;
const r = root.current!.getBoundingClientRect();
const k = scaleOf();
// lean: pointer position relative to the front card's centre, -1..1
st.lean.tx = Math.max(-1, Math.min(1, (e.clientX - (r.left + r.width / 2)) / (90 * k)));
st.lean.ty = Math.max(-1, Math.min(1, (e.clientY - (r.top + r.height / 2)) / (120 * k)));
if (!st.drag) return;
const now = performance.now();
const dx = (e.clientX - st.drag.x0) / k;
if (Math.abs(dx) > 4) st.drag.moved = true;
let p = st.drag.p0 - dx / (opts.current.spread * 1.4);
// rubber band past either end
if (p < 0) p = -(1 - 1 / (1 - p * 1.6)) / 1.6 * 0.6;
if (p > n - 1) p = n - 1 + (1 - 1 / (1 + (p - n + 1) * 1.6)) / 1.6 * 0.6;
const inst = ((st.drag.lastX - e.clientX) / k / (opts.current.spread * 1.4)) / Math.max(0.001, (now - st.drag.lastT) / 1000);
st.drag.vel = st.drag.vel * 0.6 + inst * 0.4;
st.drag.lastX = e.clientX;
st.drag.lastT = now;
st.p = p;
}}
onPointerUp={(e) => {
const st = s.current;
const d = st.drag;
st.drag = null;
if (!d) return;
if (!d.moved) {
// a tap on a side card brings it forward
const hit = (e.target as HTMLElement).closest("[data-card]");
if (hit) goTo(Number(hit.getAttribute("data-card")));
return;
}
st.v = d.vel;
goTo(Math.round(st.p + Math.max(-0.4, Math.min(0.4, d.vel * 0.08))));
}}
onPointerLeave={() => {
s.current.lean.tx = 0;
s.current.lean.ty = 0;
}}
className="relative flex h-[260px] w-[440px] cursor-grab touch-pan-y select-none items-center justify-center outline-none focus-visible:ring-2 focus-visible:ring-[var(--b-fg)]/30 active:cursor-grabbing"
style={{ borderRadius: 24 }}
>
{slides.map((sl, i) => (
<div
key={sl.src + i}
ref={(el) => {
cards.current[i] = el;
}}
data-card={i}
role="group"
aria-roledescription="slide"
aria-label={`${i + 1} of ${n}`}
aria-hidden={i !== index}
className="absolute h-[228px] w-[168px] overflow-hidden bg-[var(--b-surface)] shadow-[0_14px_30px_-12px_rgba(0,0,0,0.55)] will-change-transform"
style={{ borderRadius: corner }}
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={sl.src} alt={sl.alt} draggable={false} className="h-full w-full object-cover" />
</div>
))}
<span className="sr-only" aria-live="polite">
{slides[index]?.alt}, {index + 1} of {n}
</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;
}