1import * as ComboboxPrimitive from "@diceui/combobox";
2import { Check, ChevronDown, X } from "lucide-react";
3import type * as React from "react";
4
5import { cn } from "@/lib/utils";
6
7const Combobox = (({
8 className,
9 ...props
10}: React.ComponentProps<typeof ComboboxPrimitive.Root>) => {
11 return (
12 <ComboboxPrimitive.Root
13 data-slot="combobox"
14 className={cn(className)}
15 {...props}
16 />
17 );
18}) as ComboboxPrimitive.ComboboxRootComponentProps;
19
20function ComboboxLabel({
21 className,
22 ...props
23}: React.ComponentProps<typeof ComboboxPrimitive.Label>) {
24 return (
25 <ComboboxPrimitive.Label
26 data-slot="combobox-label"
27 className={cn("px-0.5 py-1.5 font-semibold text-sm", className)}
28 {...props}
29 />
30 );
31}
32
33function ComboboxAnchor({
34 className,
35 ...props
36}: React.ComponentProps<typeof ComboboxPrimitive.Anchor>) {
37 return (
38 <ComboboxPrimitive.Anchor
39 data-slot="combobox-anchor"
40 className={cn(
41 "relative flex h-9 w-full items-center justify-between gap-2 rounded-md border border-input bg-transparent px-3 py-2 shadow-xs data-focused:ring-1 data-focused:ring-ring",
42 className,
43 )}
44 {...props}
45 />
46 );
47}
48
49function ComboboxInput({
50 className,
51 ...props
52}: React.ComponentProps<typeof ComboboxPrimitive.Input>) {
53 return (
54 <ComboboxPrimitive.Input
55 data-slot="combobox-input"
56 className={cn(
57 "flex h-9 w-full rounded-md bg-transparent text-base placeholder:text-muted-foreground focus:outline-hidden disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
58 className,
59 )}
60 {...props}
61 />
62 );
63}
64
65function ComboboxTrigger({
66 className,
67 children,
68 ...props
69}: React.ComponentProps<typeof ComboboxPrimitive.Trigger>) {
70 return (
71 <ComboboxPrimitive.Trigger
72 data-slot="combobox-trigger"
73 className={cn(
74 "flex shrink-0 items-center justify-center rounded-r-md border-input bg-transparent text-muted-foreground transition-colors hover:text-foreground/80 focus-visible:outline-hidden disabled:cursor-not-allowed disabled:opacity-50",
75 className,
76 )}
77 {...props}
78 >
79 {children || <ChevronDown className="size-4" />}
80 </ComboboxPrimitive.Trigger>
81 );
82}
83
84function ComboboxCancel({
85 className,
86 ...props
87}: React.ComponentProps<typeof ComboboxPrimitive.Cancel>) {
88 return (
89 <ComboboxPrimitive.Cancel
90 data-slot="combobox-cancel"
91 className={cn(
92 "absolute top-1/2 right-1 flex size-6 -translate-y-1/2 items-center justify-center rounded-sm bg-background opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-hidden focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none",
93 className,
94 )}
95 {...props}
96 />
97 );
98}
99
100function ComboboxBadgeList({
101 className,
102 ...props
103}: React.ComponentProps<typeof ComboboxPrimitive.BadgeList>) {
104 return (
105 <ComboboxPrimitive.BadgeList
106 data-slot="combobox-badge-list"
107 className={cn("flex flex-wrap items-center gap-1.5", className)}
108 {...props}
109 />
110 );
111}
112
113function ComboboxBadgeItem({
114 className,
115 children,
116 ...props
117}: React.ComponentProps<typeof ComboboxPrimitive.BadgeItem>) {
118 return (
119 <ComboboxPrimitive.BadgeItem
120 data-slot="combobox-badge-item"
121 className={cn(
122 "inline-flex items-center justify-between gap-1 rounded-sm bg-secondary px-2 py-0.5",
123 className,
124 )}
125 {...props}
126 >
127 <span className="truncate text-[13px] text-secondary-foreground">
128 {children}
129 </span>
130 <ComboboxPrimitive.BadgeItemDelete
131 data-slot="combobox-badge-item-delete"
132 className="shrink-0 rounded p-0.5 opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring data-highlighted:bg-destructive"
133 >
134 <X className="size-3" />
135 </ComboboxPrimitive.BadgeItemDelete>
136 </ComboboxPrimitive.BadgeItem>
137 );
138}
139
140function ComboboxContent({
141 sideOffset = 6,
142 className,
143 children,
144 ...props
145}: React.ComponentProps<typeof ComboboxPrimitive.Content>) {
146 return (
147 <ComboboxPrimitive.Portal>
148 <ComboboxPrimitive.Content
149 data-slot="combobox-content"
150 sideOffset={sideOffset}
151 className={cn(
152 "data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 relative z-50 max-h-fit min-w-(--dice-anchor-width) origin-(--dice-transform-origin) overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md data-[state=closed]:animate-out data-[state=open]:animate-in",
153 className,
154 )}
155 {...props}
156 >
157 {children}
158 </ComboboxPrimitive.Content>
159 </ComboboxPrimitive.Portal>
160 );
161}
162
163function ComboboxLoading({
164 className,
165 ...props
166}: React.ComponentProps<typeof ComboboxPrimitive.Loading>) {
167 return (
168 <ComboboxPrimitive.Loading
169 data-slot="combobox-loading"
170 className={cn("py-6 text-center text-sm", className)}
171 {...props}
172 >
173 Loading...
174 </ComboboxPrimitive.Loading>
175 );
176}
177
178function ComboboxEmpty({
179 className,
180 ...props
181}: React.ComponentProps<typeof ComboboxPrimitive.Empty>) {
182 return (
183 <ComboboxPrimitive.Empty
184 data-slot="combobox-empty"
185 className={cn("py-6 text-center text-sm", className)}
186 {...props}
187 />
188 );
189}
190
191function ComboboxGroup({
192 className,
193 ...props
194}: React.ComponentProps<typeof ComboboxPrimitive.Group>) {
195 return (
196 <ComboboxPrimitive.Group
197 data-slot="combobox-group"
198 className={cn("overflow-hidden", className)}
199 {...props}
200 />
201 );
202}
203
204function ComboboxGroupLabel({
205 className,
206 ...props
207}: React.ComponentProps<typeof ComboboxPrimitive.GroupLabel>) {
208 return (
209 <ComboboxPrimitive.GroupLabel
210 data-slot="combobox-group-label"
211 className={cn(
212 "px-2 py-1.5 font-semibold text-muted-foreground text-xs",
213 className,
214 )}
215 {...props}
216 />
217 );
218}
219
220function ComboboxItem({
221 className,
222 children,
223 outset,
224 ...props
225}: React.ComponentProps<typeof ComboboxPrimitive.Item> & {
226 outset?: boolean;
227}) {
228 return (
229 <ComboboxPrimitive.Item
230 data-slot="combobox-item"
231 className={cn(
232 "relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 text-sm outline-hidden data-disabled:pointer-events-none data-highlighted:bg-accent data-highlighted:text-accent-foreground data-disabled:opacity-50",
233 outset ? "pr-8 pl-2" : "pr-2 pl-8",
234 className,
235 )}
236 {...props}
237 >
238 <ComboboxPrimitive.ItemIndicator
239 className={cn(
240 "absolute flex size-3.5 items-center justify-center",
241 outset ? "right-2" : "left-2",
242 )}
243 >
244 <Check className="size-4" />
245 </ComboboxPrimitive.ItemIndicator>
246 <ComboboxPrimitive.ItemText>{children}</ComboboxPrimitive.ItemText>
247 </ComboboxPrimitive.Item>
248 );
249}
250
251function ComboboxSeparator({
252 className,
253 ...props
254}: React.ComponentProps<typeof ComboboxPrimitive.Separator>) {
255 return (
256 <ComboboxPrimitive.Separator
257 data-slot="combobox-separator"
258 className={cn("-mx-1 my-1 h-px bg-muted", className)}
259 {...props}
260 />
261 );
262}
263
264export {
265 Combobox,
266 ComboboxAnchor,
267 ComboboxBadgeItem,
268 ComboboxBadgeList,
269 ComboboxCancel,
270 ComboboxContent,
271 ComboboxEmpty,
272 ComboboxGroup,
273 ComboboxGroupLabel,
274 ComboboxInput,
275 ComboboxItem,
276 ComboboxLabel,
277 ComboboxLoading,
278 ComboboxSeparator,
279 ComboboxTrigger,
280};
281