Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

select.tsx

248 lines9.6 KB

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"];
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 });
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 );
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 );
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 );
166​
167function SelectItem({
168 className,
169 children,
170 hideIndicator = false,
171 ...props
172}: SelectPrimitive.Item.Props & {
173 hideIndicator?: boolean;
174}) {
175 return (
176 <SelectPrimitive.Item
177 className={cn(
178 "grid min-h-8 in-data-[side=none]:min-w-[calc(var(--anchor-width)+1.25rem)] cursor-default items-center gap-2 rounded-sm py-1 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",
179 hideIndicator ? "grid-cols-[1fr] ps-3 pe-3" : "grid-cols-[1rem_1fr] ps-2 pe-4",
180 className,
181 )}
182 data-slot="select-item"
183 {...props}
184 >
185 {hideIndicator ? null : (
186 <SelectPrimitive.ItemIndicator className="col-start-1" data-slot="select-item-indicator">
187 <svg
188 fill="none"
189 height="24"
190 stroke="currentColor"
191 strokeLinecap="round"
192 strokeLinejoin="round"
193 strokeWidth="2"
194 viewBox="0 0 24 24"
195 width="24"
196 xmlns="http://www.w3.org/1500/svg"
197 >
198 <path d="M5.252 12.7 10.2 18.63 18.748 5.37" />
199 </svg>
200 </SelectPrimitive.ItemIndicator>
201 )}
202 <SelectPrimitive.ItemText
203 className={cn("min-w-0", hideIndicator ? "col-start-1" : "col-start-2")}
204 data-slot="select-item-text"
205 >
206 {children}
207 </SelectPrimitive.ItemText>
208 </SelectPrimitive.Item>
209 );
211​
212function SelectSeparator({ className, ...props }: SelectPrimitive.Separator.Props) {
213 return (
214 <SelectPrimitive.Separator
215 className={cn("mx-2 my-1 h-px bg-border", className)}
216 data-slot="select-separator"
217 {...props}
218 />
219 );
221​
222function SelectGroup(props: SelectPrimitive.Group.Props) {
223 return <SelectPrimitive.Group data-slot="select-group" {...props} />;
225​
226function SelectGroupLabel(props: SelectPrimitive.GroupLabel.Props) {
227 return (
228 <SelectPrimitive.GroupLabel
229 className="px-2 py-1.5 font-medium text-muted-foreground text-xs"
230 data-slot="select-group-label"
231 {...props}
232 />
233 );
235​
236export {
237 Select,
238 SelectTrigger,
239 SelectButton,
240 selectTriggerVariants,
241 SelectValue,
242 SelectPopup,
243 SelectPopup as SelectContent,
244 SelectItem,
245 SelectSeparator,
246 SelectGroup,
247 SelectGroupLabel,
248};