"use client";
import { AnimatePresence, animate, motion, useMotionValue, useTransform } from "motion/react";
import { useEffect, useRef, useState } from "react";
/**
* Slide to Confirm — an original take inspired by bencho.dev's "Slide to confirm".
* Drag the grip and a bright wash stretches behind it, swallowing the label.
* Let go near the end and the whole track floods and says "Confirmed".
*/
const TRACK_W = 280;
const TRACK_H = 56;
const PAD = 4;
const GRIP = TRACK_H - PAD * 2;
const MAX = TRACK_W - PAD * 2 - GRIP;
export function SlideToConfirm({
label = "Slide to confirm",
doneLabel = "Confirmed",
onConfirm,
resetAfter = 1600,
}: {
label?: string;
doneLabel?: string;
onConfirm?: () => void;
resetAfter?: number;
}) {
const x = useMotionValue(0);
const [done, setDone] = useState(false);
const [dragging, setDragging] = useState(false);
const start = useRef<{ px: number; x: number } | null>(null);
const timer = useRef<number>(0);
useEffect(() => () => window.clearTimeout(timer.current), []);
const washW = useTransform(x, (v) => v + GRIP);
const labelOpacity = useTransform(x, [0, MAX * 0.9], [1, 0.25]);
const arrowOpacity = useTransform(x, [MAX * 0.7, MAX * 0.96], [1, 0]);
const settle = (to: number, after?: () => void) =>
animate(x, to, { type: "spring", stiffness: 420, damping: to === 0 ? 30 : 38, onComplete: after });
const release = () => {
if (!start.current) return;
start.current = null;
setDragging(false);
if (x.get() > MAX * 0.86) {
settle(MAX);
setDone(true);
onConfirm?.();
timer.current = window.setTimeout(() => {
setDone(false);
settle(0);
}, resetAfter);
} else settle(0);
};
return (
<div
className="relative overflow-hidden rounded-[28px] bg-[var(--b-surface)] shadow-[var(--b-shadow)]"
style={{ width: TRACK_W, height: TRACK_H }}
>
<motion.span
style={{ opacity: labelOpacity, paddingLeft: GRIP }}
className="pointer-events-none absolute inset-0 flex items-center justify-center text-[14px] font-medium text-[var(--b-fg-muted)]"
>
{label}
</motion.span>
{/* the wash: grows from the left edge to the grip */}
<motion.i
aria-hidden
className="absolute rounded-[24px] bg-[var(--b-invert)]"
style={{ left: PAD, top: PAD, height: GRIP, width: washW }}
/>
<motion.button
type="button"
aria-label={label}
disabled={done}
className="absolute flex cursor-grab touch-none items-center justify-center rounded-[24px] bg-[var(--b-invert)] text-[var(--b-invert-fg)] outline-none active:cursor-grabbing"
style={{ left: PAD, top: PAD, width: GRIP, height: GRIP, x }}
animate={{ scale: dragging ? 0.97 : 1 }}
onPointerDown={(e) => {
if (done) return;
e.currentTarget.setPointerCapture(e.pointerId);
x.stop();
start.current = { px: e.clientX, x: x.get() };
setDragging(true);
}}
onPointerMove={(e) => {
if (!start.current) return;
const r = e.currentTarget.getBoundingClientRect();
const scale = r.width / GRIP; // stage may scale us
let nx = start.current.x + (e.clientX - start.current.px) / scale;
if (nx < 0) nx = nx / 4; // rubber band
if (nx > MAX) nx = MAX + (nx - MAX) / 6;
x.set(nx);
}}
onPointerUp={release}
onPointerCancel={release}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
start.current = { px: 0, x: 0 };
x.set(MAX);
release();
}
}}
>
<motion.span animate={{ opacity: done ? 0 : 1, x: dragging ? 2 : 0 }} transition={{ duration: 0.18 }}>
<motion.span className="block" style={{ opacity: arrowOpacity }}>
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<path d="M5 12h14" />
<path d="m12 5 7 7-7 7" />
</svg>
</motion.span>
</motion.span>
</motion.button>
<AnimatePresence>
{done && (
<motion.span
key="done"
className="pointer-events-none absolute inset-0 flex items-center justify-center gap-[8px] text-[14px] font-medium text-[var(--b-invert-fg)]"
initial={{ opacity: 0, scale: 0.7, filter: "blur(4px)" }}
animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }}
exit={{ opacity: 0, scale: 0.9, filter: "blur(3px)" }}
transition={{ type: "spring", stiffness: 420, damping: 28, delay: 0.06 }}
>
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<motion.path d="M20 6 9 17l-5-5" initial={{ pathLength: 0 }} animate={{ pathLength: 1 }} transition={{ delay: 0.12, duration: 0.28 }} />
</svg>
{doneLabel}
</motion.span>
)}
</AnimatePresence>
</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;
}