"use client"; import { useRef, useState } from "react"; import { motion, useReducedMotion } from "motion/react"; import { Check } from "lucide-react"; import { TIERS } from "@/lib/donations"; import { useIsWide } from "@/components/donate-hand"; import { cn } from "@/lib/utils"; // The "which rank?" step as a podium: the top rank stands tallest in the // middle, the others step down on either side. Blocks grow up from the floor // when the step opens, rise a little under the pointer, and the perks of the // rank being looked at are written underneath. export type Rank = "iron" | "gold" | "diamond"; type Step = { id: Rank; // Block height in rem on narrow and wide screens. narrow: number; wide: number; // Light and deep tone, matching the page glow this rank selects. light: string; deep: string; }; // Left to right, so keyboard order matches what is on screen. const PODIUM: Step[] = [ { id: "gold", narrow: 11, wide: 12.5, light: "#fbbf24", deep: "#f59e0b" }, { id: "diamond", narrow: 13, wide: 15, light: "#22d3ee", deep: "#06b6d4" }, { id: "iron", narrow: 9, wide: 10.5, light: "#cbd5e1", deep: "#64748b" }, ]; // Centre first, then outwards. const ENTRANCE = [1, 0, 2]; const SPRING = { type: "spring", stiffness: 320, damping: 30, mass: 0.8 } as const; export function DonatePodium({ value, onSelect, onPreview, ariaLabel = "Which rank?", disabled = [], price, note }: { value: string; onSelect: (id: Rank) => void; onPreview: (id: Rank | null) => void; ariaLabel?: string; // Ranks that stand on the podium but cannot be picked. disabled?: Rank[]; // Overrides for the line under a rank's name (null hides it) and a small // caption below that. The default is the rank's full price. price?: (id: Rank) => string | null; note?: (id: Rank) => string | undefined; }) { const reduced = useReducedMotion() ?? false; const wide = useIsWide(); const [hovered, setHovered] = useState(null); const hoveredRef = useRef(null); const buttons = useRef<(HTMLButtonElement | null)[]>([]); const selected = PODIUM.findIndex((p) => p.id === value); const off = (i: number) => disabled.includes(PODIUM[i].id); const firstOn = PODIUM.findIndex((_, i) => !off(i)); const tabStop = selected >= 0 && !off(selected) ? selected : firstOn; const shown = hovered ?? (selected >= 0 ? selected : null); // One owner for the lifted block and the glow preview. A block's blur only // clears it when that block holds it, so pressing another block (which moves // focus) cannot flash the old colour. function hover(i: number | null) { hoveredRef.current = i; setHovered(i); onPreview(i === null ? null : PODIUM[i].id); } // Move focus to the next pickable block in a direction, wrapping round. function focusStep(from: number, dir: 1 | -1) { for (let n = 1; n <= PODIUM.length; n++) { const i = (from + dir * n + PODIUM.length * n) % PODIUM.length; if (!off(i)) { buttons.current[i]?.focus(); return; } } } function onKeyDown(e: React.KeyboardEvent, i: number) { if (e.key === "ArrowRight" || e.key === "ArrowDown") { e.preventDefault(); focusStep(i, 1); } else if (e.key === "ArrowLeft" || e.key === "ArrowUp") { e.preventDefault(); focusStep(i, -1); } } return (
hover(null)} > {PODIUM.map((step, i) => { const tier = TIERS[step.id]; const unavailable = off(i); const checked = i === selected && !unavailable; const lifted = hovered === i; const faded = selected >= 0 && !checked && !lifted && !unavailable; const priceText = price ? price(step.id) : `£${(tier.price / 100).toFixed(2)}`; const noteText = note?.(step.id); const bump = checked ? 0.75 : lifted ? 0.5 : 0; return ( { buttons.current[i] = el; }} type="button" role="radio" aria-checked={checked} aria-disabled={unavailable || undefined} tabIndex={i === tabStop ? 0 : -1} onClick={() => { if (!unavailable) onSelect(step.id); }} onKeyDown={(e) => onKeyDown(e, i)} onPointerEnter={(e) => { if (e.pointerType !== "touch" && !unavailable) hover(i); }} onFocus={(e) => { if (e.currentTarget.matches(":focus-visible")) hover(i); }} onBlur={() => { if (hoveredRef.current === i) hover(null); }} initial={reduced ? false : { height: 0 }} animate={{ height: ((wide ? step.wide : step.narrow) + bump) * 16, opacity: unavailable ? 0.45 : faded ? 0.8 : 1, }} transition={reduced ? { duration: 0 } : { ...SPRING, delay: ENTRANCE[i] * 0.07 }} style={{ "--c": step.light, "--c2": step.deep, } as React.CSSProperties} className={cn( "relative flex min-w-0 flex-1 flex-col overflow-hidden rounded-t-2xl border border-b-0 p-3 text-start select-none md:p-4", "bg-[color-mix(in_oklab,var(--c2)_20%,oklch(0.19_0.006_286))]", "shadow-[inset_0_1px_0_color-mix(in_oklab,var(--c)_45%,transparent),0_-14px_24px_-16px_rgb(0_0_0/0.6)]", "focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-4 focus-visible:outline-(--c)", checked ? "border-(--c)" : "border-[color-mix(in_oklab,var(--c)_20%,transparent)]", unavailable ? "cursor-not-allowed" : "cursor-pointer", "transition-[border-color] duration-200" )} > {tier.label} {priceText !== null && {priceText}} {noteText && {noteText}} ); })}
{/* The floor the podium stands on. */} ); }