1 import { Button, clx } from "@medusajs/ui"
2 import { ReactNode } from "react"
3
4 export type SegmentedControlOption = {
5 value: string
6 label: ReactNode
7 }
8
9 type SegmentedControlProps = {
10 value: string
11 onValueChange: (value: string) => void
12 options: SegmentedControlOption[]
13 className?: string
14 }
15
16 export const SegmentedControl = ({
17 value,
18 onValueChange,
19 options,
20 className,
21 }: SegmentedControlProps) => {
22 return (
23 <div
24 className={clx(
25 "bg-ui-bg-disabled grid items-center gap-x-[1px] rounded-md p-[1px]",
26 className
27 )}
28 style={{
29 gridTemplateColumns: `repeat(${options.length}, 1fr)`,
30 }}
31 >
32 {options.map((option) => {
33 const isSelected = value === option.value
34
35 return (
36 <Button
37 key={option.value}
38 size="small"
39 onClick={() => onValueChange(option.value)}
40 variant={isSelected ? "secondary" : "transparent"}
41 type="button"
42 className={clx(
43 "w-auto",
44 !isSelected && "hover:text-ui-fg-base text-ui-fg-muted"
45 )}
46 >
47 {option.label}
48 </Button>
49 )
50 })}
51 </div>
52 )
53 }