select.tsx 6.5 KB

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