Open History to see the last commit
1"use client";
2
3import { mergeProps } from "@base-ui/react/merge-props";
4import { Select as SelectPrimitive } from "@base-ui/react/select";
5import { useRender } from "@base-ui/react/use-render";
6import { cva, type VariantProps } from "class-variance-authority";
7import { CheckIcon, ChevronDownIcon, ChevronsUpDownIcon, ChevronUpIcon } from "lucide-react";
8import type * as React from "react";
9
10import { cn } from "~/lib/utils";
11
12const Select = SelectPrimitive.Root;
13
14const selectTriggerVariants = cva(
15 "relative inline-flex cursor-pointer select-none items-center justify-between gap-2 border rounded-lg text-left text-base outline-none transition-[color,box-shadow,background-color] data-disabled:pointer-events-none data-disabled:opacity-64 sm:text-sm [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4",
16 {
17 defaultVariants: {
18 size: "default",
19 variant: "default",
20 },
21 variants: {
22 variant: {
23 default:
24 "w-full min-w-36 border-input bg-background not-dark:bg-clip-padding text-foreground shadow-xs/5 ring-ring/24 before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] not-data-disabled:not-focus-visible:not-aria-invalid:not-data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] pointer-coarse:after:absolute pointer-coarse:after:size-full pointer-coarse:after:min-h-11 focus-visible:border-ring focus-visible:ring-[3px] aria-invalid:border-destructive/36 focus-visible:aria-invalid:border-destructive/64 focus-visible:aria-invalid:ring-destructive/16 dark:bg-input/32 dark:aria-invalid:ring-destructive/24 dark:not-data-disabled:not-focus-visible:not-aria-invalid:not-data-pressed:before:shadow-[0_-1px_--theme(--color-white/6%)] [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='text-'])]:text-icon-muted [[data-disabled],:focus-visible,[aria-invalid],[data-pressed]]:shadow-none",
25 ghost:
26 "border-transparent text-secondary-label focus-visible:ring-2 focus-visible:ring-ring data-pressed:bg-accent [:hover,[data-pressed]]:bg-accent [:hover,[data-pressed]]:text-foreground",
27 },
28 size: {
29 compact:
30 "h-7 gap-1 rounded-md px-[calc(--spacing(2)-1px)] text-xs before:rounded-[calc(var(--radius-md)-1px)] [&_svg:not([class*='size-'])]:size-3.5",
31 default: "min-h-9 px-[calc(--spacing(3)-1px)] sm:min-h-8",
32 lg: "min-h-10 px-[calc(--spacing(3)-1px)] sm:min-h-9",
33 sm: "min-h-8 gap-1.5 px-[calc(--spacing(2.5)-1px)] sm:min-h-7",
34 xs: "h-7 gap-1 rounded-md px-[calc(--spacing(2)-1px)] text-sm before:rounded-[calc(var(--radius-md)-1px)] sm:h-6 sm:text-xs [&_svg:not([class*='size-'])]:size-4 sm:[&_svg:not([class*='size-'])]:size-3.5",
35 },
36 },
37 },
38);
39
40const selectTriggerIconClassName = "-me-1 size-4.5 opacity-80 sm:size-4";
41
42interface SelectButtonProps extends useRender.ComponentProps<"button"> {
43 size?: VariantProps<typeof selectTriggerVariants>["size"];
44 variant?: VariantProps<typeof selectTriggerVariants>["variant"];
45}
46
47function SelectButton({ className, size, variant, render, children, ...props }: SelectButtonProps) {
48 const typeValue: React.ButtonHTMLAttributes<HTMLButtonElement>["type"] = render
49 ? undefined
50 : "button";
51
52 const defaultProps = {
53 children: (
54 <>
55 <span className="flex-1 truncate in-data-placeholder:text-placeholder">{children}</span>
56 {variant === "ghost" ? (
57 <ChevronDownIcon className="-me-1 size-3 opacity-50" />
58 ) : (
59 <ChevronsUpDownIcon className={selectTriggerIconClassName} />
60 )}
61 </>
62 ),
63 className: cn(selectTriggerVariants({ size, variant }), "min-w-none", className),
64 "data-slot": "select-button",
65 type: typeValue,
66 };
67
68 return useRender({
69 defaultTagName: "button",
70 props: mergeProps<"button">(defaultProps, props),
71 render,
72 });
73}
74
75function SelectTrigger({
76 className,
77 size = "default",
78 variant = "default",
79 children,
80 icon,
81 ...props
82}: SelectPrimitive.Trigger.Props &
83 VariantProps<typeof selectTriggerVariants> & { icon?: React.ReactNode }) {
84 return (
85 <SelectPrimitive.Trigger
86 className={cn(selectTriggerVariants({ size, variant }), className)}
87 data-slot="select-trigger"
88 {...props}
89 >
90 {children}
91 <SelectPrimitive.Icon data-slot="select-icon">
92 {icon ?? <ChevronDownIcon className="-me-1 size-3 opacity-50" />}
93 </SelectPrimitive.Icon>
94 </SelectPrimitive.Trigger>
95 );
96}
97
98function SelectValue({ className, ...props }: SelectPrimitive.Value.Props) {
99 return (
100 <SelectPrimitive.Value
101 className={cn("flex-1 truncate data-placeholder:text-placeholder", className)}
102 data-slot="select-value"
103 {...props}
104 />
105 );
106}
107
108function SelectPopup({
109 className,
110 popupClassName,
111 children,
112 side = "bottom",
113 sideOffset = 4,
114 align = "start",
115 alignOffset = 0,
116 alignItemWithTrigger = true,
117 matchTriggerWidth = true,
118 anchor,
119 ...props
120}: SelectPrimitive.Popup.Props & {
121 popupClassName?: string;
122 side?: SelectPrimitive.Positioner.Props["side"];
123 sideOffset?: SelectPrimitive.Positioner.Props["sideOffset"];
124 align?: SelectPrimitive.Positioner.Props["align"];
125 alignOffset?: SelectPrimitive.Positioner.Props["alignOffset"];
126 alignItemWithTrigger?: SelectPrimitive.Positioner.Props["alignItemWithTrigger"];
127 matchTriggerWidth?: boolean;
128 anchor?: SelectPrimitive.Positioner.Props["anchor"];
129}) {
130 return (
131 <SelectPrimitive.Portal>
132 <SelectPrimitive.Positioner
133 align={align}
134 alignItemWithTrigger={alignItemWithTrigger}
135 alignOffset={alignOffset}
136 anchor={anchor}
137 className="z-[130] select-none"
138 data-slot="select-positioner"
139 side={side}
140 sideOffset={sideOffset}
141 >
142 <SelectPrimitive.Popup
143 className="origin-(--transform-origin) rounded-lg text-foreground outline-none"
144 data-slot="select-popup"
145 {...props}
146 >
147 <SelectPrimitive.ScrollUpArrow
148 className="top-0 z-50 flex h-6 w-full cursor-default items-center justify-center before:pointer-events-none before:absolute before:inset-x-px before:top-px before:h-[200%] before:rounded-t-[calc(var(--radius-lg)-1px)] before:bg-linear-to-b before:from-50% before:from-popover"
149 data-slot="select-scroll-up-arrow"
150 >
151 <ChevronUpIcon className="relative size-4.5 sm:size-4" />
152 </SelectPrimitive.ScrollUpArrow>
153 <div
154 className={cn(
155 "dropdown-glass relative h-full rounded-lg shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
156 matchTriggerWidth && "min-w-(--anchor-width)",
157 popupClassName,
158 )}
159 >
160 <SelectPrimitive.List
161 className={cn("max-h-(--available-height) overflow-y-auto p-1", className)}
162 data-slot="select-list"
163 >
164 {children}
165 </SelectPrimitive.List>
166 </div>
167 <SelectPrimitive.ScrollDownArrow
168 className="bottom-0 z-50 flex h-6 w-full cursor-default items-center justify-center before:pointer-events-none before:absolute before:inset-x-px before:bottom-px before:h-[200%] before:rounded-b-[calc(var(--radius-lg)-1px)] before:bg-linear-to-t before:from-50% before:from-popover"
169 data-slot="select-scroll-down-arrow"
170 >
171 <ChevronDownIcon className="relative size-4.5 sm:size-4" />
172 </SelectPrimitive.ScrollDownArrow>
173 </SelectPrimitive.Popup>
174 </SelectPrimitive.Positioner>
175 </SelectPrimitive.Portal>
176 );
177}
178
179function SelectItem({
180 className,
181 children,
182 hideIndicator: _hideIndicator = false,
183 showCheck = false,
184 ...props
185}: SelectPrimitive.Item.Props & {
186 hideIndicator?: boolean;
187 /** Render a check mark on selected items. Multi-selects use it so every chosen item is visible at once. */
188 showCheck?: boolean;
189}) {
190 return (
191 <SelectPrimitive.Item
192 className={cn(
193 "flex min-h-8 in-data-[side=none]:min-w-[calc(var(--anchor-width)+1.25rem)] cursor-pointer items-center rounded-sm px-2 py-1 text-base outline-none data-selected:bg-foreground/[0.08] data-disabled:pointer-events-none data-disabled:cursor-not-allowed data-highlighted:bg-accent data-highlighted:text-accent-foreground data-disabled:opacity-64 sm:min-h-7 sm:text-sm [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0",
194 showCheck && "gap-2",
195 className,
196 )}
197 data-slot="select-item"
198 {...props}
199 >
200 {showCheck ? (
201 <SelectPrimitive.ItemIndicator
202 className="flex w-4 shrink-0 items-center justify-center"
203 data-slot="select-item-indicator"
204 keepMounted
205 >
206 <CheckIcon className="in-data-[selected]:opacity-100 opacity-0" />
207 </SelectPrimitive.ItemIndicator>
208 ) : null}
209 <SelectPrimitive.ItemText
210 className="min-w-0 flex-1 [&_svg:not([class*='text-'])]:text-muted-foreground"
211 data-slot="select-item-text"
212 >
213 {children}
214 </SelectPrimitive.ItemText>
215 </SelectPrimitive.Item>
216 );
217}
218
219function SelectSeparator({ className, ...props }: SelectPrimitive.Separator.Props) {
220 return (
221 <SelectPrimitive.Separator
222 className={cn("mx-2 my-1 h-px bg-border", className)}
223 data-slot="select-separator"
224 {...props}
225 />
226 );
227}
228
229function SelectGroup(props: SelectPrimitive.Group.Props) {
230 return <SelectPrimitive.Group data-slot="select-group" {...props} />;
231}
232
233function SelectGroupLabel(props: SelectPrimitive.GroupLabel.Props) {
234 return (
235 <SelectPrimitive.GroupLabel
236 className="px-2 py-1.5 font-medium text-muted-foreground text-xs"
237 data-slot="select-group-label"
238 {...props}
239 />
240 );
241}
242
243export {
244 Select,
245 SelectTrigger,
246 SelectButton,
247 selectTriggerVariants,
248 SelectValue,
249 SelectPopup,
250 SelectPopup as SelectContent,
251 SelectItem,
252 SelectSeparator,
253 SelectGroup,
254 SelectGroupLabel,
255};