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 { 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 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 [[data-disabled],:focus-visible,[aria-invalid],[data-pressed]]:shadow-none",
25 ghost:
26 "border-transparent text-muted-foreground/70 focus-visible:ring-2 focus-visible:ring-ring data-pressed:bg-accent [:hover,[data-pressed]]:bg-accent [:hover,[data-pressed]]:text-foreground/80",
27 },
28 size: {
29 default: "min-h-9 px-[calc(--spacing(3)-1px)] sm:min-h-8",
30 lg: "min-h-10 px-[calc(--spacing(3)-1px)] sm:min-h-9",
31 sm: "min-h-8 gap-1.5 px-[calc(--spacing(2.5)-1px)] sm:min-h-7",
32 },
33 },
34 },
35);
36
37const selectTriggerIconClassName = "-me-1 size-4.5 opacity-80 sm:size-4";
38
39interface SelectButtonProps extends useRender.ComponentProps<"button"> {
40 size?: VariantProps<typeof selectTriggerVariants>["size"];
41 variant?: VariantProps<typeof selectTriggerVariants>["variant"];
42}
43
44function SelectButton({ className, size, variant, render, children, ...props }: SelectButtonProps) {
45 const typeValue: React.ButtonHTMLAttributes<HTMLButtonElement>["type"] = render
46 ? undefined
47 : "button";
48
49 const defaultProps = {
50 children: (
51 <>
52 <span className="flex-1 truncate in-data-placeholder:text-muted-foreground/72">
53 {children}
54 </span>
55 {variant === "ghost" ? (
56 <ChevronDownIcon className="size-3 opacity-50" />
57 ) : (
58 <ChevronsUpDownIcon className={selectTriggerIconClassName} />
59 )}
60 </>
61 ),
62 className: cn(selectTriggerVariants({ size, variant }), "min-w-none", className),
63 "data-slot": "select-button",
64 type: typeValue,
65 };
66
67 return useRender({
68 defaultTagName: "button",
69 props: mergeProps<"button">(defaultProps, props),
70 render,
71 });
72}
73
74function SelectTrigger({
75 className,
76 size = "default",
77 variant = "default",
78 children,
79 ...props
80}: SelectPrimitive.Trigger.Props & VariantProps<typeof selectTriggerVariants>) {
81 return (
82 <SelectPrimitive.Trigger
83 className={cn(selectTriggerVariants({ size, variant }), className)}
84 data-slot="select-trigger"
85 {...props}
86 >
87 {children}
88 <SelectPrimitive.Icon data-slot="select-icon">
89 <ChevronDownIcon className="size-3 opacity-50" />
90 </SelectPrimitive.Icon>
91 </SelectPrimitive.Trigger>
92 );
93}
94
95function SelectValue({ className, ...props }: SelectPrimitive.Value.Props) {
96 return (
97 <SelectPrimitive.Value
98 className={cn("flex-1 truncate data-placeholder:text-muted-foreground", className)}
99 data-slot="select-value"
100 {...props}
101 />
102 );
103}
104
105function SelectPopup({
106 className,
107 children,
108 side = "bottom",
109 sideOffset = 4,
110 align = "start",
111 alignOffset = 0,
112 alignItemWithTrigger = true,
113 anchor,
114 ...props
115}: SelectPrimitive.Popup.Props & {
116 side?: SelectPrimitive.Positioner.Props["side"];
117 sideOffset?: SelectPrimitive.Positioner.Props["sideOffset"];
118 align?: SelectPrimitive.Positioner.Props["align"];
119 alignOffset?: SelectPrimitive.Positioner.Props["alignOffset"];
120 alignItemWithTrigger?: SelectPrimitive.Positioner.Props["alignItemWithTrigger"];
121 anchor?: SelectPrimitive.Positioner.Props["anchor"];
122}) {
123 return (
124 <SelectPrimitive.Portal>
125 <SelectPrimitive.Positioner
126 align={align}
127 alignItemWithTrigger={alignItemWithTrigger}
128 alignOffset={alignOffset}
129 anchor={anchor}
130 className="z-50 select-none"
131 data-slot="select-positioner"
132 side={side}
133 sideOffset={sideOffset}
134 >
135 <SelectPrimitive.Popup
136 className="origin-(--transform-origin) text-foreground"
137 data-slot="select-popup"
138 {...props}
139 >
140 <SelectPrimitive.ScrollUpArrow
141 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"
142 data-slot="select-scroll-up-arrow"
143 >
144 <ChevronUpIcon className="relative size-4.5 sm:size-4" />
145 </SelectPrimitive.ScrollUpArrow>
146 <div className="relative h-full min-w-(--anchor-width) rounded-lg border bg-popover not-dark:bg-clip-padding shadow-lg/5 before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] dark:before:shadow-[0_-1px_--theme(--color-white/6%)]">
147 <SelectPrimitive.List
148 className={cn("max-h-(--available-height) overflow-y-auto p-1", className)}
149 data-slot="select-list"
150 >
151 {children}
152 </SelectPrimitive.List>
153 </div>
154 <SelectPrimitive.ScrollDownArrow
155 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"
156 data-slot="select-scroll-down-arrow"
157 >
158 <ChevronDownIcon className="relative size-4.5 sm:size-4" />
159 </SelectPrimitive.ScrollDownArrow>
160 </SelectPrimitive.Popup>
161 </SelectPrimitive.Positioner>
162 </SelectPrimitive.Portal>
163 );
164}
165
166function SelectItem({ className, children, ...props }: SelectPrimitive.Item.Props) {
167 return (
168 <SelectPrimitive.Item
169 className={cn(
170 "grid min-h-8 in-data-[side=none]:min-w-[calc(var(--anchor-width)+1.25rem)] cursor-default grid-cols-[1rem_1fr] items-center gap-2 rounded-sm py-1 ps-2 pe-4 text-base outline-none data-disabled:pointer-events-none 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",
171 className,
172 )}
173 data-slot="select-item"
174 {...props}
175 >
176 <SelectPrimitive.ItemIndicator className="col-start-1">
177 <svg
178 fill="none"
179 height="24"
180 stroke="currentColor"
181 strokeLinecap="round"
182 strokeLinejoin="round"
183 strokeWidth="2"
184 viewBox="0 0 24 24"
185 width="24"
186 xmlns="http://www.w3.org/1500/svg"
187 >
188 <path d="M5.252 12.7 10.2 18.63 18.748 5.37" />
189 </svg>
190 </SelectPrimitive.ItemIndicator>
191 <SelectPrimitive.ItemText className="col-start-2 min-w-0">
192 {children}
193 </SelectPrimitive.ItemText>
194 </SelectPrimitive.Item>
195 );
196}
197
198function SelectSeparator({ className, ...props }: SelectPrimitive.Separator.Props) {
199 return (
200 <SelectPrimitive.Separator
201 className={cn("mx-2 my-1 h-px bg-border", className)}
202 data-slot="select-separator"
203 {...props}
204 />
205 );
206}
207
208function SelectGroup(props: SelectPrimitive.Group.Props) {
209 return <SelectPrimitive.Group data-slot="select-group" {...props} />;
210}
211
212function SelectGroupLabel(props: SelectPrimitive.GroupLabel.Props) {
213 return (
214 <SelectPrimitive.GroupLabel
215 className="px-2 py-1.5 font-medium text-muted-foreground text-xs"
216 data-slot="select-group-label"
217 {...props}
218 />
219 );
220}
221
222export {
223 Select,
224 SelectTrigger,
225 SelectButton,
226 selectTriggerVariants,
227 SelectValue,
228 SelectPopup,
229 SelectPopup as SelectContent,
230 SelectItem,
231 SelectSeparator,
232 SelectGroup,
233 SelectGroupLabel,
234};