File size: 1,260 Bytes
61db3dc
2471af3
 
 
 
 
 
61db3dc
 
2471af3
 
 
 
 
61db3dc
2471af3
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** 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>
  )
}