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