Raw
1 "use client"
2
3 import { Check, TrianglesMini } from "@medusajs/icons"
4 import { cva } from "cva"
5 import { Select as RadixSelect } from "radix-ui"
6 import * as React from "react"
7
8 import { clx } from "@/utils/clx"
9
10 interface SelectProps
11 extends React.ComponentPropsWithoutRef<typeof RadixSelect.Root> {
12 size?: "base" | "small"
13 }
14
15 type SelectContextValue = {
16 size: "base" | "small"
17 }
18
19 const SelectContext = React.createContext<SelectContextValue | null>(null)
20
21 const useSelectContext = () => {
22 const context = React.useContext(SelectContext)
23
24 if (context === null) {
25 throw new Error("useSelectContext must be used within a SelectProvider")
26 }
27
28 return context
29 }
30
31 /**
32 * This component is based on [Radix UI Select](https://www.radix-ui.com/primitives/docs/components/select).
33 * It also accepts all props of the HTML `select` component.
34 */
35 const Root = ({
36 children,
37 /**
38 * The select's size.
39 */
40 size = "base",
41 ...props
42 }: SelectProps) => {
43 return (
44 <SelectContext.Provider value={React.useMemo(() => ({ size }), [size])}>
45 <RadixSelect.Root {...props}>{children}</RadixSelect.Root>
46 </SelectContext.Provider>
47 )
48 }
49 Root.displayName = "Select"
50
51 /**
52 * Groups multiple items together.
53 */
54 const Group = RadixSelect.Group
55 Group.displayName = "Select.Group"
56
57 /**
58 * Displays the selected value, or a placeholder if no value is selected.
59 * It's based on [Radix UI Select Value](https://www.radix-ui.com/primitives/docs/components/select#value).
60 */
61 const Value = RadixSelect.Value
62 Value.displayName = "Select.Value"
63
64 const triggerVariants = cva({
65 base: clx(
66 "bg-ui-bg-field shadow-buttons-neutral transition-fg flex w-full select-none items-center justify-between rounded-md outline-none",
67 "data-[placeholder]:text-ui-fg-muted text-ui-fg-base",
68 "hover:bg-ui-bg-field-hover",
69 "focus-visible:shadow-borders-interactive-with-active data-[state=open]:!shadow-borders-interactive-with-active",
70 "aria-[invalid=true]:border-ui-border-error aria-[invalid=true]:shadow-borders-error",
71 "invalid:border-ui-border-error invalid:shadow-borders-error",
72 "disabled:!bg-ui-bg-disabled disabled:!text-ui-fg-disabled",
73 "group/trigger"
74 ),
75 variants: {
76 size: {
77 base: "h-8 px-2 py-1.5 txt-compact-small",
78 small: "h-7 px-2 py-1 txt-compact-small",
79 },
80 },
81 })
82
83 /**
84 * The trigger that toggles the select.
85 * It's based on [Radix UI Select Trigger](https://www.radix-ui.com/primitives/docs/components/select#trigger).
86 */
87 const Trigger = React.forwardRef<
88 React.ElementRef<typeof RadixSelect.Trigger>,
89 React.ComponentPropsWithoutRef<typeof RadixSelect.Trigger>
90 >(({ className, children, ...props }, ref) => {
91 const { size } = useSelectContext()
92
93 return (
94 <RadixSelect.Trigger
95 ref={ref}
96 className={clx(triggerVariants({ size }), className)}
97 {...props}
98 >
99 {children}
100 <RadixSelect.Icon asChild>
101 <TrianglesMini className="text-ui-fg-muted group-disabled/trigger:text-ui-fg-disabled" />
102 </RadixSelect.Icon>
103 </RadixSelect.Trigger>
104 )
105 })
106 Trigger.displayName = "Select.Trigger"
107
108 interface SelectContentProps
109 extends React.ComponentPropsWithoutRef<typeof RadixSelect.Content> {}
110
111 /**
112 * The content that appears when the select is open.
113 * It's based on [Radix UI Select Content](https://www.radix-ui.com/primitives/docs/components/select#content).
114 */
115 const Content = React.forwardRef<
116 React.ElementRef<typeof RadixSelect.Content>,
117 SelectContentProps
118 >(
119 (
120 {
121 className,
122 children,
123 onScroll,
124 /**
125 * Whether to show the select items below (`popper`) or over (`item-aligned`) the select input.
126 */
127 position = "popper",
128 /**
129 * The distance of the content pop-up in pixels from the select input. Only available when position is set to popper.
130 */
131 sideOffset = 8,
132 /**
133 * The distance in pixels from the boundary edges where collision detection should occur. Only available when position is set to popper.
134 */
135 collisionPadding = 24,
136 ...props
137 }: SelectContentProps,
138 ref
139 ) => (
140 <RadixSelect.Portal>
141 <RadixSelect.Content
142 ref={ref}
143 className={clx(
144 "bg-ui-bg-component text-ui-fg-base shadow-elevation-flyout relative max-h-[200px] min-w-[var(--radix-select-trigger-width)] overflow-hidden rounded-lg",
145 "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95",
146 "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95",
147 "data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
148 {
149 "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1":
150 position === "popper",
151 },
152 className
153 )}
154 position={position}
155 sideOffset={sideOffset}
156 collisionPadding={collisionPadding}
157 {...props}
158 >
159 <RadixSelect.Viewport
160 className={clx(
161 "p-1",
162 position === "popper" &&
163 "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
164 )}
165 onScroll={onScroll}
166 >
167 {children}
168 </RadixSelect.Viewport>
169 </RadixSelect.Content>
170 </RadixSelect.Portal>
171 )
172 )
173 Content.displayName = "Select.Content"
174
175 /**
176 * Used to label a group of items.
177 * It's based on [Radix UI Select Label](https://www.radix-ui.com/primitives/docs/components/select#label).
178 */
179 const Label = React.forwardRef<
180 React.ElementRef<typeof RadixSelect.Label>,
181 React.ComponentPropsWithoutRef<typeof RadixSelect.Label>
182 >(({ className, ...props }, ref) => (
183 <RadixSelect.Label
184 ref={ref}
185 className={clx(
186 "txt-compact-xsmall-plus text-ui-fg-muted px-2 py-1.5",
187 className
188 )}
189 {...props}
190 />
191 ))
192 Label.displayName = "Select.Label"
193
194 /**
195 * An item in the select. It's based on [Radix UI Select Item](https://www.radix-ui.com/primitives/docs/components/select#item)
196 * and accepts its props.
197 */
198 const Item = React.forwardRef<
199 React.ElementRef<typeof RadixSelect.Item>,
200 React.ComponentPropsWithoutRef<typeof RadixSelect.Item>
201 >(({ className, children, ...props }, ref) => {
202 return (
203 <RadixSelect.Item
204 ref={ref}
205 className={clx(
206 "bg-ui-bg-component txt-compact-small grid cursor-pointer grid-cols-[15px_1fr] items-center gap-x-2 rounded-[4px] px-2 py-1.5 outline-none transition-colors",
207 "focus-visible:bg-ui-bg-component-hover",
208 "active:bg-ui-bg-component-pressed",
209 "data-[state=checked]:txt-compact-small-plus",
210 "disabled:text-ui-fg-disabled",
211 className
212 )}
213 {...props}
214 >
215 <span className="flex h-[15px] w-[15px] items-center justify-center">
216 <RadixSelect.ItemIndicator className="flex items-center justify-center">
217 <Check />
218 </RadixSelect.ItemIndicator>
219 </span>
220 <RadixSelect.ItemText className="flex-1 truncate">
221 {children}
222 </RadixSelect.ItemText>
223 </RadixSelect.Item>
224 )
225 })
226 Item.displayName = "Select.Item"
227
228 const Separator = React.forwardRef<
229 React.ElementRef<typeof RadixSelect.Separator>,
230 React.ComponentPropsWithoutRef<typeof RadixSelect.Separator>
231 >(({ className, ...props }, ref) => (
232 <RadixSelect.Separator
233 ref={ref}
234 className={clx(
235 "bg-ui-border-component border-t-ui-border-menu-top border-b-ui-border-menu-bot -mx-1 my-1 h-0.5 border-b border-t",
236 className
237 )}
238 {...props}
239 />
240 ))
241 Separator.displayName = "Select.Separator"
242
243 const Select = Object.assign(Root, {
244 Group,
245 Value,
246 Trigger,
247 Content,
248 Label,
249 Item,
250 Separator,
251 })
252
253 export { Select }