"use client";
import { animate, motion, useMotionValue, useSpring, useTransform, useVelocity } from "motion/react";
import { useEffect, useRef, useState } from "react";
/**
* Escape Button — an original take inspired by bencho.dev's "Escape button".
* "Touch me" darts away whenever the pointer closes in, each dodge a little
* shorter as it tires, until it gives up with a bright "Fine."
*/
const FIELD_W = 300;
const FIELD_H = 240;
const BTN_W = 122;
const LIMIT = (FIELD_W - BTN_W) / 2 - 2;
const NEAR = 34; // px of breathing room around the button
const GIVE_UP_AFTER = 7;
export function EscapeButton() {
const tx = useMotionValue(0);
const x = useSpring(tx, { stiffness: 340, damping: 22, mass: 0.7 });
const vel = useVelocity(x);
const skew = useTransform(vel, [-1600, 0, 1600], [7, 0, -7], { clamp: true });
const [dodges, setDodges] = useState(0);
const [state, setState] = useState<"flee" | "tired" | "caught">("flee");
const field = useRef<HTMLDivElement>(null);
const btn = useRef<HTMLButtonElement>(null);
const last = useRef(0);
const timer = useRef(0);
useEffect(() => () => window.clearTimeout(timer.current), []);
const onMove = (e: React.PointerEvent) => {
if (state !== "flee" || !btn.current || !field.current) return;
const fr = field.current.getBoundingClientRect();
const scale = fr.width / FIELD_W;
const br = btn.current.getBoundingClientRect();
const px = (e.clientX - fr.left) / scale;
const py = (e.clientY - fr.top) / scale;
const bx = (br.left - fr.left) / scale;
const by = (br.top - fr.top) / scale;
const bw = br.width / scale;
const bh = br.height / scale;
const inside =
px > bx - NEAR && px < bx + bw + NEAR && py > by - NEAR * 0.8 && py < by + bh + NEAR * 0.8;
const now = performance.now();
if (!inside || now - last.current < 140) return;
last.current = now;
const n = dodges + 1;
setDodges(n);
const cx = bx + bw / 2 - FIELD_W / 2;
const from = px - FIELD_W / 2;
// dart to the side away from the pointer; shorter hops as it tires
const hop = 58 - n * 3;
let next = from < cx ? cx + hop : cx - hop;
// out of room on that side: hug the wall, or double back past the pointer
if (next > LIMIT) next = LIMIT - cx > 24 ? LIMIT : cx - hop * 1.6;
if (next < -LIMIT) next = -LIMIT - cx < -24 ? -LIMIT : cx + hop * 1.6;
next = Math.max(-LIMIT, Math.min(LIMIT, next));
tx.set(next);
if (n >= GIVE_UP_AFTER) {
timer.current = window.setTimeout(() => setState("tired"), 380);
}
};
const onClick = (e: React.MouseEvent) => {
// A keyboard press (detail === 0) can't be dodged, so it skips the chase.
if (state === "flee" && e.detail === 0) {
setState("tired");
return;
}
if (state !== "tired") return;
setState("caught");
timer.current = window.setTimeout(() => {
setDodges(0);
setState("flee");
animate(tx, 0, { duration: 0 });
}, 900);
};
const bright = state !== "flee";
return (
<div
ref={field}
onPointerMove={onMove}
className="relative flex items-center justify-center"
style={{ width: FIELD_W, height: FIELD_H }}
>
<motion.button
ref={btn}
type="button"
onClick={onClick}
style={{ x, skewX: skew }}
animate={{
backgroundColor: bright ? "var(--b-invert)" : "var(--b-surface)",
color: bright ? "var(--b-invert-fg)" : "color-mix(in srgb, var(--b-fg) 76%, transparent)",
scale: state === "caught" ? [1, 0.92, 1.04, 1] : 1,
opacity: state === "caught" ? [1, 1, 1, 0.6] : 1,
}}
transition={{ duration: state === "caught" ? 0.8 : 0.28 }}
className="relative h-[44px] rounded-[22px] text-[16px] font-medium shadow-[var(--b-shadow)] outline-none"
data-state={state}
>
<motion.span layout className="block px-[22px]" style={{ minWidth: state === "flee" ? BTN_W : undefined }}>
<motion.span key={state} initial={{ opacity: 0, filter: "blur(3px)" }} animate={{ opacity: 1, filter: "blur(0px)" }} transition={{ duration: 0.2 }} className="block">
{state === "tired" ? "Fine." : "Touch me"}
</motion.span>
</motion.span>
</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;
}