command.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. "use client"
  2. import * as React from "react"
  3. import { type DialogProps } from "@radix-ui/react-dialog"
  4. import { Command as CommandPrimitive } from "cmdk"
  5. import { Search } from "lucide-react"
  6. import { cn } from "@/lib/utils"
  7. import { Dialog, DialogContent } from "@/components/ui/dialog"
  8. const Command = React.forwardRef<
  9. React.ElementRef<typeof CommandPrimitive>,
  10. React.ComponentPropsWithoutRef<typeof CommandPrimitive>
  11. >(({ className, ...props }, ref) => (
  12. <CommandPrimitive
  13. ref={ref}
  14. className={cn(
  15. "flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground",
  16. className
  17. )}
  18. {...props}
  19. />
  20. ))
  21. Command.displayName = CommandPrimitive.displayName
  22. const CommandDialog = ({ children, ...props }: DialogProps) => {
  23. return (
  24. <Dialog {...props}>
  25. <DialogContent className="overflow-hidden p-0 shadow-lg">
  26. <Command className="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
  27. {children}
  28. </Command>
  29. </DialogContent>
  30. </Dialog>
  31. )
  32. }
  33. const CommandInput = React.forwardRef<
  34. React.ElementRef<typeof CommandPrimitive.Input>,
  35. React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input>
  36. >(({ className, ...props }, ref) => (
  37. <div className="flex items-center border-b px-3" cmdk-input-wrapper="">
  38. <Search className="mr-2 h-4 w-4 shrink-0 opacity-50" />
  39. <CommandPrimitive.Input
  40. ref={ref}
  41. className={cn(
  42. "flex h-11 w-full rounded-md bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50",
  43. className
  44. )}
  45. {...props}
  46. />
  47. </div>
  48. ))
  49. CommandInput.displayName = CommandPrimitive.Input.displayName
  50. const CommandList = React.forwardRef<
  51. React.ElementRef<typeof CommandPrimitive.List>,
  52. React.ComponentPropsWithoutRef<typeof CommandPrimitive.List>
  53. >(({ className, ...props }, ref) => (
  54. <CommandPrimitive.List
  55. ref={ref}
  56. className={cn("max-h-[300px] overflow-y-auto overflow-x-hidden", className)}
  57. {...props}
  58. />
  59. ))
  60. CommandList.displayName = CommandPrimitive.List.displayName
  61. const CommandEmpty = React.forwardRef<
  62. React.ElementRef<typeof CommandPrimitive.Empty>,
  63. React.ComponentPropsWithoutRef<typeof CommandPrimitive.Empty>
  64. >((props, ref) => (
  65. <CommandPrimitive.Empty
  66. ref={ref}
  67. className="py-6 text-center text-sm"
  68. {...props}
  69. />
  70. ))
  71. CommandEmpty.displayName = CommandPrimitive.Empty.displayName
  72. const CommandGroup = React.forwardRef<
  73. React.ElementRef<typeof CommandPrimitive.Group>,
  74. React.ComponentPropsWithoutRef<typeof CommandPrimitive.Group>
  75. >(({ className, ...props }, ref) => (
  76. <CommandPrimitive.Group
  77. ref={ref}
  78. className={cn(
  79. "overflow-hidden p-1 text-foreground [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground",
  80. className
  81. )}
  82. {...props}
  83. />
  84. ))
  85. CommandGroup.displayName = CommandPrimitive.Group.displayName
  86. const CommandSeparator = React.forwardRef<
  87. React.ElementRef<typeof CommandPrimitive.Separator>,
  88. React.ComponentPropsWithoutRef<typeof CommandPrimitive.Separator>
  89. >(({ className, ...props }, ref) => (
  90. <CommandPrimitive.Separator
  91. ref={ref}
  92. className={cn("-mx-1 h-px bg-border", className)}
  93. {...props}
  94. />
  95. ))
  96. CommandSeparator.displayName = CommandPrimitive.Separator.displayName
  97. const CommandItem = React.forwardRef<
  98. React.ElementRef<typeof CommandPrimitive.Item>,
  99. React.ComponentPropsWithoutRef<typeof CommandPrimitive.Item>
  100. >(({ className, ...props }, ref) => (
  101. <CommandPrimitive.Item
  102. ref={ref}
  103. className={cn(
  104. "relative flex cursor-default gap-2 select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none data-[disabled=true]:pointer-events-none data-[selected='true']:bg-accent data-[selected=true]:text-accent-foreground data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
  105. className
  106. )}
  107. {...props}
  108. />
  109. ))
  110. CommandItem.displayName = CommandPrimitive.Item.displayName
  111. const CommandShortcut = ({
  112. className,
  113. ...props
  114. }: React.HTMLAttributes<HTMLSpanElement>) => {
  115. return (
  116. <span
  117. className={cn(
  118. "ml-auto text-xs tracking-widest text-muted-foreground",
  119. className
  120. )}
  121. {...props}
  122. />
  123. )
  124. }
  125. CommandShortcut.displayName = "CommandShortcut"
  126. export {
  127. Command,
  128. CommandDialog,
  129. CommandInput,
  130. CommandList,
  131. CommandEmpty,
  132. CommandGroup,
  133. CommandItem,
  134. CommandShortcut,
  135. CommandSeparator,
  136. }