saladday1's picture
code: sync from git
61db3dc verified
Raw History Blame
1.26 kB
/** Two-way switch on a white card: the chosen option is a black pill, as on minimax.io's tabs. */
export function Segmented<T extends string>({ value, options, onChange, label }: {
value: T; options: [{ value: T; label: string }, { value: T; label: string }]; onChange: (v: T) => void; label: string
}) {
const i = options.findIndex((o) => o.value === value)
return (
<div role="radiogroup" aria-label={label}
className="relative grid h-10 shrink-0 grid-cols-2 rounded-full bg-muted p-1">
<span aria-hidden className="absolute inset-y-1 left-1 w-[calc(50%-4px)] rounded-full bg-[#171717] transition-transform duration-300 ease-[cubic-bezier(.22,.61,.36,1)]"
style={{ transform: `translateX(${i * 100}%)` }} />
{options.map((o) => {
const on = o.value === value
return (
<button key={o.value} type="button" role="radio" aria-checked={on} onClick={() => onChange(o.value)}
className={`relative z-10 rounded-full px-4 text-[15px] font-medium transition-colors duration-300 focus-visible:outline-2 focus-visible:outline-ring ${on ? "text-white" : "text-muted-foreground hover:text-foreground"}`}>
{o.label}
</button>
)
})}
</div>
)
}