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