accesscodes/src/components/segmented.tsx
anonymousratwastaken ebfa25d1e8 Rework donate flow into guided steps with gift options
- Split the donate page into step components and reorder the flow: intent, rank or amount, recipient, gift, upgrade target, then confirm
- Let a gift go to a player on the other edition, and allow anonymous gifts (donor is left blank in metadata)
- Look up the recipient's current rank for upgrades and disable ranks they already hold
- Validate custom amounts on the server as integers between CUSTOM_MIN and CUSTOM_MAX
- Show friendly messages for checkout errors and send users back to the step that needs fixing
- Build the Stripe appearance from the page's glow accent colour, and tint the glow to match a custom amount's unlocked tier
2026-10-24 16:40:08 +01:00

68 lines
2.5 KiB
TypeScript

"use client";
import { useId } from "react";
import { motion, useReducedMotion } from "motion/react";
import { cn } from "@/lib/utils";
// A two-or-more way switch: big halves with a pill in the accent colour that
// slides to the chosen one. `value` may be "" to leave every option unchosen.
export function Segmented<T extends string>({ options, value, onChange, label, compact = false, className }: {
options: readonly { id: T; label: string }[];
value: T | "";
onChange: (id: T) => void;
label: string;
compact?: boolean;
className?: string;
}) {
const reduced = useReducedMotion() ?? false;
const pillId = useId();
return (
<div
role="radiogroup"
aria-label={label}
style={{ gridTemplateColumns: `repeat(${options.length}, minmax(0, 1fr))` }}
className={cn(
"grid w-full rounded-full border border-white/15 bg-black/35 p-1",
compact ? "h-9 max-w-44" : "h-14 max-w-xs",
className
)}
>
{options.map((o, i) => {
const active = o.id === value;
return (
<button
key={o.id}
type="button"
role="radio"
aria-checked={active}
tabIndex={active || (!value && i === 0) ? 0 : -1}
onClick={() => onChange(o.id)}
onKeyDown={(e) => {
if (e.key !== "ArrowLeft" && e.key !== "ArrowRight") return;
e.preventDefault();
const step = e.key === "ArrowRight" ? 1 : -1;
const next = options[(i + step + options.length) % options.length];
onChange(next.id);
(e.currentTarget.parentElement?.children[options.indexOf(next)] as HTMLElement | undefined)?.focus();
}}
className={cn(
"relative rounded-full font-semibold outline-none transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-black",
compact ? "text-xs" : "text-sm",
active ? "text-primary-foreground" : "text-white/70 hover:text-white"
)}
>
{active && (
<motion.span
layoutId={pillId}
aria-hidden="true"
className="absolute inset-0 rounded-full bg-primary"
transition={reduced ? { duration: 0 } : { type: "spring", stiffness: 420, damping: 34 }}
/>
)}
<span className="relative">{o.label}</span>
</button>
);
})}
</div>
);
}