"use client";
import { motion } from "motion/react";
import { useRef, useState } from "react";
/**
* Magnetic Select — an original take inspired by bencho.dev's "Magnetic select".
* Seven chips sit in a honeycomb. Pressing one swells it and lights its
* colour, while every other chip is shoved straight away from it, shrinking
* and leaning a little more the closer it sits.
*/
const R = 45; // honeycomb pitch
const HOME = [
{ x: 0, y: 0 },
...Array.from({ length: 6 }, (_, i) => {
const a = (-i * Math.PI) / 3; // 0°, -60°, … → E, NE, NW, W, SW, SE
return { x: Math.round(Math.cos(a) * R * 10) / 10, y: Math.round(Math.sin(a) * R * 10) / 10 };
}),
];
/** Original iridescent fills — layered CSS gradients, no raster assets. */
const SKINS = [
"radial-gradient(120% 90% at 78% 70%, #ff5fd2 0%, rgba(255,95,210,0) 58%), radial-gradient(90% 80% at 18% 30%, #57f0c9 0%, rgba(87,240,201,0) 62%), linear-gradient(135deg, #9b7bff, #ff86e1)",
"radial-gradient(110% 90% at 80% 75%, #ff3fa4 0%, rgba(255,63,164,0) 60%), radial-gradient(80% 80% at 20% 25%, #ffb347 0%, rgba(255,179,71,0) 65%), linear-gradient(140deg, #ff8a3d, #ff4fae)",
"radial-gradient(100% 90% at 75% 75%, #3f8cff 0%, rgba(63,140,255,0) 62%), radial-gradient(90% 80% at 20% 25%, #6ff7ff 0%, rgba(111,247,255,0) 60%), linear-gradient(135deg, #59c8ff, #6a5bff)",
"radial-gradient(100% 90% at 80% 70%, #a855f7 0%, rgba(168,85,247,0) 62%), radial-gradient(90% 90% at 18% 32%, #ffe14d 0%, rgba(255,225,77,0) 62%), linear-gradient(135deg, #f8e36b, #9a6bff)",
"radial-gradient(100% 90% at 78% 72%, #00c389 0%, rgba(0,195,137,0) 62%), radial-gradient(90% 80% at 22% 28%, #d9ff6a 0%, rgba(217,255,106,0) 62%), linear-gradient(135deg, #a7f36b, #16b98a)",
"radial-gradient(100% 90% at 80% 72%, #ff4d4d 0%, rgba(255,77,77,0) 62%), radial-gradient(90% 80% at 20% 28%, #ffd1e8 0%, rgba(255,209,232,0) 62%), linear-gradient(135deg, #ff9ec7, #ff5a5a)",
"radial-gradient(100% 90% at 80% 72%, #2b5cff 0%, rgba(43,92,255,0) 60%), radial-gradient(90% 80% at 20% 28%, #ff7ae0 0%, rgba(255,122,224,0) 62%), linear-gradient(135deg, #c77dff, #3d6bff)",
];
export type MagneticSelectProps = {
options?: string[];
defaultValue?: number;
onChange?: (index: number) => void;
/** 0–100 — how far the other chips are shoved. */
pull?: number;
/** 0–100 — spring bounce. */
bounce?: number;
size?: "small" | "large";
};
export function MagneticSelect({
options = ["Iris", "Ember", "Lagoon", "Citrine", "Moss", "Rose", "Dusk"],
defaultValue = 0,
onChange,
pull = 55,
bounce = 55,
size = "large",
}: MagneticSelectProps) {
const [sel, setSel] = useState(defaultValue);
const refs = useRef<(HTMLButtonElement | null)[]>([]);
const choose = (i: number) => {
if (i === sel) return;
setSel(i);
onChange?.(i);
};
// Radio-group keyboard model: arrows move the selection (wrapping), Home/End jump.
const onKeyDown = (e: React.KeyboardEvent) => {
const n = HOME.length;
const next =
e.key === "ArrowRight" || e.key === "ArrowDown"
? (sel + 1) % n
: e.key === "ArrowLeft" || e.key === "ArrowUp"
? (sel - 1 + n) % n
: e.key === "Home"
? 0
: e.key === "End"
? n - 1
: -1;
if (next < 0) return;
e.preventDefault();
choose(next);
refs.current[next]?.focus();
};
const chip = size === "large" ? 44 : 34;
const k = chip / 44;
const push = (pull / 55) * 14.13 * k;
const spring = { type: "spring" as const, stiffness: 520, damping: 34 - (bounce / 100) * 14, mass: 0.9 };
const target = (i: number) => {
if (i === sel) return { x: 0, y: 0, scale: 1.281, rotate: 0 };
const dx = (HOME[i].x - HOME[sel].x) * k;
const dy = (HOME[i].y - HOME[sel].y) * k;
const d = Math.hypot(dx, dy) / k;
const ux = dx / (d * k);
const uy = dy / (d * k);
return {
x: ux * push,
y: uy * push,
scale: 0.9095 + (d - R) * 0.000609,
rotate: 2.75 * ux * Math.sqrt(R / d),
};
};
return (
<div
role="radiogroup"
aria-label="Magnetic select"
onKeyDown={onKeyDown}
className="relative"
style={{ width: 4 * R * k + chip * 1.4, height: 4 * R * 0.866 * k + chip * 1.4 }}
>
{HOME.map((h, i) => {
const on = i === sel;
return (
<motion.button
key={i}
ref={(el) => {
refs.current[i] = el;
}}
type="button"
role="radio"
tabIndex={on ? 0 : -1}
aria-checked={on}
aria-label={options[i] ?? `Option ${i + 1}`}
className="absolute left-1/2 top-1/2 rounded-full outline-none focus-visible:ring-2 focus-visible:ring-[var(--b-fg)]/40"
style={{
width: chip,
height: chip,
marginLeft: h.x * k - chip / 2,
marginTop: h.y * k - chip / 2,
zIndex: on ? 2 : 1,
}}
initial={false}
animate={target(i)}
transition={spring}
whileTap={{ scale: on ? 1.24 : 0.86 }}
onClick={() => choose(i)}
>
<span className="absolute inset-0 overflow-hidden rounded-full bg-[var(--b-surface)] shadow-[var(--b-shadow)]">
<motion.span
aria-hidden
className="absolute inset-[-8%] rounded-full"
style={{ background: SKINS[i % SKINS.length], filter: "saturate(1.15)" }}
initial={false}
animate={{ opacity: on ? 1 : 0, scale: on ? 1 : 0.6, rotate: on ? 0 : -40 }}
transition={{ duration: on ? 0.32 : 0.2, ease: [0.2, 0, 0, 1] }}
/>
<motion.span
aria-hidden
className="absolute inset-0 rounded-full"
style={{ background: "radial-gradient(60% 50% at 32% 26%, rgba(255,255,255,0.55), rgba(255,255,255,0) 70%)", mixBlendMode: "soft-light" }}
initial={false}
animate={{ opacity: on ? 1 : 0 }}
/>
</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;
}