combobox.tsx
This file
Path
apps/web/src/components/ui/combobox.tsx
298 lines · 8.8 KiB · TypeScript
Last changed by
Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
History of this fileTake it
Give the web its W9 dependencies, components, project nav, line and revision helpers, and a stub for every new pageoscabrielbefore adoption254268a
lines 1–111why, one step back1import { Combobox as ComboboxPrimitive } from "@base-ui/react";2import { cn } from "cn";3import { ChevronDownIcon, XIcon, CheckIcon } from "lucide-react";4import * as React from "react";56import { Button } from "@/components/ui/button";7import {8 InputGroup,9 InputGroupAddon,10 InputGroupButton,11 InputGroupInput,12} from "@/components/ui/input-group";1314const Combobox = ComboboxPrimitive.Root;1516function ComboboxValue({ ...props }: ComboboxPrimitive.Value.Props) {17 return <ComboboxPrimitive.Value data-slot="combobox-value" {...props} />;18}1920function ComboboxTrigger({21 className,22 children,23 ...props24}: ComboboxPrimitive.Trigger.Props) {25 return (26 <ComboboxPrimitive.Trigger27 data-slot="combobox-trigger"28 className={cn("[&_svg:not([class*='size-'])]:size-4", className)}29 {...props}30 >31 {children}32 <ChevronDownIcon className="text-muted-foreground pointer-events-none size-4" />33 </ComboboxPrimitive.Trigger>34 );35}3637function ComboboxClear({ className, ...props }: ComboboxPrimitive.Clear.Props) {38 return (39 <ComboboxPrimitive.Clear40 data-slot="combobox-clear"41 render={<InputGroupButton variant="ghost" size="icon-xs" />}42 className={cn(className)}43 {...props}44 >45 <XIcon className="pointer-events-none" />46 </ComboboxPrimitive.Clear>47 );48}4950function ComboboxInput({51 className,52 children,53 disabled = false,54 showTrigger = true,55 showClear = false,56 ...props57}: ComboboxPrimitive.Input.Props & {58 showTrigger?: boolean;59 showClear?: boolean;60}) {61 return (62 <InputGroup className={cn("w-auto", className)}>63 <ComboboxPrimitive.Input64 render={<InputGroupInput disabled={disabled} />}65 {...props}66 />67 <InputGroupAddon align="inline-end">68 {showTrigger && (69 <InputGroupButton70 size="icon-xs"71 variant="ghost"72 render={<ComboboxTrigger />}73 data-slot="input-group-button"74 className="group-has-data-[slot=combobox-clear]/input-group:hidden data-pressed:bg-transparent"75 disabled={disabled}76 />77 )}78 {showClear && <ComboboxClear disabled={disabled} />}79 </InputGroupAddon>80 {children}81 </InputGroup>82 );83}8485function ComboboxContent({86 className,87 side = "bottom",88 sideOffset = 6,89 align = "start",90 alignOffset = 0,91 anchor,92 ...props93}: ComboboxPrimitive.Popup.Props &94 Pick<95 ComboboxPrimitive.Positioner.Props,96 "side" | "align" | "sideOffset" | "alignOffset" | "anchor"97 >) {98 return (99 <ComboboxPrimitive.Portal>100 <ComboboxPrimitive.Positioner101 side={side}102 sideOffset={sideOffset}103 align={align}104 alignOffset={alignOffset}105 anchor={anchor}106 className="isolate z-50"107 >108 <ComboboxPrimitive.Popup109 data-slot="combobox-content"110 data-chips={!!anchor}111 className={cn(Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 112why, one step back112 "group/combobox-content bg-popover text-popover-foreground ring-input data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-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 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95 relative max-h-(--available-height) w-(--anchor-width) max-w-(--available-width) min-w-[calc(var(--anchor-width)+--spacing(7))] origin-(--transform-origin) overflow-hidden rounded-none ring-1 duration-100 data-[chips=true]:min-w-(--anchor-width) *:data-[slot=input-group]:m-1 *:data-[slot=input-group]:mb-0 *:data-[slot=input-group]:h-9 *:data-[slot=input-group]:bg-transparent *:data-[slot=input-group]:shadow-none sm:*:data-[slot=input-group]:h-8",Give the web its W9 dependencies, components, project nav, line and revision helpers, and a stub for every new pageoscabrielbefore adoption254268a
lines 113–143why, one step back113 className114 )}115 {...props}116 />117 </ComboboxPrimitive.Positioner>118 </ComboboxPrimitive.Portal>119 );120}121122function ComboboxList({ className, ...props }: ComboboxPrimitive.List.Props) {123 return (124 <ComboboxPrimitive.List125 data-slot="combobox-list"126 className={cn(127 "no-scrollbar max-h-[min(calc(--spacing(72)---spacing(9)),calc(var(--available-height)---spacing(9)))] scroll-py-1 overflow-y-auto overscroll-contain data-empty:p-0",128 className129 )}130 {...props}131 />132 );133}134135function ComboboxItem({136 className,137 children,138 ...props139}: ComboboxPrimitive.Item.Props) {140 return (141 <ComboboxPrimitive.Item142 data-slot="combobox-item"143 className={cn(Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 144why, one step back144 "data-highlighted:bg-accent data-highlighted:text-accent-foreground not-data-[variant=destructive]:data-highlighted:**:text-accent-foreground text-ui relative flex w-full cursor-default items-center gap-2 rounded-none py-2 pr-8 pl-2 outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",Give the web its W9 dependencies, components, project nav, line and revision helpers, and a stub for every new pageoscabrielbefore adoption254268a
lines 145–177why, one step back145 className146 )}147 {...props}148 >149 {children}150 <ComboboxPrimitive.ItemIndicator151 render={152 <span className="pointer-events-none absolute right-2 flex size-4 items-center justify-center" />153 }154 >155 <CheckIcon className="pointer-events-none" />156 </ComboboxPrimitive.ItemIndicator>157 </ComboboxPrimitive.Item>158 );159}160161function ComboboxGroup({ className, ...props }: ComboboxPrimitive.Group.Props) {162 return (163 <ComboboxPrimitive.Group164 data-slot="combobox-group"165 className={cn(className)}166 {...props}167 />168 );169}170171function ComboboxLabel({172 className,173 ...props174}: ComboboxPrimitive.GroupLabel.Props) {175 return (176 <ComboboxPrimitive.GroupLabel177 data-slot="combobox-label"Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 178why, one step back178 className={cn("text-muted-foreground text-label px-2 py-2", className)}Give the web its W9 dependencies, components, project nav, line and revision helpers, and a stub for every new pageoscabrielbefore adoption254268a
lines 179–194why, one step back179 {...props}180 />181 );182}183184function ComboboxCollection({ ...props }: ComboboxPrimitive.Collection.Props) {185 return (186 <ComboboxPrimitive.Collection data-slot="combobox-collection" {...props} />187 );188}189190function ComboboxEmpty({ className, ...props }: ComboboxPrimitive.Empty.Props) {191 return (192 <ComboboxPrimitive.Empty193 data-slot="combobox-empty"194 className={cn(Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 195why, one step back195 "text-muted-foreground text-ui hidden w-full justify-center py-2 text-center group-data-empty/combobox-content:flex",Give the web its W9 dependencies, components, project nav, line and revision helpers, and a stub for every new pageoscabrielbefore adoption254268a
lines 196–224why, one step back196 className197 )}198 {...props}199 />200 );201}202203function ComboboxSeparator({204 className,205 ...props206}: ComboboxPrimitive.Separator.Props) {207 return (208 <ComboboxPrimitive.Separator209 data-slot="combobox-separator"210 className={cn("bg-border -mx-1 h-px", className)}211 {...props}212 />213 );214}215216function ComboboxChips({217 className,218 ...props219}: React.ComponentPropsWithRef<typeof ComboboxPrimitive.Chips> &220 ComboboxPrimitive.Chips.Props) {221 return (222 <ComboboxPrimitive.Chips223 data-slot="combobox-chips"224 className={cn(Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 225why, one step back225 "border-input focus-within:border-ring focus-within:ring-ring/50 has-aria-invalid:border-destructive has-aria-invalid:ring-destructive/20 dark:has-aria-invalid:border-destructive/50 dark:has-aria-invalid:ring-destructive/40 sm:text-ui flex min-h-9 flex-wrap items-center gap-1 rounded-none border bg-transparent bg-clip-padding px-2.5 py-1 text-base transition-colors focus-within:ring-1 has-aria-invalid:ring-1 has-data-[slot=combobox-chip]:px-1 sm:min-h-8",Give the web its W9 dependencies, components, project nav, line and revision helpers, and a stub for every new pageoscabrielbefore adoption254268a
lines 226–244why, one step back226 className227 )}228 {...props}229 />230 );231}232233function ComboboxChip({234 className,235 children,236 showRemove = true,237 ...props238}: ComboboxPrimitive.Chip.Props & {239 showRemove?: boolean;240}) {241 return (242 <ComboboxPrimitive.Chip243 data-slot="combobox-chip"244 className={cn(Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 245why, one step back245 "bg-muted text-foreground text-label flex h-[calc(--spacing(5.25))] w-fit items-center justify-center gap-1 rounded-none px-1.5 font-medium whitespace-nowrap has-disabled:pointer-events-none has-disabled:cursor-not-allowed has-disabled:opacity-50 has-data-[slot=combobox-chip-remove]:pr-0",Give the web its W9 dependencies, components, project nav, line and revision helpers, and a stub for every new pageoscabrielbefore adoption254268a
lines 246–298why, one step back246 className247 )}248 {...props}249 >250 {children}251 {showRemove && (252 <ComboboxPrimitive.ChipRemove253 render={<Button variant="ghost" size="icon-xs" />}254 className="-ml-1 opacity-50 hover:opacity-100"255 data-slot="combobox-chip-remove"256 >257 <XIcon className="pointer-events-none" />258 </ComboboxPrimitive.ChipRemove>259 )}260 </ComboboxPrimitive.Chip>261 );262}263264function ComboboxChipsInput({265 className,266 ...props267}: ComboboxPrimitive.Input.Props) {268 return (269 <ComboboxPrimitive.Input270 data-slot="combobox-chip-input"271 className={cn("min-w-16 flex-1 outline-none", className)}272 {...props}273 />274 );275}276277function useComboboxAnchor() {278 return React.useRef<HTMLDivElement | null>(null);279}280281export {282 Combobox,283 ComboboxInput,284 ComboboxContent,285 ComboboxList,286 ComboboxItem,287 ComboboxGroup,288 ComboboxLabel,289 ComboboxCollection,290 ComboboxEmpty,291 ComboboxSeparator,292 ComboboxChips,293 ComboboxChip,294 ComboboxChipsInput,295 ComboboxTrigger,296 ComboboxValue,297 useComboboxAnchor,298};