Open History to see the last commit
1"use client";
2
3import { Combobox as ComboboxPrimitive } from "@base-ui/react/combobox";
4import { ChevronsUpDownIcon, SearchIcon, 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 ComboboxSearchInput(props: React.ComponentProps<typeof ComboboxInput>) {
134 return (
135 <div className="shrink-0 px-3 pt-2.5">
136 <div className="relative -translate-y-px border-b border-border/70 pb-1.5 transition-colors focus-within:border-ring">
137 <SearchIcon
138 aria-hidden="true"
139 className="pointer-events-none absolute top-1.5 left-0 size-4 shrink-0 text-muted-foreground/55"
140 />
141 <ComboboxInput
142 {...props}
143 className="[&_input]:h-6.5 [&_input]:ps-5 [&_input]:font-sans [&_input]:leading-6.5"
144 inputClassName="rounded-none bg-transparent text-sm"
145 showTrigger={false}
146 size="sm"
147 unstyled
148 />
149 </div>
150 </div>
151 );
152}
153
154function ComboboxTrigger({ className, children, ...props }: ComboboxPrimitive.Trigger.Props) {
155 return (
156 <ComboboxPrimitive.Trigger className={className} data-slot="combobox-trigger" {...props}>
157 {children}
158 </ComboboxPrimitive.Trigger>
159 );
160}
161
162function ComboboxPopup({
163 className,
164 children,
165 side = "bottom",
166 sideOffset = 4,
167 alignOffset,
168 align = "start",
169 anchor: anchorProp,
170 ...props
171}: ComboboxPrimitive.Popup.Props & {
172 align?: ComboboxPrimitive.Positioner.Props["align"];
173 sideOffset?: ComboboxPrimitive.Positioner.Props["sideOffset"];
174 alignOffset?: ComboboxPrimitive.Positioner.Props["alignOffset"];
175 side?: ComboboxPrimitive.Positioner.Props["side"];
176 anchor?: ComboboxPrimitive.Positioner.Props["anchor"];
177}) {
178 const { chipsRef, multiple } = React.use(ComboboxContext);
179 const anchor = anchorProp ?? (multiple ? chipsRef : undefined);
180
181 return (
182 <ComboboxPrimitive.Portal>
183 <ComboboxPrimitive.Positioner
184 align={align}
185 alignOffset={alignOffset}
186 anchor={anchor}
187 className="z-[130] select-none"
188 data-slot="combobox-positioner"
189 side={side}
190 sideOffset={sideOffset}
191 >
192 <span
193 className={cn(
194 "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%)]",
195 className,
196 )}
197 >
198 <ComboboxPrimitive.Popup
199 className="flex min-w-0 max-h-[min(var(--available-height),23rem)] flex-1 flex-col overflow-hidden text-foreground"
200 data-slot="combobox-popup"
201 {...props}
202 >
203 {children}
204 </ComboboxPrimitive.Popup>
205 </span>
206 </ComboboxPrimitive.Positioner>
207 </ComboboxPrimitive.Portal>
208 );
209}
210
211function ComboboxItem({
212 className,
213 contentClassName,
214 children,
215 hideIndicator: _hideIndicator = false,
216 ...props
217}: ComboboxPrimitive.Item.Props & {
218 contentClassName?: string;
219 hideIndicator?: boolean;
220}) {
221 return (
222 <ComboboxPrimitive.Item
223 className={cn(
224 "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",
225 className,
226 )}
227 data-slot="combobox-item"
228 {...props}
229 >
230 <div
231 className={cn(
232 "min-w-0 flex-1 [&_svg:not([class*='text-'])]:text-muted-foreground",
233 contentClassName,
234 )}
235 data-slot="combobox-item-content"
236 >
237 {children}
238 </div>
239 </ComboboxPrimitive.Item>
240 );
241}
242
243function ComboboxSeparator({ className, ...props }: ComboboxPrimitive.Separator.Props) {
244 return (
245 <ComboboxPrimitive.Separator
246 className={cn("mx-2 my-1 h-px bg-border last:hidden", className)}
247 data-slot="combobox-separator"
248 {...props}
249 />
250 );
251}
252
253function ComboboxGroup({ className, ...props }: ComboboxPrimitive.Group.Props) {
254 return (
255 <ComboboxPrimitive.Group
256 className={cn("[[role=group]+&]:mt-1.5", className)}
257 data-slot="combobox-group"
258 {...props}
259 />
260 );
261}
262
263function ComboboxGroupLabel({ className, ...props }: ComboboxPrimitive.GroupLabel.Props) {
264 return (
265 <ComboboxPrimitive.GroupLabel
266 className={cn("px-2 py-1.5 font-medium text-muted-foreground text-xs", className)}
267 data-slot="combobox-group-label"
268 {...props}
269 />
270 );
271}
272
273function ComboboxEmpty({ className, ...props }: ComboboxPrimitive.Empty.Props) {
274 return (
275 <ComboboxPrimitive.Empty
276 className={cn(
277 "not-empty:p-2 text-center text-base text-muted-foreground sm:text-sm",
278 className,
279 )}
280 data-slot="combobox-empty"
281 {...props}
282 />
283 );
284}
285
286function ComboboxRow({ className, ...props }: ComboboxPrimitive.Row.Props) {
287 return <ComboboxPrimitive.Row className={className} data-slot="combobox-row" {...props} />;
288}
289
290function ComboboxValue({ ...props }: ComboboxPrimitive.Value.Props) {
291 return <ComboboxPrimitive.Value data-slot="combobox-value" {...props} />;
292}
293
294function ComboboxList({ className, ...props }: ComboboxPrimitive.List.Props) {
295 return (
296 <ScrollArea scrollbarGutter scrollFade>
297 <ComboboxPrimitive.List
298 className={cn("not-empty:scroll-py-1 not-empty:px-1 not-empty:py-1", className)}
299 data-slot="combobox-list"
300 {...props}
301 />
302 </ScrollArea>
303 );
304}
305
306/**
307 * A variant of `ComboboxList` without `ScrollArea`, for use when
308 * an external virtualizer (e.g. LegendList) owns the scroll container.
309 */
310function ComboboxListVirtualized({ className, ...props }: ComboboxPrimitive.List.Props) {
311 return (
312 <ComboboxPrimitive.List
313 className={cn("not-empty:px-1 not-empty:py-1", className)}
314 data-slot="combobox-list"
315 {...props}
316 />
317 );
318}
319
320function ComboboxClear({ className, ...props }: ComboboxPrimitive.Clear.Props) {
321 return <ComboboxPrimitive.Clear className={className} data-slot="combobox-clear" {...props} />;
322}
323
324function ComboboxStatus({ className, ...props }: ComboboxPrimitive.Status.Props) {
325 return (
326 <ComboboxPrimitive.Status
327 className={cn(
328 "px-3 py-2 font-medium text-muted-foreground text-xs empty:m-0 empty:p-0",
329 className,
330 )}
331 data-slot="combobox-status"
332 {...props}
333 />
334 );
335}
336
337function ComboboxCollection(props: ComboboxPrimitive.Collection.Props) {
338 return <ComboboxPrimitive.Collection data-slot="combobox-collection" {...props} />;
339}
340
341function ComboboxChips({
342 className,
343 children,
344 startAddon,
345 ...props
346}: ComboboxPrimitive.Chips.Props & {
347 startAddon?: React.ReactNode;
348}) {
349 const { chipsRef } = React.use(ComboboxContext);
350
351 return (
352 <ComboboxPrimitive.Chips
353 className={cn(
354 "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%)]",
355 className,
356 )}
357 data-slot="combobox-chips"
358 ref={chipsRef as React.Ref<HTMLDivElement> | null}
359 {...props}
360 >
361 {startAddon && (
362 <div
363 aria-hidden="true"
364 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"
365 data-slot="combobox-start-addon"
366 >
367 {startAddon}
368 </div>
369 )}
370 {children}
371 </ComboboxPrimitive.Chips>
372 );
373}
374
375function ComboboxChip({ children, ...props }: ComboboxPrimitive.Chip.Props) {
376 const labelId = React.useId();
377
378 return (
379 <ComboboxPrimitive.Chip
380 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"
381 data-slot="combobox-chip"
382 {...props}
383 >
384 <span id={labelId}>{children}</span>
385 <ComboboxChipRemove labelId={labelId} />
386 </ComboboxPrimitive.Chip>
387 );
388}
389
390function ComboboxChipRemove({
391 labelId,
392 ...props
393}: ComboboxPrimitive.ChipRemove.Props & { labelId: string }) {
394 const removeLabelId = `${labelId}-remove`;
395
396 return (
397 <ComboboxPrimitive.ChipRemove
398 aria-labelledby={`${removeLabelId} ${labelId}`}
399 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"
400 data-slot="combobox-chip-remove"
401 {...props}
402 >
403 <span id={removeLabelId} className="sr-only">
404 Remove
405 </span>
406 <XIcon aria-hidden="true" />
407 </ComboboxPrimitive.ChipRemove>
408 );
409}
410
411const useComboboxFilter = ComboboxPrimitive.useFilter;
412
413export {
414 Combobox,
415 ComboboxChipsInput,
416 ComboboxInput,
417 ComboboxSearchInput,
418 ComboboxTrigger,
419 ComboboxPopup,
420 ComboboxItem,
421 ComboboxSeparator,
422 ComboboxGroup,
423 ComboboxGroupLabel,
424 ComboboxEmpty,
425 ComboboxValue,
426 ComboboxList,
427 ComboboxListVirtualized,
428 ComboboxClear,
429 ComboboxStatus,
430 ComboboxRow,
431 ComboboxCollection,
432 ComboboxChips,
433 ComboboxChip,
434 useComboboxFilter,
435};