Open History to see the last commit
1"use client";
2
3import { Combobox as ComboboxPrimitive } from "@base-ui/react/combobox";
4import { ChevronsUpDownIcon, XIcon } from "lucide-react";
5import * as React from "react";
6
7import { cn } from "~/lib/utils";
8import { Input } from "~/components/ui/input";
9import { ScrollArea } from "~/components/ui/scroll-area";
10
11const ComboboxContext = React.createContext<{
12 chipsRef: React.RefObject<Element | null> | null;
13 multiple: boolean;
14}>({
15 chipsRef: null,
16 multiple: false,
17});
18
19function Combobox<Value, Multiple extends boolean | undefined = false>(
20 props: ComboboxPrimitive.Root.Props<Value, Multiple>,
21) {
22 const chipsRef = React.useRef<Element | null>(null);
23 const value = React.useMemo(() => ({ chipsRef, multiple: !!props.multiple }), [props.multiple]);
24 return (
25 <ComboboxContext value={value}>
26 <ComboboxPrimitive.Root {...props} />
27 </ComboboxContext>
28 );
29}
30
31function ComboboxChipsInput({
32 className,
33 size,
34 ...props
35}: Omit<ComboboxPrimitive.Input.Props, "size"> & {
36 size?: "sm" | "default" | "lg" | number;
37 ref?: React.Ref<HTMLInputElement>;
38}) {
39 const sizeValue = (size ?? "default") as "sm" | "default" | "lg" | number;
40
41 return (
42 <ComboboxPrimitive.Input
43 className={cn(
44 "min-w-12 flex-1 text-base outline-none sm:text-sm [[data-slot=combobox-chip]+&]:ps-0.5",
45 sizeValue === "sm" ? "ps-1.5" : "ps-2",
46 className,
47 )}
48 data-size={typeof sizeValue === "string" ? sizeValue : undefined}
49 data-slot="combobox-chips-input"
50 size={typeof sizeValue === "number" ? sizeValue : undefined}
51 {...props}
52 />
53 );
54}
55
56function ComboboxInput({
57 className,
58 inputClassName,
59 showTrigger = true,
60 showClear = false,
61 startAddon,
62 size,
63 unstyled = false,
64 ...props
65}: Omit<ComboboxPrimitive.Input.Props, "size"> & {
66 inputClassName?: string;
67 showTrigger?: boolean;
68 showClear?: boolean;
69 startAddon?: React.ReactNode;
70 size?: "sm" | "default" | "lg" | number;
71 unstyled?: boolean;
72 ref?: React.Ref<HTMLInputElement>;
73}) {
74 const sizeValue = (size ?? "default") as "sm" | "default" | "lg" | number;
75
76 return (
77 <div className="relative not-has-[>*.w-full]:w-fit w-full text-foreground has-disabled:opacity-64">
78 {startAddon && (
79 <div
80 aria-hidden="true"
81 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"
82 data-slot="combobox-start-addon"
83 >
84 {startAddon}
85 </div>
86 )}
87 <ComboboxPrimitive.Input
88 className={cn(
89 startAddon &&
90 "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)]",
91 sizeValue === "sm"
92 ? "has-[+[data-slot=combobox-trigger],+[data-slot=combobox-clear]]:*:data-[slot=combobox-input]:pe-6.5"
93 : "has-[+[data-slot=combobox-trigger],+[data-slot=combobox-clear]]:*:data-[slot=combobox-input]:pe-7",
94 className,
95 )}
96 data-slot="combobox-input"
97 render={
98 <Input
99 className={cn("has-disabled:opacity-100", inputClassName)}
100 nativeInput
101 size={sizeValue}
102 unstyled={unstyled}
103 />
104 }
105 {...props}
106 />
107 {showTrigger && (
108 <ComboboxTrigger
109 className={cn(
110 "-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",
111 sizeValue === "sm" ? "end-0" : "end-0.5",
112 )}
113 >
114 <ComboboxPrimitive.Icon data-slot="combobox-icon">
115 <ChevronsUpDownIcon />
116 </ComboboxPrimitive.Icon>
117 </ComboboxTrigger>
118 )}
119 {showClear && (
120 <ComboboxClear
121 className={cn(
122 "-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",
123 sizeValue === "sm" ? "end-0" : "end-0.5",
124 )}
125 >
126 <XIcon />
127 </ComboboxClear>
128 )}
129 </div>
130 );
131}
132
133function ComboboxTrigger({ className, children, ...props }: ComboboxPrimitive.Trigger.Props) {
134 return (
135 <ComboboxPrimitive.Trigger className={className} data-slot="combobox-trigger" {...props}>
136 {children}
137 </ComboboxPrimitive.Trigger>
138 );
139}
140
141function ComboboxPopup({
142 className,
143 children,
144 side = "bottom",
145 sideOffset = 4,
146 alignOffset,
147 align = "start",
148 anchor: anchorProp,
149 ...props
150}: ComboboxPrimitive.Popup.Props & {
151 align?: ComboboxPrimitive.Positioner.Props["align"];
152 sideOffset?: ComboboxPrimitive.Positioner.Props["sideOffset"];
153 alignOffset?: ComboboxPrimitive.Positioner.Props["alignOffset"];
154 side?: ComboboxPrimitive.Positioner.Props["side"];
155 anchor?: ComboboxPrimitive.Positioner.Props["anchor"];
156}) {
157 const { chipsRef } = React.use(ComboboxContext);
158 const anchor = anchorProp ?? chipsRef;
159
160 return (
161 <ComboboxPrimitive.Portal>
162 <ComboboxPrimitive.Positioner
163 align={align}
164 alignOffset={alignOffset}
165 anchor={anchor}
166 className="z-[130] select-none"
167 data-slot="combobox-positioner"
168 side={side}
169 sideOffset={sideOffset}
170 >
171 <span
172 className={cn(
173 "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%)]",
174 className,
175 )}
176 >
177 <ComboboxPrimitive.Popup
178 className="flex min-w-0 max-h-[min(var(--available-height),23rem)] flex-1 flex-col overflow-hidden text-foreground"
179 data-slot="combobox-popup"
180 {...props}
181 >
182 {children}
183 </ComboboxPrimitive.Popup>
184 </span>
185 </ComboboxPrimitive.Positioner>
186 </ComboboxPrimitive.Portal>
187 );
188}
189
190function ComboboxItem({
191 className,
192 contentClassName,
193 children,
194 hideIndicator: _hideIndicator = false,
195 ...props
196}: ComboboxPrimitive.Item.Props & {
197 contentClassName?: string;
198 hideIndicator?: boolean;
199}) {
200 return (
201 <ComboboxPrimitive.Item
202 className={cn(
203 "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 hover:bg-accent data-disabled:pointer-events-none data-disabled:cursor-not-allowed data-selected:bg-foreground/[0.08] data-selected:text-foreground data-highlighted:bg-accent data-highlighted:text-accent-foreground [&[data-highlighted][data-selected]]:bg-accent [&[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",
204 className,
205 )}
206 data-slot="combobox-item"
207 {...props}
208 >
209 <div
210 className={cn(
211 "min-w-0 flex-1 [&_svg:not([class*='text-'])]:text-muted-foreground",
212 contentClassName,
213 )}
214 data-slot="combobox-item-content"
215 >
216 {children}
217 </div>
218 </ComboboxPrimitive.Item>
219 );
220}
221
222function ComboboxSeparator({ className, ...props }: ComboboxPrimitive.Separator.Props) {
223 return (
224 <ComboboxPrimitive.Separator
225 className={cn("mx-2 my-1 h-px bg-border last:hidden", className)}
226 data-slot="combobox-separator"
227 {...props}
228 />
229 );
230}
231
232function ComboboxGroup({ className, ...props }: ComboboxPrimitive.Group.Props) {
233 return (
234 <ComboboxPrimitive.Group
235 className={cn("[[role=group]+&]:mt-1.5", className)}
236 data-slot="combobox-group"
237 {...props}
238 />
239 );
240}
241
242function ComboboxGroupLabel({ className, ...props }: ComboboxPrimitive.GroupLabel.Props) {
243 return (
244 <ComboboxPrimitive.GroupLabel
245 className={cn("px-2 py-1.5 font-medium text-muted-foreground text-xs", className)}
246 data-slot="combobox-group-label"
247 {...props}
248 />
249 );
250}
251
252function ComboboxEmpty({ className, ...props }: ComboboxPrimitive.Empty.Props) {
253 return (
254 <ComboboxPrimitive.Empty
255 className={cn(
256 "not-empty:p-2 text-center text-base text-muted-foreground sm:text-sm",
257 className,
258 )}
259 data-slot="combobox-empty"
260 {...props}
261 />
262 );
263}
264
265function ComboboxRow({ className, ...props }: ComboboxPrimitive.Row.Props) {
266 return <ComboboxPrimitive.Row className={className} data-slot="combobox-row" {...props} />;
267}
268
269function ComboboxValue({ ...props }: ComboboxPrimitive.Value.Props) {
270 return <ComboboxPrimitive.Value data-slot="combobox-value" {...props} />;
271}
272
273function ComboboxList({ className, ...props }: ComboboxPrimitive.List.Props) {
274 return (
275 <ScrollArea scrollbarGutter scrollFade>
276 <ComboboxPrimitive.List
277 className={cn("not-empty:scroll-py-1 not-empty:px-1 not-empty:py-1", className)}
278 data-slot="combobox-list"
279 {...props}
280 />
281 </ScrollArea>
282 );
283}
284
285/**
286 * A variant of `ComboboxList` without `ScrollArea`, for use when
287 * an external virtualizer (e.g. LegendList) owns the scroll container.
288 */
289function ComboboxListVirtualized({ className, ...props }: ComboboxPrimitive.List.Props) {
290 return (
291 <ComboboxPrimitive.List
292 className={cn("not-empty:px-1 not-empty:py-1", className)}
293 data-slot="combobox-list"
294 {...props}
295 />
296 );
297}
298
299function ComboboxClear({ className, ...props }: ComboboxPrimitive.Clear.Props) {
300 return <ComboboxPrimitive.Clear className={className} data-slot="combobox-clear" {...props} />;
301}
302
303function ComboboxStatus({ className, ...props }: ComboboxPrimitive.Status.Props) {
304 return (
305 <ComboboxPrimitive.Status
306 className={cn(
307 "px-3 py-2 font-medium text-muted-foreground text-xs empty:m-0 empty:p-0",
308 className,
309 )}
310 data-slot="combobox-status"
311 {...props}
312 />
313 );
314}
315
316function ComboboxCollection(props: ComboboxPrimitive.Collection.Props) {
317 return <ComboboxPrimitive.Collection data-slot="combobox-collection" {...props} />;
318}
319
320function ComboboxChips({
321 className,
322 children,
323 startAddon,
324 ...props
325}: ComboboxPrimitive.Chips.Props & {
326 startAddon?: React.ReactNode;
327}) {
328 const { chipsRef } = React.use(ComboboxContext);
329
330 return (
331 <ComboboxPrimitive.Chips
332 className={cn(
333 "relative inline-flex min-h-9 w-full flex-wrap gap-1 rounded-lg border border-input bg-background not-dark:bg-clip-padding p-[calc(--spacing(1)-1px)] text-base shadow-xs/5 outline-none ring-ring/24 transition-shadow *:min-h-7 before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] not-has-disabled:not-focus-within:not-aria-invalid:before:shadow-[0_1px_--theme(--color-black/4%)] focus-within:border-ring focus-within:ring-[3px] has-disabled:pointer-events-none has-data-[size=lg]:min-h-10 has-data-[size=sm]:min-h-8 has-aria-invalid:border-destructive/36 has-autofill:bg-foreground/4 has-disabled:opacity-64 has-[:disabled,:focus-within,[aria-invalid]]:shadow-none focus-within:has-aria-invalid:border-destructive/64 focus-within:has-aria-invalid:ring-destructive/16 has-data-[size=lg]:*:min-h-8 has-data-[size=sm]:*:min-h-6 sm:min-h-8 sm:text-sm sm:has-data-[size=lg]:min-h-9 sm:has-data-[size=sm]:min-h-7 sm:*:min-h-6 sm:has-data-[size=lg]:*:min-h-7 sm:has-data-[size=sm]:*:min-h-5 dark:not-has-disabled:bg-input/32 dark:has-autofill:bg-foreground/8 dark:has-aria-invalid:ring-destructive/24 dark:not-has-disabled:not-focus-within:not-aria-invalid:before:shadow-[0_-1px_--theme(--color-white/6%)]",
334 className,
335 )}
336 data-slot="combobox-chips"
337 ref={chipsRef as React.Ref<HTMLDivElement> | null}
338 {...props}
339 >
340 {startAddon && (
341 <div
342 aria-hidden="true"
343 className="[&_svg]:-ms-0.5 [&_svg]:-me-1.5 flex shrink-0 items-center ps-2 opacity-80 has-[~[data-size=sm]]:has-[+[data-slot=combobox-chip]]:pe-1.5 has-[~[data-size=sm]]:ps-1.5 has-[+[data-slot=combobox-chip]]:pe-2 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none"
344 data-slot="combobox-start-addon"
345 >
346 {startAddon}
347 </div>
348 )}
349 {children}
350 </ComboboxPrimitive.Chips>
351 );
352}
353
354function ComboboxChip({ children, ...props }: ComboboxPrimitive.Chip.Props) {
355 const labelId = React.useId();
356
357 return (
358 <ComboboxPrimitive.Chip
359 className="flex items-center rounded-[calc(var(--radius-md)-1px)] bg-accent ps-2 font-medium text-accent-foreground text-sm outline-none sm:text-xs/(--text-xs--line-height) [&_svg:not([class*='size-'])]:size-4 sm:[&_svg:not([class*='size-'])]:size-3.5"
360 data-slot="combobox-chip"
361 {...props}
362 >
363 <span id={labelId}>{children}</span>
364 <ComboboxChipRemove labelId={labelId} />
365 </ComboboxPrimitive.Chip>
366 );
367}
368
369function ComboboxChipRemove({
370 labelId,
371 ...props
372}: ComboboxPrimitive.ChipRemove.Props & { labelId: string }) {
373 const removeLabelId = `${labelId}-remove`;
374
375 return (
376 <ComboboxPrimitive.ChipRemove
377 aria-labelledby={`${removeLabelId} ${labelId}`}
378 className="h-full shrink-0 cursor-pointer px-1.5 opacity-80 hover:opacity-100 [&_svg:not([class*='size-'])]:size-4 sm:[&_svg:not([class*='size-'])]:size-3.5"
379 data-slot="combobox-chip-remove"
380 {...props}
381 >
382 <span id={removeLabelId} className="sr-only">
383 Remove
384 </span>
385 <XIcon aria-hidden="true" />
386 </ComboboxPrimitive.ChipRemove>
387 );
388}
389
390const useComboboxFilter = ComboboxPrimitive.useFilter;
391
392export {
393 Combobox,
394 ComboboxChipsInput,
395 ComboboxInput,
396 ComboboxTrigger,
397 ComboboxPopup,
398 ComboboxItem,
399 ComboboxSeparator,
400 ComboboxGroup,
401 ComboboxGroupLabel,
402 ComboboxEmpty,
403 ComboboxValue,
404 ComboboxList,
405 ComboboxListVirtualized,
406 ComboboxClear,
407 ComboboxStatus,
408 ComboboxRow,
409 ComboboxCollection,
410 ComboboxChips,
411 ComboboxChip,
412 useComboboxFilter,
413};