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