"use client";
import { AnimatePresence, motion } from "motion/react";
import { useState } from "react";
import { RollingDigits } from "./rolling-digits";
/**
* Like — an original take inspired by bencho.dev's "Like".
* The heart ducks to a dot, then blooms back filled while a violet disc
* hollows into a ring and a halo of confetti dots bursts outward. The count
* rolls digit by digit.
*/
const DOTS = ["#ff6b8b", "#c084fc", "#ffb454", "#5ad8c8", "#7aa2ff", "#ff6b8b", "#c084fc", "#ffd166"];
const RED = "#f4525c";
export function LikeButton({ defaultCount = 1300, defaultLiked = false }: { defaultCount?: number; defaultLiked?: boolean }) {
const [liked, setLiked] = useState(defaultLiked);
const [burst, setBurst] = useState(0);
const count = defaultCount + (liked ? 1 : 0) - (defaultLiked ? 1 : 0);
return (
<motion.button
type="button"
aria-pressed={liked}
aria-label={`Like. ${count.toLocaleString("en-US")} likes`}
onClick={() => {
setLiked((l) => !l);
if (!liked) setBurst((b) => b + 1);
}}
whileTap={{ scale: 0.96 }}
transition={{ type: "spring", stiffness: 600, damping: 30 }}
className="flex h-[44px] items-center gap-[8px] rounded-[22px] bg-[var(--b-surface)] pl-[16px] pr-[20px] text-[16px] font-medium text-[var(--b-fg)] shadow-[var(--b-shadow)] outline-none focus-visible:ring-2 focus-visible:ring-[var(--b-fg)]/30"
>
<span className="relative flex size-[20px] items-center justify-center">
{/* violet disc → ring */}
<AnimatePresence>
{liked && (
<motion.span
key={`ring${burst}`}
aria-hidden
className="pointer-events-none absolute left-1/2 top-1/2 size-[34px] -translate-x-1/2 -translate-y-1/2 rounded-full"
style={{ borderStyle: "solid", borderColor: "#d083d8" }}
initial={{ scale: 0.2, borderWidth: 17, opacity: 1 }}
animate={{ scale: [0.2, 0.8, 0.86, 1, 1.04], borderWidth: [17, 17, 17, 1.5, 0], opacity: [1, 1, 1, 1, 0] }}
exit={{ opacity: 0 }}
transition={{ duration: 0.38, times: [0, 0.22, 0.3, 0.5, 1], ease: "easeOut" }}
/>
)}
</AnimatePresence>
{/* confetti halo */}
{liked &&
DOTS.map((c, i) => {
const a = (i / DOTS.length) * Math.PI * 2 - Math.PI / 2;
const r0 = 14;
const r1 = 21 + (i % 2) * 2;
return (
<motion.span
key={`${burst}-${i}`}
aria-hidden
className="pointer-events-none absolute left-1/2 top-1/2 size-[2.5px] rounded-full"
style={{ background: c, marginLeft: -1.25, marginTop: -1.25 }}
initial={{ x: Math.cos(a) * r0, y: Math.sin(a) * r0, scale: 0, opacity: 0 }}
animate={{
x: [Math.cos(a) * r0, Math.cos(a) * r1, Math.cos(a + 0.25) * (r1 + 4)],
y: [Math.sin(a) * r0, Math.sin(a) * r1, Math.sin(a + 0.25) * (r1 + 4)],
scale: [0, 1, 0.4],
opacity: [0, 0.75, 0],
}}
transition={{ duration: 0.34, delay: 0.1, times: [0, 0.4, 1], ease: "easeOut" }}
/>
);
})}
<motion.svg
key={liked ? `on${burst}` : "off"}
width="20"
height="20"
viewBox="0 0 24 24"
fill={liked ? RED : "none"}
stroke={liked ? RED : "currentColor"}
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden
className="relative"
initial={liked ? { scale: 0 } : { scale: 0.85 }}
animate={liked ? { scale: [0, 0, 1.16, 1] } : { scale: 1 }}
transition={liked ? { duration: 0.42, times: [0, 0.3, 0.62, 1], ease: "easeOut" } : { type: "spring", stiffness: 500, damping: 22 }}
>
<path d="M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z" />
</motion.svg>
</span>
<RollingDigits value={count} tabular={false} />
</motion.button>
);
}
"use client";
import { AnimatePresence, motion } from "motion/react";
import { useState } from "react";
/**
* Per-digit odometer. Only the digits that change move: going up, the old
* digit rolls out the top while the new one rises in from below (and the
* reverse going down), with a touch of overshoot and motion blur.
*/
export function RollingDigits({
value,
className,
digitClassName,
group = true,
tabular = true,
format,
}: {
value: number;
/** Custom text for the value (e.g. 175.6K). Digits still roll by place. */
format?: (value: number) => string;
className?: string;
digitClassName?: string;
group?: boolean;
/** Equal-width digits (steppers). Turn off for inline counts. */
tabular?: boolean;
}) {
const [track, setTrack] = useState({ value, dir: 1 });
if (track.value !== value) setTrack({ value, dir: value > track.value ? 1 : -1 });
const dir = track.value !== value ? (value > track.value ? 1 : -1) : track.dir;
const text = format ? format(value) : group ? value.toLocaleString("en-US") : String(value);
const chars = text.split("");
// Key digits by their place value (counted from the right) so a carry
// only animates the places that actually changed.
const total = chars.filter((c) => /\d/.test(c)).length;
let seen = 0;
const keyed = chars.map((c) =>
/\d/.test(c)
? { c, key: `d${total - 1 - seen++}` }
: { c, key: `s${total - seen}` }
);
return (
<span className={`${tabular ? "bencho-tabular " : ""}relative inline-flex overflow-hidden ${className ?? ""}`} style={{ lineHeight: 1 }}>
<AnimatePresence initial={false} mode="popLayout">
{keyed.map(({ c, key }) =>
/\d/.test(c) ? (
<motion.span key={key} layout="position" className="relative inline-block" transition={{ layout: { type: "spring", stiffness: 500, damping: 40 } }}>
<AnimatePresence initial={false} mode="popLayout" custom={dir}>
<motion.span
key={c}
custom={dir}
className={`inline-block ${digitClassName ?? ""}`}
variants={{
enter: (d: number) => ({ y: d > 0 ? "70%" : "-70%", opacity: 0, filter: "blur(2px)" }),
center: { y: "0%", opacity: 1, filter: "blur(0px)" },
exit: (d: number) => ({ y: d > 0 ? "-70%" : "70%", opacity: 0, filter: "blur(2px)" }),
}}
initial="enter"
animate="center"
exit="exit"
transition={{ type: "spring", stiffness: 280, damping: 24, mass: 0.8, opacity: { duration: 0.22 } }}
>
{c}
</motion.span>
</AnimatePresence>
</motion.span>
) : (
<motion.span key={key} layout="position" className="inline-block" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>
{c}
</motion.span>
)
)}
</AnimatePresence>
</span>
);
}
"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;
}