File size: 1,318 Bytes
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 the dark stage: a white pill slides under the chosen option. */
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 border border-white/15 bg-white/[0.06] p-1 backdrop-blur">
      <span aria-hidden className="absolute inset-y-1 left-1 w-[calc(50%-4px)] rounded-full bg-white shadow-[0_4px_14px_rgba(0,0,0,0.3)] 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-white/60 ${on ? "text-[#171717]" : "text-white/70 hover:text-white"}`}>
            {o.label}
          </button>
        )
      })}
    </div>
  )
}