Repository navigation
Expand file tree
/
Copy pathcombobox.tsx
More file actions
279 lines (262 loc) · 9.78 KB
/
Copy pathcombobox.tsx
File metadata and controls
279 lines (262 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { Combobox as ComboboxPrimitive } from "@base-ui/react/combobox";
import { ChevronsUpDownIcon, SearchIcon, XIcon } from "lucide-react";
import * as React from "react";
import { cn } from "~/lib/utils";
import { Input } from "~/components/ui/input";
import { ScrollArea } from "~/components/ui/scroll-area";
const ComboboxContext = React.createContext<{
chipsRef: React.RefObject<Element | null> | null;
multiple: boolean;
}>({
chipsRef: null,
multiple: false,
});
function Combobox<Value, Multiple extends boolean | undefined = false>(
props: ComboboxPrimitive.Root.Props<Value, Multiple>,
) {
const chipsRef = React.useRef<Element | null>(null);
const value = { chipsRef, multiple: !!props.multiple };
return (
<ComboboxContext value={value}>
<ComboboxPrimitive.Root {...props} />
</ComboboxContext>
);
}
function ComboboxInput({
className,
showTrigger = true,
showClear = false,
startAddon,
size,
unstyled = false,
...props
}: Omit<ComboboxPrimitive.Input.Props, "size"> & {
showTrigger?: boolean;
showClear?: boolean;
startAddon?: React.ReactNode;
size?: "sm" | "default" | "lg" | number;
unstyled?: boolean;
ref?: React.Ref<HTMLInputElement>;
}) {
const sizeValue = (size ?? "default") as "sm" | "default" | "lg" | number;
return (
<div className="relative not-has-[>*.w-full]:w-fit w-full text-foreground has-disabled:opacity-64">
{startAddon && (
<div
aria-hidden="true"
className="[&_svg]:-mx-0.5 pointer-events-none absolute inset-y-0 start-px z-10 flex items-center ps-[calc(--spacing(3)-1px)] opacity-80 has-[+[data-size=sm]]:ps-[calc(--spacing(2.5)-1px)] [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4"
data-slot="combobox-start-addon"
>
{startAddon}
</div>
)}
<ComboboxPrimitive.Input
className={cn(
startAddon &&
"data-[size=sm]:*:data-[slot=combobox-input]:ps-[calc(--spacing(7.5)-1px)] *:data-[slot=combobox-input]:ps-[calc(--spacing(8.5)-1px)] sm:data-[size=sm]:*:data-[slot=combobox-input]:ps-[calc(--spacing(7)-1px)] sm:*:data-[slot=combobox-input]:ps-[calc(--spacing(8)-1px)]",
sizeValue === "sm"
? "has-[+[data-slot=combobox-trigger],+[data-slot=combobox-clear]]:*:data-[slot=combobox-input]:pe-6.5"
: "has-[+[data-slot=combobox-trigger],+[data-slot=combobox-clear]]:*:data-[slot=combobox-input]:pe-7",
className,
)}
data-slot="combobox-input"
render={
<Input
className={cn(
"has-disabled:opacity-100",
unstyled && "rounded-none bg-transparent text-sm",
)}
nativeInput
size={sizeValue}
unstyled={unstyled}
/>
}
{...props}
/>
{showTrigger && (
<ComboboxTrigger
className={cn(
"-translate-y-1/2 absolute top-1/2 inline-flex size-8 shrink-0 cursor-pointer items-center justify-center rounded-md border border-transparent opacity-80 outline-none transition-opacity pointer-coarse:after:absolute pointer-coarse:after:min-h-11 pointer-coarse:after:min-w-11 hover:opacity-100 has-[+[data-slot=combobox-clear]]:hidden sm:size-7 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0",
sizeValue === "sm" ? "end-0" : "end-0.5",
)}
>
<ComboboxPrimitive.Icon data-slot="combobox-icon">
<ChevronsUpDownIcon />
</ComboboxPrimitive.Icon>
</ComboboxTrigger>
)}
{showClear && (
<ComboboxClear
className={cn(
"-translate-y-1/2 absolute top-1/2 inline-flex size-8 shrink-0 cursor-pointer items-center justify-center rounded-md border border-transparent opacity-80 outline-none transition-opacity pointer-coarse:after:absolute pointer-coarse:after:min-h-11 pointer-coarse:after:min-w-11 hover:opacity-100 has-[+[data-slot=combobox-clear]]:hidden sm:size-7 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0",
sizeValue === "sm" ? "end-0" : "end-0.5",
)}
>
<XIcon />
</ComboboxClear>
)}
</div>
);
}
function ComboboxSearchInput(props: React.ComponentProps<typeof ComboboxInput>) {
return (
<div className="min-w-0 shrink-0 px-3 pt-2.5">
<div className="relative -translate-y-px border-b border-border/70 pb-1.5 transition-colors focus-within:border-ring">
<SearchIcon
aria-hidden="true"
className="pointer-events-none absolute top-1.5 left-0 size-4 shrink-0 text-muted-foreground/55"
/>
<ComboboxInput
{...props}
className="[&_input]:h-6.5 [&_input]:ps-5 [&_input]:font-sans [&_input]:leading-6.5"
showTrigger={false}
size="sm"
unstyled
/>
</div>
</div>
);
}
function ComboboxTrigger({ className, children, ...props }: ComboboxPrimitive.Trigger.Props) {
return (
<ComboboxPrimitive.Trigger className={className} data-slot="combobox-trigger" {...props}>
{children}
</ComboboxPrimitive.Trigger>
);
}
function ComboboxPopup({
className,
children,
side = "bottom",
sideOffset = 4,
alignOffset,
align = "start",
anchor: anchorProp,
...props
}: ComboboxPrimitive.Popup.Props & {
align?: ComboboxPrimitive.Positioner.Props["align"];
sideOffset?: ComboboxPrimitive.Positioner.Props["sideOffset"];
alignOffset?: ComboboxPrimitive.Positioner.Props["alignOffset"];
side?: ComboboxPrimitive.Positioner.Props["side"];
anchor?: ComboboxPrimitive.Positioner.Props["anchor"];
}) {
const { chipsRef, multiple } = React.use(ComboboxContext);
const anchor = anchorProp ?? (multiple ? chipsRef : undefined);
return (
<ComboboxPrimitive.Portal>
<ComboboxPrimitive.Positioner
align={align}
alignOffset={alignOffset}
anchor={anchor}
className="z-[130] select-none"
data-slot="combobox-positioner"
side={side}
sideOffset={sideOffset}
>
<span
className={cn(
"dropdown-glass relative flex max-h-full min-w-(--anchor-width) max-w-(--available-width) origin-(--transform-origin) rounded-lg shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] transition-[scale,opacity] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
className,
)}
>
<ComboboxPrimitive.Popup
className="flex min-w-0 max-h-[min(var(--available-height),23rem)] flex-1 flex-col overflow-hidden text-foreground"
data-slot="combobox-popup"
{...props}
>
{children}
</ComboboxPrimitive.Popup>
</span>
</ComboboxPrimitive.Positioner>
</ComboboxPrimitive.Portal>
);
}
function ComboboxItem({
className,
children,
hideIndicator: _hideIndicator = false,
...props
}: ComboboxPrimitive.Item.Props & {
hideIndicator?: boolean;
}) {
return (
<ComboboxPrimitive.Item
className={cn(
"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 not-data-disabled:hover:bg-foreground/[0.09] data-disabled:pointer-events-none data-disabled:cursor-not-allowed data-selected:bg-foreground/[0.06] data-selected:text-foreground data-highlighted:bg-foreground/[0.09] data-highlighted:text-accent-foreground [&[data-highlighted][data-selected]]:bg-foreground/[0.09] [&[data-highlighted][data-selected]]: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",
className,
)}
data-slot="combobox-item"
{...props}
>
<div
className="flex min-w-0 flex-1 items-center gap-2 [&_svg:not([class*='text-'])]:text-muted-foreground"
data-slot="combobox-item-content"
>
{children}
</div>
</ComboboxPrimitive.Item>
);
}
function ComboboxEmpty({ className, ...props }: ComboboxPrimitive.Empty.Props) {
return (
<ComboboxPrimitive.Empty
className={cn(
"not-empty:p-2 text-center text-base text-muted-foreground sm:text-sm",
className,
)}
data-slot="combobox-empty"
{...props}
/>
);
}
function ComboboxList({ className, ...props }: ComboboxPrimitive.List.Props) {
return (
<ScrollArea scrollbarGutter scrollFade>
<ComboboxPrimitive.List
className={cn("not-empty:scroll-py-1 not-empty:px-1 not-empty:py-1", className)}
data-slot="combobox-list"
{...props}
/>
</ScrollArea>
);
}
/** Omits ScrollArea when an external virtualizer owns scrolling. */
function ComboboxListVirtualized({ className, ...props }: ComboboxPrimitive.List.Props) {
return (
<ComboboxPrimitive.List
className={cn("size-full min-w-0 not-empty:px-1 not-empty:py-1", className)}
data-slot="combobox-list"
{...props}
/>
);
}
function ComboboxClear({ className, ...props }: ComboboxPrimitive.Clear.Props) {
return <ComboboxPrimitive.Clear className={className} data-slot="combobox-clear" {...props} />;
}
function ComboboxStatus({ className, ...props }: ComboboxPrimitive.Status.Props) {
return (
<ComboboxPrimitive.Status
className={cn(
"px-3 py-2 font-medium text-muted-foreground text-xs empty:m-0 empty:p-0",
className,
)}
data-slot="combobox-status"
{...props}
/>
);
}
const useComboboxFilter = ComboboxPrimitive.useFilter;
export {
Combobox,
ComboboxInput,
ComboboxSearchInput,
ComboboxTrigger,
ComboboxPopup,
ComboboxItem,
ComboboxEmpty,
ComboboxList,
ComboboxListVirtualized,
ComboboxClear,
ComboboxStatus,
useComboboxFilter,
};