command.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use client"
  2. import * as React from "react"
  3. import {Command as CommandPrimitive} from "cmdk"
  4. import {SearchIcon} from "lucide-react"
  5. import {cn} from "@/lib/utils"
  6. import {Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle,} from "@/components/ui/dialog"
  7. function Command({
  8. className,
  9. ...props
  10. }: React.ComponentProps<typeof CommandPrimitive>) {
  11. return (
  12. <CommandPrimitive
  13. data-slot="command"
  14. className={cn(
  15. "bg-popover text-popover-foreground flex h-full w-full flex-col overflow-hidden rounded-md",
  16. className
  17. )}
  18. {...props}
  19. />
  20. )
  21. }
  22. function CommandDialog({
  23. title = "Command Palette",
  24. description = "Search for a command to run...",
  25. children,
  26. className,
  27. showCloseButton = true,
  28. ...props
  29. }: React.ComponentProps<typeof Dialog> & {
  30. title?: string
  31. description?: string
  32. className?: string
  33. showCloseButton?: boolean
  34. }) {
  35. return (
  36. <Dialog {...props}>
  37. <DialogHeader className="sr-only">
  38. <DialogTitle>{title}</DialogTitle>
  39. <DialogDescription>{description}</DialogDescription>
  40. </DialogHeader>
  41. <DialogContent
  42. className={cn("overflow-hidden p-0", className)}
  43. showCloseButton={showCloseButton}
  44. >
  45. <Command className="[&_[cmdk-group-heading]]:text-muted-foreground **:data-[slot=command-input-wrapper]:h-12 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group]]:px-2 [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[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">
  46. {children}
  47. </Command>
  48. </DialogContent>
  49. </Dialog>
  50. )
  51. }
  52. function CommandInput({
  53. className,
  54. ...props
  55. }: React.ComponentProps<typeof CommandPrimitive.Input>) {
  56. return (
  57. <div
  58. data-slot="command-input-wrapper"
  59. className="flex h-9 items-center gap-2 border-b px-3"
  60. >
  61. <SearchIcon className="size-4 shrink-0 opacity-50" />
  62. <CommandPrimitive.Input
  63. data-slot="command-input"
  64. className={cn(
  65. "placeholder:text-muted-foreground flex h-10 w-full rounded-md bg-transparent py-3 text-sm outline-hidden disabled:cursor-not-allowed disabled:opacity-50",
  66. className
  67. )}
  68. {...props}
  69. />
  70. </div>
  71. )
  72. }
  73. function CommandList({
  74. className,
  75. ...props
  76. }: React.ComponentProps<typeof CommandPrimitive.List>) {
  77. return (
  78. <CommandPrimitive.List
  79. data-slot="command-list"
  80. className={cn(
  81. "max-h-[300px] scroll-py-1 overflow-x-hidden overflow-y-auto",
  82. className
  83. )}
  84. {...props}
  85. />
  86. )
  87. }
  88. function CommandEmpty({
  89. ...props
  90. }: React.ComponentProps<typeof CommandPrimitive.Empty>) {
  91. return (
  92. <CommandPrimitive.Empty
  93. data-slot="command-empty"
  94. className="py-6 text-center text-sm"
  95. {...props}
  96. />
  97. )
  98. }
  99. function CommandGroup({
  100. className,
  101. ...props
  102. }: React.ComponentProps<typeof CommandPrimitive.Group>) {
  103. return (
  104. <CommandPrimitive.Group
  105. data-slot="command-group"
  106. className={cn(
  107. "text-foreground [&_[cmdk-group-heading]]:text-muted-foreground overflow-hidden p-1 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium",
  108. className
  109. )}
  110. {...props}
  111. />
  112. )
  113. }
  114. function CommandSeparator({
  115. className,
  116. ...props
  117. }: React.ComponentProps<typeof CommandPrimitive.Separator>) {
  118. return (
  119. <CommandPrimitive.Separator
  120. data-slot="command-separator"
  121. className={cn("bg-border -mx-1 h-px", className)}
  122. {...props}
  123. />
  124. )
  125. }
  126. function CommandItem({
  127. className,
  128. ...props
  129. }: React.ComponentProps<typeof CommandPrimitive.Item>) {
  130. return (
  131. <CommandPrimitive.Item
  132. data-slot="command-item"
  133. className={cn(
  134. "data-[selected=true]:bg-accent data-[selected=true]:text-accent-foreground [&_svg:not([class*='text-'])]:text-muted-foreground relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
  135. className
  136. )}
  137. {...props}
  138. />
  139. )
  140. }
  141. function CommandShortcut({
  142. className,
  143. ...props
  144. }: React.ComponentProps<"span">) {
  145. return (
  146. <span
  147. data-slot="command-shortcut"
  148. className={cn(
  149. "text-muted-foreground ml-auto text-xs tracking-widest",
  150. className
  151. )}
  152. {...props}
  153. />
  154. )
  155. }
  156. export {
  157. Command,
  158. CommandDialog,
  159. CommandInput,
  160. CommandList,
  161. CommandEmpty,
  162. CommandGroup,
  163. CommandItem,
  164. CommandShortcut,
  165. CommandSeparator,
  166. }