xonecas.io
5414
Sa

sat / files

sat / src / components / ui / combobox.tsx

8.9 KB
1"use client";
2
3import { Combobox as ComboboxPrimitive } from "@base-ui/react";
4import { CheckIcon, ChevronDownIcon, XIcon } from "lucide-react";
5import * as React from "react";
6import { Button } from "@/components/ui/button";
7import {
8  InputGroup,
9  InputGroupAddon,
10  InputGroupButton,
11  InputGroupInput,
12} from "@/components/ui/input-group";
13import { cn } from "@/lib/utils";
14
15const Combobox = ComboboxPrimitive.Root;
16
17function ComboboxValue({ ...props }: ComboboxPrimitive.Value.Props) {
18  return <ComboboxPrimitive.Value data-slot="combobox-value" {...props} />;
19}
20
21function ComboboxTrigger({
22  className,
23  children,
24  ...props
25}: ComboboxPrimitive.Trigger.Props) {
26  return (
27    <ComboboxPrimitive.Trigger
28      data-slot="combobox-trigger"
29      className={cn("[&_svg:not([class*='size-'])]:size-4", className)}
30      {...props}
31    >
32      {children}
33      <ChevronDownIcon className="text-muted-foreground size-4 pointer-events-none" />
34    </ComboboxPrimitive.Trigger>
35  );
36}
37
38function ComboboxClear({ className, ...props }: ComboboxPrimitive.Clear.Props) {
39  return (
40    <ComboboxPrimitive.Clear
41      data-slot="combobox-clear"
42      render={<InputGroupButton variant="ghost" size="icon-xs" />}
43      className={cn(className)}
44      {...props}
45    >
46      <XIcon className="pointer-events-none" />
47    </ComboboxPrimitive.Clear>
48  );
49}
50
51function ComboboxInput({
52  className,
53  children,
54  disabled = false,
55  showTrigger = true,
56  showClear = false,
57  ...props
58}: ComboboxPrimitive.Input.Props & {
59  showTrigger?: boolean;
60  showClear?: boolean;
61}) {
62  return (
63    <InputGroup className={cn("w-auto", className)}>
64      <ComboboxPrimitive.Input
65        render={<InputGroupInput disabled={disabled} />}
66        {...props}
67      />
68      <InputGroupAddon align="inline-end">
69        {showTrigger && (
70          <InputGroupButton
71            size="icon-xs"
72            variant="ghost"
73            asChild
74            data-slot="input-group-button"
75            className="group-has-data-[slot=combobox-clear]/input-group:hidden data-pressed:bg-transparent"
76            disabled={disabled}
77          >
78            <ComboboxTrigger />
79          </InputGroupButton>
80        )}
81        {showClear && <ComboboxClear disabled={disabled} />}
82      </InputGroupAddon>
83      {children}
84    </InputGroup>
85  );
86}
87
88function ComboboxContent({
89  className,
90  side = "bottom",
91  sideOffset = 6,
92  align = "start",
93  alignOffset = 0,
94  anchor,
95  ...props
96}: ComboboxPrimitive.Popup.Props &
97  Pick<
98    ComboboxPrimitive.Positioner.Props,
99    "side" | "align" | "sideOffset" | "alignOffset" | "anchor"
100  >) {
101  return (
102    <ComboboxPrimitive.Portal>
103      <ComboboxPrimitive.Positioner
104        side={side}
105        sideOffset={sideOffset}
106        align={align}
107        alignOffset={alignOffset}
108        anchor={anchor}
109        className="isolate z-50"
110      >
111        <ComboboxPrimitive.Popup
112          data-slot="combobox-content"
113          data-chips={!!anchor}
114          className={cn(
115            "bg-popover text-popover-foreground data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 data-closed:zoom-out-95 data-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 ring-foreground/5 *:data-[slot=input-group]:bg-input/30 max-h-72 min-w-36 overflow-hidden rounded-2xl shadow-2xl ring-1 duration-100 *:data-[slot=input-group]:m-1 *:data-[slot=input-group]:mb-0 *:data-[slot=input-group]:h-9 *:data-[slot=input-group]:border-none *:data-[slot=input-group]:shadow-none group/combobox-content relative max-h-(--available-height) w-(--anchor-width) max-w-(--available-width) min-w-[calc(var(--anchor-width)+--spacing(7))] origin-(--transform-origin) data-[chips=true]:min-w-(--anchor-width)",
116            className,
117          )}
118          {...props}
119        />
120      </ComboboxPrimitive.Positioner>
121    </ComboboxPrimitive.Portal>
122  );
123}
124
125function ComboboxList({ className, ...props }: ComboboxPrimitive.List.Props) {
126  return (
127    <ComboboxPrimitive.List
128      data-slot="combobox-list"
129      className={cn(
130        "no-scrollbar max-h-[min(calc(--spacing(72)---spacing(9)),calc(var(--available-height)---spacing(9)))] scroll-py-1 overflow-y-auto p-1 data-empty:p-0 overflow-y-auto overscroll-contain",
131        className,
132      )}
133      {...props}
134    />
135  );
136}
137
138function ComboboxItem({
139  className,
140  children,
141  ...props
142}: ComboboxPrimitive.Item.Props) {
143  return (
144    <ComboboxPrimitive.Item
145      data-slot="combobox-item"
146      className={cn(
147        "data-highlighted:bg-accent data-highlighted:text-accent-foreground not-data-[variant=destructive]:data-highlighted:**:text-accent-foreground gap-2.5 rounded-xl py-2 pr-8 pl-3 text-sm [&_svg:not([class*='size-'])]:size-4 relative flex w-full cursor-default items-center outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0",
148        className,
149      )}
150      {...props}
151    >
152      {children}
153      <ComboboxPrimitive.ItemIndicator
154        render={
155          <span className="pointer-events-none absolute right-2 flex size-4 items-center justify-center" />
156        }
157      >
158        <CheckIcon className="pointer-events-none" />
159      </ComboboxPrimitive.ItemIndicator>
160    </ComboboxPrimitive.Item>
161  );
162}
163
164function ComboboxGroup({ className, ...props }: ComboboxPrimitive.Group.Props) {
165  return (
166    <ComboboxPrimitive.Group
167      data-slot="combobox-group"
168      className={cn(className)}
169      {...props}
170    />
171  );
172}
173
174function ComboboxLabel({
175  className,
176  ...props
177}: ComboboxPrimitive.GroupLabel.Props) {
178  return (
179    <ComboboxPrimitive.GroupLabel
180      data-slot="combobox-label"
181      className={cn("text-muted-foreground px-3.5 py-2.5 text-xs", className)}
182      {...props}
183    />
184  );
185}
186
187function ComboboxCollection({ ...props }: ComboboxPrimitive.Collection.Props) {
188  return (
189    <ComboboxPrimitive.Collection data-slot="combobox-collection" {...props} />
190  );
191}
192
193function ComboboxEmpty({ className, ...props }: ComboboxPrimitive.Empty.Props) {
194  return (
195    <ComboboxPrimitive.Empty
196      data-slot="combobox-empty"
197      className={cn(
198        "text-muted-foreground hidden w-full justify-center py-2 text-center text-sm group-data-empty/combobox-content:flex",
199        className,
200      )}
201      {...props}
202    />
203  );
204}
205
206function ComboboxSeparator({
207  className,
208  ...props
209}: ComboboxPrimitive.Separator.Props) {
210  return (
211    <ComboboxPrimitive.Separator
212      data-slot="combobox-separator"
213      className={cn("bg-border/50 -mx-1 my-1 h-px", className)}
214      {...props}
215    />
216  );
217}
218
219function ComboboxChips({
220  className,
221  ...props
222}: React.ComponentPropsWithRef<typeof ComboboxPrimitive.Chips> &
223  ComboboxPrimitive.Chips.Props) {
224  return (
225    <ComboboxPrimitive.Chips
226      data-slot="combobox-chips"
227      className={cn(
228        "bg-input/30 border-input focus-within:border-ring focus-within:ring-ring/50 has-aria-invalid:ring-destructive/20 dark:has-aria-invalid:ring-destructive/40 has-aria-invalid:border-destructive dark:has-aria-invalid:border-destructive/50 flex min-h-9 flex-wrap items-center gap-1.5 rounded-4xl border bg-clip-padding px-2.5 py-1.5 text-sm transition-colors focus-within:ring-[3px] has-aria-invalid:ring-[3px] has-data-[slot=combobox-chip]:px-1.5",
229        className,
230      )}
231      {...props}
232    />
233  );
234}
235
236function ComboboxChip({
237  className,
238  children,
239  showRemove = true,
240  ...props
241}: ComboboxPrimitive.Chip.Props & {
242  showRemove?: boolean;
243}) {
244  return (
245    <ComboboxPrimitive.Chip
246      data-slot="combobox-chip"
247      className={cn(
248        "bg-muted-foreground/10 text-foreground flex h-[calc(--spacing(5.5))] w-fit items-center justify-center gap-1 rounded-4xl px-2 text-xs font-medium whitespace-nowrap has-data-[slot=combobox-chip-remove]:pr-0 has-disabled:pointer-events-none has-disabled:cursor-not-allowed has-disabled:opacity-50",
249        className,
250      )}
251      {...props}
252    >
253      {children}
254      {showRemove && (
255        <ComboboxPrimitive.ChipRemove
256          render={<Button variant="ghost" size="icon-xs" />}
257          className="-ml-1 opacity-50 hover:opacity-100"
258          data-slot="combobox-chip-remove"
259        >
260          <XIcon className="pointer-events-none" />
261        </ComboboxPrimitive.ChipRemove>
262      )}
263    </ComboboxPrimitive.Chip>
264  );
265}
266
267function ComboboxChipsInput({
268  className,
269  ...props
270}: ComboboxPrimitive.Input.Props) {
271  return (
272    <ComboboxPrimitive.Input
273      data-slot="combobox-chip-input"
274      className={cn("min-w-16 flex-1 outline-none", className)}
275      {...props}
276    />
277  );
278}
279
280function useComboboxAnchor() {
281  return React.useRef<HTMLDivElement | null>(null);
282}
283
284export {
285  Combobox,
286  ComboboxInput,
287  ComboboxContent,
288  ComboboxList,
289  ComboboxItem,
290  ComboboxGroup,
291  ComboboxLabel,
292  ComboboxCollection,
293  ComboboxEmpty,
294  ComboboxSeparator,
295  ComboboxChips,
296  ComboboxChip,
297  ComboboxChipsInput,
298  ComboboxTrigger,
299  ComboboxValue,
300  useComboboxAnchor,
301};