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