|
| 1 | +import * as React from 'react'; |
| 2 | +import { type DialogProps } from '@radix-ui/react-dialog'; |
| 3 | +import { Command as CommandPrimitive } from 'cmdk'; |
| 4 | + |
| 5 | +import { cn } from '@/utils'; |
| 6 | +import { Dialog, DialogContent } from '@/ui/dialog'; |
| 7 | + |
| 8 | +const Command = React.forwardRef< |
| 9 | + React.ElementRef<typeof CommandPrimitive>, |
| 10 | + React.ComponentPropsWithoutRef<typeof CommandPrimitive> |
| 11 | +>(({ className, ...props }, ref) => ( |
| 12 | + <CommandPrimitive |
| 13 | + ref={ref} |
| 14 | + className={cn( |
| 15 | + 'flex h-full w-full flex-col overflow-hidden rounded-md bg-white text-neutral-950 dark:bg-neutral-900 dark:text-neutral-50', |
| 16 | + className, |
| 17 | + )} |
| 18 | + {...props} |
| 19 | + /> |
| 20 | +)); |
| 21 | +Command.displayName = CommandPrimitive.displayName; |
| 22 | + |
| 23 | +interface CommandDialogProps extends DialogProps {} |
| 24 | + |
| 25 | +const CommandDialog = ({ children, ...props }: CommandDialogProps) => { |
| 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-neutral-500 dark:[&_[cmdk-group-heading]]:text-neutral-400 [&_[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-[18px] [&_[cmdk-item]_svg]:w-[18px]"> |
| 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 px-3" cmdk-input-wrapper=""> |
| 42 | + <CommandPrimitive.Input |
| 43 | + ref={ref} |
| 44 | + className={cn( |
| 45 | + 'flex h-10 w-full rounded-md bg-transparent py-3 text-sm outline-none placeholder:text-neutral-500 disabled:cursor-not-allowed disabled:opacity-50 dark:placeholder:text-neutral-400', |
| 46 | + className, |
| 47 | + )} |
| 48 | + {...props} |
| 49 | + /> |
| 50 | + </div> |
| 51 | +)); |
| 52 | + |
| 53 | +CommandInput.displayName = CommandPrimitive.Input.displayName; |
| 54 | + |
| 55 | +const CommandList = React.forwardRef< |
| 56 | + React.ElementRef<typeof CommandPrimitive.List>, |
| 57 | + React.ComponentPropsWithoutRef<typeof CommandPrimitive.List> |
| 58 | +>(({ className, ...props }, ref) => ( |
| 59 | + <CommandPrimitive.List |
| 60 | + ref={ref} |
| 61 | + className={cn('max-h-[300px] overflow-y-auto overflow-x-hidden', className)} |
| 62 | + {...props} |
| 63 | + /> |
| 64 | +)); |
| 65 | + |
| 66 | +CommandList.displayName = CommandPrimitive.List.displayName; |
| 67 | + |
| 68 | +const CommandEmpty = React.forwardRef< |
| 69 | + React.ElementRef<typeof CommandPrimitive.Empty>, |
| 70 | + React.ComponentPropsWithoutRef<typeof CommandPrimitive.Empty> |
| 71 | +>((props, ref) => ( |
| 72 | + <CommandPrimitive.Empty |
| 73 | + ref={ref} |
| 74 | + className="py-6 text-center text-sm" |
| 75 | + {...props} |
| 76 | + /> |
| 77 | +)); |
| 78 | + |
| 79 | +CommandEmpty.displayName = CommandPrimitive.Empty.displayName; |
| 80 | + |
| 81 | +const CommandGroup = React.forwardRef< |
| 82 | + React.ElementRef<typeof CommandPrimitive.Group>, |
| 83 | + React.ComponentPropsWithoutRef<typeof CommandPrimitive.Group> |
| 84 | +>(({ className, ...props }, ref) => ( |
| 85 | + <CommandPrimitive.Group |
| 86 | + ref={ref} |
| 87 | + className={cn( |
| 88 | + 'overflow-hidden p-1 text-neutral-950 dark:text-neutral-50 [&_[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-neutral-500 dark:[&_[cmdk-group-heading]]:text-neutral-400', |
| 89 | + className, |
| 90 | + )} |
| 91 | + {...props} |
| 92 | + /> |
| 93 | +)); |
| 94 | + |
| 95 | +CommandGroup.displayName = CommandPrimitive.Group.displayName; |
| 96 | + |
| 97 | +const CommandSeparator = React.forwardRef< |
| 98 | + React.ElementRef<typeof CommandPrimitive.Separator>, |
| 99 | + React.ComponentPropsWithoutRef<typeof CommandPrimitive.Separator> |
| 100 | +>(({ className, ...props }, ref) => ( |
| 101 | + <CommandPrimitive.Separator |
| 102 | + ref={ref} |
| 103 | + className={cn( |
| 104 | + '-mx-1 my-2 h-px bg-neutral-200 dark:bg-neutral-800', |
| 105 | + className, |
| 106 | + )} |
| 107 | + {...props} |
| 108 | + /> |
| 109 | +)); |
| 110 | +CommandSeparator.displayName = CommandPrimitive.Separator.displayName; |
| 111 | + |
| 112 | +const CommandItem = React.forwardRef< |
| 113 | + React.ElementRef<typeof CommandPrimitive.Item>, |
| 114 | + React.ComponentPropsWithoutRef<typeof CommandPrimitive.Item> |
| 115 | +>(({ className, ...props }, ref) => ( |
| 116 | + <CommandPrimitive.Item |
| 117 | + ref={ref} |
| 118 | + className={cn( |
| 119 | + 'flex cursor-default items-center gap-2 rounded-md px-2 text-sm outline-none data-[disabled=true]:pointer-events-none data-[selected=true]:bg-neutral-100 data-[selected=true]:text-neutral-900 data-[disabled=true]:opacity-50 dark:data-[selected=true]:bg-neutral-800 dark:data-[selected=true]:text-neutral-50', |
| 120 | + className, |
| 121 | + )} |
| 122 | + {...props} |
| 123 | + /> |
| 124 | +)); |
| 125 | + |
| 126 | +CommandItem.displayName = CommandPrimitive.Item.displayName; |
| 127 | + |
| 128 | +const CommandShortcut = ({ |
| 129 | + className, |
| 130 | + ...props |
| 131 | +}: React.HTMLAttributes<HTMLSpanElement>) => { |
| 132 | + return ( |
| 133 | + <span |
| 134 | + className={cn( |
| 135 | + 'ml-auto text-xs tracking-widest text-neutral-500 dark:text-neutral-400', |
| 136 | + className, |
| 137 | + )} |
| 138 | + {...props} |
| 139 | + /> |
| 140 | + ); |
| 141 | +}; |
| 142 | +CommandShortcut.displayName = 'CommandShortcut'; |
| 143 | + |
| 144 | +export { |
| 145 | + Command, |
| 146 | + CommandDialog, |
| 147 | + CommandInput, |
| 148 | + CommandList, |
| 149 | + CommandEmpty, |
| 150 | + CommandGroup, |
| 151 | + CommandItem, |
| 152 | + CommandShortcut, |
| 153 | + CommandSeparator, |
| 154 | +}; |
0 commit comments