Open History to see the last commit
1import { Combobox as ComboboxPrimitive } from "@base-ui/react/combobox";
2import { ChevronsUpDownIcon, SearchIcon, XIcon } from "lucide-react";
3import * as React from "react";
4
5import { cn } from "~/lib/utils";
6import { Input } from "~/components/ui/input";
7import { ScrollArea } from "~/components/ui/scroll-area";
8import { OpenPopupRegistration, PopupActionsProvider, usePopupActionsRef } from "./open-popups";
9
10const ComboboxContext = React.createContext<{
11 chipsRef: React.RefObject<Element | null> | null;
12 multiple: boolean;
13}>({
14 chipsRef: null,
15 multiple: false,
16});
17
18function Combobox<Value, Multiple extends boolean | undefined = false>(
19 props: ComboboxPrimitive.Root.Props<Value, Multiple>,
20) {
21 const chipsRef = React.useRef<Element | null>(null);
22 const actionsRef = usePopupActionsRef(props.actionsRef);
23 const value = { chipsRef, multiple: !!props.multiple };
24 return (
25 <ComboboxContext value={value}>
26 <PopupActionsProvider value={actionsRef}>
27 <ComboboxPrimitive.Root {...props} actionsRef={actionsRef} />
28 </PopupActionsProvider>
29 </ComboboxContext>
30 );
31}
32
33function ComboboxInput({
34 className,
35 showTrigger = true,
36 showClear = false,
37 startAddon,
38 size,
39 unstyled = false,
40 ...props
41}: Omit<ComboboxPrimitive.Input.Props, "size"> & {
42 showTrigger?: boolean;
43 showClear?: boolean;
44 startAddon?: React.ReactNode;
45 size?: "sm" | "default" | "lg" | number;
46 unstyled?: boolean;
47 ref?: React.Ref<HTMLInputElement>;
48}) {
49 const sizeValue = (size ?? "default") as "sm" | "default" | "lg" | number;
50
51 return (
52 <div className="relative not-has-[>*.w-full]:w-fit w-full text-foreground has-disabled:opacity-64">
53 {startAddon && (
54 <div
55 aria-hidden="true"
56 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"
57 data-slot="combobox-start-addon"
58 >
59 {startAddon}
60 </div>
61 )}
62 <ComboboxPrimitive.Input
63 className={cn(
64 startAddon &&
65 "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)]",
66 sizeValue === "sm"
67 ? "has-[+[data-slot=combobox-trigger],+[data-slot=combobox-clear]]:*:data-[slot=combobox-input]:pe-6.5"
68 : "has-[+[data-slot=combobox-trigger],+[data-slot=combobox-clear]]:*:data-[slot=combobox-input]:pe-7",
69 className,
70 )}
71 data-slot="combobox-input"
72 render={
73 <Input
74 className={cn(
75 "has-disabled:opacity-100",
76 unstyled && "rounded-none bg-transparent text-sm",
77 )}
78 nativeInput
79 size={sizeValue}
80 unstyled={unstyled}
81 />
82 }
83 {...props}
84 />
85 {showTrigger && (
86 <ComboboxTrigger
87 className={cn(
88 "-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",
89 sizeValue === "sm" ? "end-0" : "end-0.5",
90 )}
91 >
92 <ComboboxPrimitive.Icon data-slot="combobox-icon">
93 <ChevronsUpDownIcon />
94 </ComboboxPrimitive.Icon>
95 </ComboboxTrigger>
96 )}
97 {showClear && (
98 <ComboboxClear
99 className={cn(
100 "-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",
101 sizeValue === "sm" ? "end-0" : "end-0.5",
102 )}
103 >
104 <XIcon />
105 </ComboboxClear>
106 )}
107 </div>
108 );
109}
110
111function ComboboxSearchInput(props: React.ComponentProps<typeof ComboboxInput>) {
112 return (
113 <label className="block min-w-0 shrink-0 cursor-text px-3 pt-2.5">
114 <div className="relative -translate-y-px border-b border-border/70 pb-1.5 transition-colors focus-within:border-ring">
115 <SearchIcon
116 aria-hidden="true"
117 className="pointer-events-none absolute top-1.5 left-0 size-4 shrink-0 text-muted-foreground/55"
118 />
119 <ComboboxInput
120 {...props}
121 className="flex w-full [&_input]:h-6.5 [&_input]:ps-5 [&_input]:font-sans [&_input]:leading-6.5"
122 showTrigger={false}
123 size="sm"
124 unstyled
125 />
126 </div>
127 </label>
128 );
129}
130
131function ComboboxTrigger({ className, children, ...props }: ComboboxPrimitive.Trigger.Props) {
132 return (
133 <ComboboxPrimitive.Trigger className={className} data-slot="combobox-trigger" {...props}>
134 {children}
135 </ComboboxPrimitive.Trigger>
136 );
137}
138
139function ComboboxPopup({
140 className,
141 children,
142 side = "bottom",
143 sideOffset = 4,
144 alignOffset,
145 align = "start",
146 collisionAvoidance,
147 anchor: anchorProp,
148 ...props
149}: ComboboxPrimitive.Popup.Props & {
150 align?: ComboboxPrimitive.Positioner.Props["align"];
151 sideOffset?: ComboboxPrimitive.Positioner.Props["sideOffset"];
152 alignOffset?: ComboboxPrimitive.Positioner.Props["alignOffset"];
153 side?: ComboboxPrimitive.Positioner.Props["side"];
154 anchor?: ComboboxPrimitive.Positioner.Props["anchor"];
155 collisionAvoidance?: ComboboxPrimitive.Positioner.Props["collisionAvoidance"];
156}) {
157 const { chipsRef, multiple } = React.use(ComboboxContext);
158 const anchor = anchorProp ?? (multiple ? chipsRef : undefined);
159
160 return (
161 <ComboboxPrimitive.Portal>
162 <OpenPopupRegistration />
163 <ComboboxPrimitive.Positioner
164 align={align}
165 alignOffset={alignOffset}
166 anchor={anchor}
167 collisionAvoidance={collisionAvoidance}
168 className="z-[130] select-none"
169 data-slot="combobox-positioner"
170 side={side}
171 sideOffset={sideOffset}
172 >
173 <span
174 className={cn(
175 "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%)]",
176 className,
177 )}
178 >
179 <ComboboxPrimitive.Popup
180 // Electron drag hit-testing ignores z-index; items over a drag region would swallow clicks.
181 className="flex min-w-0 max-h-[min(var(--available-height),23rem)] flex-1 flex-col overflow-hidden text-foreground [-webkit-app-region:no-drag]"
182 data-slot="combobox-popup"
183 {...props}
184 >
185 {children}
186 </ComboboxPrimitive.Popup>
187 </span>
188 </ComboboxPrimitive.Positioner>
189 </ComboboxPrimitive.Portal>
190 );
191}
192
193function ComboboxItem({
194 className,
195 children,
196 hideIndicator: _hideIndicator = false,
197 selectedFill = true,
198 ...props
199}: ComboboxPrimitive.Item.Props & {
200 hideIndicator?: boolean;
201 /** Off for rows that mark selection themselves, so a fill only means hover or highlight. */
202 selectedFill?: boolean;
203}) {
204 return (
205 <ComboboxPrimitive.Item
206 className={cn(
207 "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: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",
208 selectedFill && "data-selected:bg-foreground/[0.06]",
209 className,
210 )}
211 data-slot="combobox-item"
212 {...props}
213 >
214 <div
215 className="flex min-w-0 flex-1 items-center gap-2 [&_svg:not([class*='text-'])]:text-muted-foreground"
216 data-slot="combobox-item-content"
217 >
218 {children}
219 </div>
220 </ComboboxPrimitive.Item>
221 );
222}
223
224function ComboboxEmpty({ className, ...props }: ComboboxPrimitive.Empty.Props) {
225 return (
226 <ComboboxPrimitive.Empty
227 className={cn(
228 "not-empty:p-2 text-center text-base text-muted-foreground sm:text-sm",
229 className,
230 )}
231 data-slot="combobox-empty"
232 {...props}
233 />
234 );
235}
236
237function ComboboxList({
238 className,
239 scrollAreaClassName,
240 ...props
241}: ComboboxPrimitive.List.Props & {
242 /** Put height caps here, not on `className`: a capped list never resizes, so the scrollbar goes stale. */
243 scrollAreaClassName?: string;
244}) {
245 return (
246 <ScrollArea className={scrollAreaClassName} observeContent scrollbarGutter scrollFade>
247 <ComboboxPrimitive.List
248 className={cn("not-empty:scroll-py-1 not-empty:px-1 not-empty:py-1", className)}
249 data-slot="combobox-list"
250 {...props}
251 />
252 </ScrollArea>
253 );
254}
255
256/** Omits ScrollArea when an external virtualizer owns scrolling. */
257function ComboboxListVirtualized({ className, ...props }: ComboboxPrimitive.List.Props) {
258 return (
259 <ComboboxPrimitive.List
260 className={cn("size-full min-w-0 not-empty:px-1 not-empty:py-1", className)}
261 data-slot="combobox-list"
262 {...props}
263 />
264 );
265}
266
267function ComboboxClear({ className, ...props }: ComboboxPrimitive.Clear.Props) {
268 return <ComboboxPrimitive.Clear className={className} data-slot="combobox-clear" {...props} />;
269}
270
271function ComboboxStatus({ className, ...props }: ComboboxPrimitive.Status.Props) {
272 return (
273 <ComboboxPrimitive.Status
274 className={cn(
275 "px-3 py-2 font-medium text-muted-foreground text-xs empty:m-0 empty:p-0",
276 className,
277 )}
278 data-slot="combobox-status"
279 {...props}
280 />
281 );
282}
283
284const useComboboxFilter = ComboboxPrimitive.useFilter;
285
286export {
287 Combobox,
288 ComboboxInput,
289 ComboboxSearchInput,
290 ComboboxTrigger,
291 ComboboxPopup,
292 ComboboxItem,
293 ComboboxEmpty,
294 ComboboxList,
295 ComboboxListVirtualized,
296 ComboboxClear,
297 ComboboxStatus,
298 useComboboxFilter,
299};