dropdown-menu.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. "use client"
  2. import * as React from "react"
  3. import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu"
  4. import { Check, ChevronRight, Circle } from "lucide-react"
  5. import { cn } from "@/lib/utils"
  6. const DropdownMenu = DropdownMenuPrimitive.Root
  7. const DropdownMenuTrigger = DropdownMenuPrimitive.Trigger
  8. const DropdownMenuGroup = DropdownMenuPrimitive.Group
  9. const DropdownMenuPortal = DropdownMenuPrimitive.Portal
  10. const DropdownMenuSub = DropdownMenuPrimitive.Sub
  11. const DropdownMenuRadioGroup = DropdownMenuPrimitive.RadioGroup
  12. const DropdownMenuSubTrigger = React.forwardRef<
  13. React.ElementRef<typeof DropdownMenuPrimitive.SubTrigger>,
  14. React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.SubTrigger> & {
  15. inset?: boolean
  16. }
  17. >(({ className, inset, children, ...props }, ref) => (
  18. <DropdownMenuPrimitive.SubTrigger
  19. ref={ref}
  20. className={cn(
  21. "flex cursor-default gap-2 select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none focus:bg-accent data-[state=open]:bg-accent [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
  22. inset && "pl-8",
  23. className
  24. )}
  25. {...props}
  26. >
  27. {children}
  28. <ChevronRight className="ml-auto" />
  29. </DropdownMenuPrimitive.SubTrigger>
  30. ))
  31. DropdownMenuSubTrigger.displayName =
  32. DropdownMenuPrimitive.SubTrigger.displayName
  33. const DropdownMenuSubContent = React.forwardRef<
  34. React.ElementRef<typeof DropdownMenuPrimitive.SubContent>,
  35. React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.SubContent>
  36. >(({ className, ...props }, ref) => (
  37. <DropdownMenuPrimitive.SubContent
  38. ref={ref}
  39. className={cn(
  40. "z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=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",
  41. className
  42. )}
  43. {...props}
  44. />
  45. ))
  46. DropdownMenuSubContent.displayName =
  47. DropdownMenuPrimitive.SubContent.displayName
  48. const DropdownMenuContent = React.forwardRef<
  49. React.ElementRef<typeof DropdownMenuPrimitive.Content>,
  50. React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Content>
  51. >(({ className, sideOffset = 4, ...props }, ref) => (
  52. <DropdownMenuPrimitive.Portal>
  53. <DropdownMenuPrimitive.Content
  54. ref={ref}
  55. sideOffset={sideOffset}
  56. className={cn(
  57. "z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=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",
  58. className
  59. )}
  60. {...props}
  61. />
  62. </DropdownMenuPrimitive.Portal>
  63. ))
  64. DropdownMenuContent.displayName = DropdownMenuPrimitive.Content.displayName
  65. const DropdownMenuItem = React.forwardRef<
  66. React.ElementRef<typeof DropdownMenuPrimitive.Item>,
  67. React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Item> & {
  68. inset?: boolean
  69. }
  70. >(({ className, inset, ...props }, ref) => (
  71. <DropdownMenuPrimitive.Item
  72. ref={ref}
  73. className={cn(
  74. "relative flex cursor-default select-none items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
  75. inset && "pl-8",
  76. className
  77. )}
  78. {...props}
  79. />
  80. ))
  81. DropdownMenuItem.displayName = DropdownMenuPrimitive.Item.displayName
  82. const DropdownMenuCheckboxItem = React.forwardRef<
  83. React.ElementRef<typeof DropdownMenuPrimitive.CheckboxItem>,
  84. React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.CheckboxItem>
  85. >(({ className, children, checked, ...props }, ref) => (
  86. <DropdownMenuPrimitive.CheckboxItem
  87. ref={ref}
  88. className={cn(
  89. "relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
  90. className
  91. )}
  92. checked={checked}
  93. {...props}
  94. >
  95. <span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
  96. <DropdownMenuPrimitive.ItemIndicator>
  97. <Check className="h-4 w-4" />
  98. </DropdownMenuPrimitive.ItemIndicator>
  99. </span>
  100. {children}
  101. </DropdownMenuPrimitive.CheckboxItem>
  102. ))
  103. DropdownMenuCheckboxItem.displayName =
  104. DropdownMenuPrimitive.CheckboxItem.displayName
  105. const DropdownMenuRadioItem = React.forwardRef<
  106. React.ElementRef<typeof DropdownMenuPrimitive.RadioItem>,
  107. React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.RadioItem>
  108. >(({ className, children, ...props }, ref) => (
  109. <DropdownMenuPrimitive.RadioItem
  110. ref={ref}
  111. className={cn(
  112. "relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
  113. className
  114. )}
  115. {...props}
  116. >
  117. <span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
  118. <DropdownMenuPrimitive.ItemIndicator>
  119. <Circle className="h-2 w-2 fill-current" />
  120. </DropdownMenuPrimitive.ItemIndicator>
  121. </span>
  122. {children}
  123. </DropdownMenuPrimitive.RadioItem>
  124. ))
  125. DropdownMenuRadioItem.displayName = DropdownMenuPrimitive.RadioItem.displayName
  126. const DropdownMenuLabel = React.forwardRef<
  127. React.ElementRef<typeof DropdownMenuPrimitive.Label>,
  128. React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Label> & {
  129. inset?: boolean
  130. }
  131. >(({ className, inset, ...props }, ref) => (
  132. <DropdownMenuPrimitive.Label
  133. ref={ref}
  134. className={cn(
  135. "px-2 py-1.5 text-sm font-semibold",
  136. inset && "pl-8",
  137. className
  138. )}
  139. {...props}
  140. />
  141. ))
  142. DropdownMenuLabel.displayName = DropdownMenuPrimitive.Label.displayName
  143. const DropdownMenuSeparator = React.forwardRef<
  144. React.ElementRef<typeof DropdownMenuPrimitive.Separator>,
  145. React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Separator>
  146. >(({ className, ...props }, ref) => (
  147. <DropdownMenuPrimitive.Separator
  148. ref={ref}
  149. className={cn("-mx-1 my-1 h-px bg-muted", className)}
  150. {...props}
  151. />
  152. ))
  153. DropdownMenuSeparator.displayName = DropdownMenuPrimitive.Separator.displayName
  154. const DropdownMenuShortcut = ({
  155. className,
  156. ...props
  157. }: React.HTMLAttributes<HTMLSpanElement>) => {
  158. return (
  159. <span
  160. className={cn("ml-auto text-xs tracking-widest opacity-60", className)}
  161. {...props}
  162. />
  163. )
  164. }
  165. DropdownMenuShortcut.displayName = "DropdownMenuShortcut"
  166. export {
  167. DropdownMenu,
  168. DropdownMenuTrigger,
  169. DropdownMenuContent,
  170. DropdownMenuItem,
  171. DropdownMenuCheckboxItem,
  172. DropdownMenuRadioItem,
  173. DropdownMenuLabel,
  174. DropdownMenuSeparator,
  175. DropdownMenuShortcut,
  176. DropdownMenuGroup,
  177. DropdownMenuPortal,
  178. DropdownMenuSub,
  179. DropdownMenuSubContent,
  180. DropdownMenuSubTrigger,
  181. DropdownMenuRadioGroup,
  182. }