"use client";
import { motion, useAnimationControls } from "motion/react";
import { useId, useState } from "react";
import { RefreshCw } from "lucide-react";
import { cn } from "@/lib/utils";
/**
* Palette — an original take inspired by bencho.dev's "Palette".
* A tray of swatches and a shuffle button. Each shuffle rolls a fresh
* palette in OKLCH (so the colours sit at an even brightness), and the
* swatches pinch into drops, swap colour at the tightest point, and open
* back up into tiles.
*/
// ── a small OKLCH → sRGB, gamut-fitted by easing chroma down ──────────────
function oklchToRgb(L: number, C: number, hDeg: number): [number, number, number] | null {
const h = (hDeg * Math.PI) / 180;
const a = C * Math.cos(h);
const b = C * Math.sin(h);
const l_ = L + 0.3963377774 * a + 0.2158037573 * b;
const m_ = L - 0.1055613458 * a - 0.0638541728 * b;
const s_ = L - 0.0894841775 * a - 1.291485548 * b;
const l = l_ ** 3, m = m_ ** 3, s = s_ ** 3;
const lin = [
4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
];
if (lin.some((v) => v < -0.0005 || v > 1.0005)) return null;
const enc = (v: number) => {
const x = Math.max(0, Math.min(1, v));
return Math.round(255 * (x <= 0.0031308 ? 12.92 * x : 1.055 * x ** (1 / 2.4) - 0.055));
};
return [enc(lin[0]), enc(lin[1]), enc(lin[2])];
}
function fit(L: number, C: number, h: number) {
for (let c = C; c >= 0; c -= 0.005) {
const rgb = oklchToRgb(L, c, h);
if (rgb) return `#${rgb.map((v) => v.toString(16).padStart(2, "0")).join("")}`;
}
return "#888888";
}
const MOODS = [
{ L: [0.72, 0.8], C: [0.13, 0.17] }, // candy
{ L: [0.62, 0.72], C: [0.15, 0.2] }, // vivid
{ L: [0.82, 0.9], C: [0.07, 0.11] }, // pastel
{ L: [0.5, 0.62], C: [0.1, 0.15] }, // deep
];
const rnd = (a: number, b: number) => a + Math.random() * (b - a);
/** A palette of n colours whose hues sit at least ~60° apart. */
export function rollPalette(n: number) {
const mood = MOODS[Math.floor(Math.random() * MOODS.length)];
const start = Math.random() * 360;
const step = 360 / n;
const hues = Array.from({ length: n }, (_, i) => (start + i * step + rnd(-step * 0.18, step * 0.18) + 360) % 360);
// shuffle so neighbours aren't always hue-adjacent
for (let i = hues.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[hues[i], hues[j]] = [hues[j], hues[i]];
}
return hues.map((h) => fit(rnd(mood.L[0], mood.L[1]), rnd(mood.C[0], mood.C[1]), h));
}
const START = ["#c7a0ff", "#00c896", "#52b0ff", "#ff74c8", "#ffb347"];
export function Palette({
colours = 4,
morph = 60,
corner = 14,
outlined = false,
onShuffle,
}: {
/** Swatches in the tray, 3–5. */
colours?: number;
/** 0–100: how far swatches pinch into drops during a swap. */
morph?: number;
/** Swatch corner radius in px. */
corner?: number;
outlined?: boolean;
onShuffle?: (colours: string[]) => void;
}) {
const n = Math.max(3, Math.min(5, Math.round(colours)));
const [cols, setCols] = useState<string[]>(START);
const [shown, setShown] = useState<string[]>(START);
const [tick, setTick] = useState(0);
const [busy, setBusy] = useState(false);
const goo = useId().replace(/:/g, "");
const [spin, setSpin] = useState(0);
const swatches = useAnimationControls();
const DUR = 0.56;
const shuffle = () => {
if (busy) return;
const next = rollPalette(5);
setCols(next);
setBusy(true);
setTick((t) => t + 1);
setSpin((s) => s + 180);
const p = morph / 100;
void swatches.start({
scale: [1, 1 - 0.3 * p, 1],
borderRadius: [corner, 28, corner],
y: [0, -3 * p, 0],
transition: { duration: DUR, ease: [0.45, 0, 0.2, 1], times: [0, 0.45, 1] },
});
window.setTimeout(() => setShown(next), DUR * 450);
window.setTimeout(() => setBusy(false), DUR * 1000 + 40);
onShuffle?.(next.slice(0, n));
};
const pinch = morph / 100;
return (
<div className="flex items-center gap-[4px]">
<svg width="0" height="0" className="absolute" aria-hidden>
<filter id={goo}>
<feGaussianBlur in="SourceGraphic" stdDeviation="3.2" result="b" />
<feColorMatrix in="b" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 20 -8" />
</filter>
</svg>
<div
role="list"
aria-label="Colour palette"
className={cn("flex h-[77px] items-center gap-[4px] rounded-[22px] bg-[var(--b-surface)] p-[10px]", outlined && "ring-1 ring-[var(--b-line)]")}
>
<div className="flex items-center gap-[4px]" style={{ filter: busy && pinch > 0.05 ? `url(#${goo})` : undefined }}>
{shown.slice(0, n).map((c, i) => (
<motion.div
key={i}
role="listitem"
aria-label={c}
title={c}
className="h-[56px] w-[52px] shrink-0"
style={{ background: c, borderRadius: corner }}
animate={swatches}
/>
))}
</div>
</div>
<motion.button
type="button"
onClick={shuffle}
whileTap={{ scale: 0.92 }}
aria-label="Shuffle palette"
className={cn(
"flex h-[77px] w-[48px] items-center justify-center rounded-[24px] bg-[var(--b-surface)] text-[var(--b-fg)] outline-none focus-visible:ring-2 focus-visible:ring-[var(--b-fg)]/30",
outlined && "ring-1 ring-[var(--b-line)]"
)}
>
<motion.span animate={{ rotate: spin }} transition={{ duration: DUR, ease: [0.4, 0, 0.2, 1] }}>
<RefreshCw className="size-[16px]" strokeWidth={2.2} />
</motion.span>
</motion.button>
<span className="sr-only" aria-live="polite">
{tick ? `New palette: ${cols.slice(0, n).join(", ")}` : ""}
</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;
}