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