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