"use client";
import { AnimatePresence, motion } from "motion/react";
import { useCallback, useEffect, useRef, useState } from "react";
import { cn } from "@/lib/utils";
/**
* Card Swap — an original take inspired by bencho.dev's "Card swap".
* Photo cards stacked in depth. Press the front card, or fling it, and it
* drops out and slides straight to the back while the rest step forward.
* Switch auto on and the deck swaps by itself every few seconds.
*/
export type SwapCard = { src: string; alt: string; label: string };
const CARDS: SwapCard[] = [
{ src: "/bencho/peak.webp", alt: "A snowy peak above a green ridge", label: "Mountain" },
{ src: "/bencho/portrait.webp", alt: "A portrait in dappled green light", label: "Portrait" },
{ src: "/bencho/procession.webp", alt: "Figures in red walking up a green hillside", label: "Procession" },
{ src: "/bencho/bicycle.webp", alt: "A bicycle leaning against a sunlit wall", label: "Bicycle" },
{ src: "/bencho/meadow.webp", alt: "A misty hillside with grazing animals", label: "Meadow" },
];
const DROP_MS = 300;
export function CardSwap({
cards = CARDS,
fill = "light",
auto = false,
every = 4,
onSwap,
}: {
cards?: SwapCard[];
/** Frame and label colour. */
fill?: "light" | "dark";
/** Swap by itself. */
auto?: boolean;
/** Seconds between automatic swaps. */
every?: number;
/** Called with the new front card's index. */
onSwap?: (index: number) => void;
}) {
const n = cards.length;
const [order, setOrder] = useState(() => cards.map((_, i) => i)); // order[0] = front
const [dropping, setDropping] = useState<number | null>(null);
const orderRef = useRef(order);
orderRef.current = order;
const busy = useRef(false);
const hovering = useRef(false);
const swap = useCallback(() => {
if (busy.current || n < 2) return;
busy.current = true;
setDropping(orderRef.current[0]);
window.setTimeout(() => {
setOrder((o) => {
const next = [...o.slice(1), o[0]];
onSwap?.(next[0]);
return next;
});
setDropping(null);
window.setTimeout(() => (busy.current = false), 260);
}, DROP_MS);
}, [n, onSwap]);
useEffect(() => {
if (!auto) return;
const id = window.setInterval(() => {
if (!hovering.current) swap();
}, Math.max(1, every) * 1000);
return () => window.clearInterval(id);
}, [auto, every, swap]);
const dark = fill === "dark";
const front = order[0];
return (
<div
className="relative select-none"
style={{ width: 340, height: 320, perspective: 900 }}
onPointerEnter={() => (hovering.current = true)}
onPointerLeave={() => (hovering.current = false)}
>
{cards.map((card, i) => {
const p = order.indexOf(i);
const isFront = p === 0;
const isDropping = dropping === i;
const depth = Math.min(p, 4);
return (
<motion.div
key={card.src + i}
className={cn(
"absolute overflow-hidden will-change-transform",
isFront ? "cursor-grab active:cursor-grabbing" : "pointer-events-none"
)}
style={{
left: 70,
top: 52,
width: 158,
height: 210,
borderRadius: 22,
transformStyle: "preserve-3d",
background: dark ? "#1c1c1c" : "#ffffff",
padding: 3,
boxShadow: "0 18px 34px -16px rgba(0,0,0,0.5), 0 0 0 1px var(--b-line)",
}}
initial={false}
animate={
isDropping
? { x: -18, y: 120, rotateY: 14, rotateZ: -5, scale: 0.94, opacity: 0.0, zIndex: 60 }
: {
x: depth * 26,
y: -depth * 18,
rotateY: 14,
rotateZ: 0,
scale: 1 - depth * 0.06,
opacity: p > 4 ? 0 : 1,
zIndex: 50 - p,
}
}
transition={
isDropping
? { duration: DROP_MS / 1000, ease: [0.4, 0, 1, 1], zIndex: { duration: 0 } }
: { type: "spring", stiffness: 240, damping: 24, zIndex: { duration: 0 }, opacity: { duration: 0.25 } }
}
drag={isFront && dropping === null}
dragSnapToOrigin
dragElastic={0.6}
onDragEnd={(_, info) => {
if (Math.hypot(info.offset.x, info.offset.y) > 70 || Math.hypot(info.velocity.x, info.velocity.y) > 500) swap();
}}
onTap={() => isFront && swap()}
role={isFront ? "button" : undefined}
tabIndex={isFront ? 0 : -1}
aria-hidden={!isFront}
aria-label={isFront ? `${card.label}. Press to send it to the back` : undefined}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
swap();
}
}}
>
<div className="relative h-full w-full overflow-hidden" style={{ borderRadius: 19 }}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={card.src} alt={card.alt} draggable={false} className="h-full w-full object-cover" />
<div
className="absolute inset-0 bg-black transition-opacity duration-300"
style={{ opacity: isFront ? 0 : 0.06 + depth * 0.05 }}
/>
</div>
</motion.div>
);
})}
{/* Label of the front card, outside the 3D stack so it stays crisp */}
<div className="pointer-events-none absolute" style={{ left: 84, top: 222, zIndex: 70 }}>
<AnimatePresence mode="popLayout" initial={false}>
{dropping === null && (
<motion.span
key={front}
initial={{ opacity: 0, y: 6, filter: "blur(4px)" }}
animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
exit={{ opacity: 0, y: 6, filter: "blur(4px)" }}
transition={{ duration: 0.22 }}
className={cn(
"inline-block rounded-full px-3 py-1.5 text-[13px] font-medium shadow-[0_4px_12px_-4px_rgba(0,0,0,0.35)]",
dark ? "bg-[#1c1c1c] text-white" : "bg-white text-[#171717]"
)}
>
{cards[front]?.label}
</motion.span>
)}
</AnimatePresence>
</div>
<span className="sr-only" aria-live="polite">
{cards[front]?.label}, card {front + 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;
}