import { RadioGroup } from "@headlessui/react"; import { motion } from "framer-motion"; import type { ReactNode } from "react"; import { cn } from "~/utils/cn"; const sizes = { small: { control: "h-6", option: "px-2 text-xs", container: "gap-x-0.5", }, medium: { control: "h-10", option: "px-3 py-[0.13rem] text-sm", container: "p-1 gap-x-0.5", }, }; const theme = { primary: { base: "bg-background-raised", active: "text-text-bright hover:bg-background-hover/50", inactive: "text-text-dimmed transition hover:text-text-bright", selected: "absolute inset-0 rounded-[2px] outline-solid outline-3 outline-primary", }, secondary: { base: "bg-background-raised/50", active: "text-text-bright", inactive: "text-text-dimmed transition hover:text-text-bright", selected: "absolute inset-0 rounded bg-background-raised border border-border-bright", }, }; type Size = keyof typeof sizes; type Theme = keyof typeof theme; type VariantStyle = { base: string; active: string; inactive: string; option: string; container: string; selected: string; }; function createVariant(sizeName: Size, themeName: Theme): VariantStyle { return { base: cn(sizes[sizeName].control, theme[themeName].base), active: theme[themeName].active, inactive: theme[themeName].inactive, option: sizes[sizeName].option, container: sizes[sizeName].container, selected: theme[themeName].selected, }; } const variants = { "primary/small": createVariant("small", "primary"), "primary/medium": createVariant("medium", "primary"), "secondary/small": createVariant("small", "secondary"), "secondary/medium": createVariant("medium", "secondary"), } as const; type VariantType = keyof typeof variants; type Options = { label: ReactNode; value: string; }; type SegmentedControlProps = { name: string; value?: string; defaultValue?: string; options: Options[]; variant?: VariantType; fullWidth?: boolean; onChange?: (value: string) => void; }; export default function SegmentedControl({ name, value, defaultValue, options, variant = "secondary/medium", fullWidth, onChange, }: SegmentedControlProps) { const variantStyle = variants[variant]; const _isPrimary = variant.startsWith("primary"); return (