xonecas.io
556
Bn

bees-nest / files

bees-nest / src / components / ui / select.tsx

6.9 KB
1"use client";
2
3import {
4  ArrowDown01Icon,
5  ArrowUp01Icon,
6  Tick02Icon,
7  UnfoldMoreIcon,
8} from "@hugeicons/core-free-icons";
9import { HugeiconsIcon } from "@hugeicons/react";
10import { Select as SelectPrimitive } from "radix-ui";
11import type * as React from "react";
12import { cn } from "@/lib/utils";
13
14function Select({
15  ...props
16}: React.ComponentProps<typeof SelectPrimitive.Root>) {
17  return <SelectPrimitive.Root data-slot="select" {...props} />;
18}
19
20function SelectGroup({
21  className,
22  ...props
23}: React.ComponentProps<typeof SelectPrimitive.Group>) {
24  return (
25    <SelectPrimitive.Group
26      data-slot="select-group"
27      className={cn("scroll-my-1 p-1", className)}
28      {...props}
29    />
30  );
31}
32
33function SelectValue({
34  ...props
35}: React.ComponentProps<typeof SelectPrimitive.Value>) {
36  return <SelectPrimitive.Value data-slot="select-value" {...props} />;
37}
38
39function SelectTrigger({
40  className,
41  size = "default",
42  children,
43  ...props
44}: React.ComponentProps<typeof SelectPrimitive.Trigger> & {
45  size?: "sm" | "default";
46}) {
47  return (
48    <SelectPrimitive.Trigger
49      data-slot="select-trigger"
50      data-size={size}
51      className={cn(
52        "border-input data-[placeholder]:text-muted-foreground bg-input/30 dark:hover:bg-input/50 focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 gap-1.5 rounded-4xl border px-3 py-2 text-sm transition-colors focus-visible:ring-[3px] aria-invalid:ring-[3px] data-[size=default]:h-9 data-[size=sm]:h-8 *:data-[slot=select-value]:flex *:data-[slot=select-value]:gap-1.5 [&_svg:not([class*='size-'])]:size-4 flex w-fit items-center justify-between whitespace-nowrap outline-none disabled:cursor-not-allowed disabled:opacity-50 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center [&_svg]:pointer-events-none [&_svg]:shrink-0",
53        className,
54      )}
55      {...props}
56    >
57      {children}
58      <SelectPrimitive.Icon asChild>
59        <HugeiconsIcon
60          icon={UnfoldMoreIcon}
61          strokeWidth={2}
62          className="text-muted-foreground size-4 pointer-events-none"
63        />
64      </SelectPrimitive.Icon>
65    </SelectPrimitive.Trigger>
66  );
67}
68
69function SelectContent({
70  className,
71  children,
72  position = "item-aligned",
73  align = "center",
74  ...props
75}: React.ComponentProps<typeof SelectPrimitive.Content>) {
76  return (
77    <SelectPrimitive.Portal>
78      <SelectPrimitive.Content
79        data-slot="select-content"
80        data-align-trigger={position === "item-aligned"}
81        className={cn(
82          "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 min-w-36 rounded-2xl shadow-2xl ring-1 duration-100 dark relative z-50 max-h-(--radix-select-content-available-height) origin-(--radix-select-content-transform-origin) overflow-x-hidden overflow-y-auto data-[align-trigger=true]:animate-none",
83          position === "popper" &&
84            "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
85          className,
86        )}
87        position={position}
88        align={align}
89        {...props}
90      >
91        <SelectScrollUpButton />
92        <SelectPrimitive.Viewport
93          data-position={position}
94          className={cn(
95            "data-[position=popper]:h-[var(--radix-select-trigger-height)] data-[position=popper]:w-full data-[position=popper]:min-w-[var(--radix-select-trigger-width)]",
96            position === "popper" && "",
97          )}
98        >
99          {children}
100        </SelectPrimitive.Viewport>
101        <SelectScrollDownButton />
102      </SelectPrimitive.Content>
103    </SelectPrimitive.Portal>
104  );
105}
106
107function SelectLabel({
108  className,
109  ...props
110}: React.ComponentProps<typeof SelectPrimitive.Label>) {
111  return (
112    <SelectPrimitive.Label
113      data-slot="select-label"
114      className={cn("text-muted-foreground px-3 py-2.5 text-xs", className)}
115      {...props}
116    />
117  );
118}
119
120function SelectItem({
121  className,
122  children,
123  ...props
124}: React.ComponentProps<typeof SelectPrimitive.Item>) {
125  return (
126    <SelectPrimitive.Item
127      data-slot="select-item"
128      className={cn(
129        "focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground gap-2.5 rounded-xl py-2 pr-8 pl-3 text-sm [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2 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",
130        className,
131      )}
132      {...props}
133    >
134      <span className="pointer-events-none absolute right-2 flex size-4 items-center justify-center">
135        <SelectPrimitive.ItemIndicator>
136          <HugeiconsIcon
137            icon={Tick02Icon}
138            strokeWidth={2}
139            className="pointer-events-none"
140          />
141        </SelectPrimitive.ItemIndicator>
142      </span>
143      <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
144    </SelectPrimitive.Item>
145  );
146}
147
148function SelectSeparator({
149  className,
150  ...props
151}: React.ComponentProps<typeof SelectPrimitive.Separator>) {
152  return (
153    <SelectPrimitive.Separator
154      data-slot="select-separator"
155      className={cn(
156        "bg-border/50 -mx-1 my-1 h-px pointer-events-none",
157        className,
158      )}
159      {...props}
160    />
161  );
162}
163
164function SelectScrollUpButton({
165  className,
166  ...props
167}: React.ComponentProps<typeof SelectPrimitive.ScrollUpButton>) {
168  return (
169    <SelectPrimitive.ScrollUpButton
170      data-slot="select-scroll-up-button"
171      className={cn(
172        "bg-popover z-10 flex cursor-default items-center justify-center py-1 [&_svg:not([class*='size-'])]:size-4",
173        className,
174      )}
175      {...props}
176    >
177      <HugeiconsIcon icon={ArrowUp01Icon} strokeWidth={2} />
178    </SelectPrimitive.ScrollUpButton>
179  );
180}
181
182function SelectScrollDownButton({
183  className,
184  ...props
185}: React.ComponentProps<typeof SelectPrimitive.ScrollDownButton>) {
186  return (
187    <SelectPrimitive.ScrollDownButton
188      data-slot="select-scroll-down-button"
189      className={cn(
190        "bg-popover z-10 flex cursor-default items-center justify-center py-1 [&_svg:not([class*='size-'])]:size-4",
191        className,
192      )}
193      {...props}
194    >
195      <HugeiconsIcon icon={ArrowDown01Icon} strokeWidth={2} />
196    </SelectPrimitive.ScrollDownButton>
197  );
198}
199
200export {
201  Select,
202  SelectContent,
203  SelectGroup,
204  SelectItem,
205  SelectLabel,
206  SelectScrollDownButton,
207  SelectScrollUpButton,
208  SelectSeparator,
209  SelectTrigger,
210  SelectValue,
211};