返回 AiToEarn
command.tsx
根目录 / project / aitoearn-web / src / components / ui / command.tsx
1 'use client'
2
3 import type { DialogProps } from '@radix-ui/react-dialog'
4 import { Command as CommandPrimitive } from 'cmdk'
5 import { Search } from 'lucide-react'
6 import * as React from 'react'
7
8 import { Dialog, DialogContent } from '@/components/ui/dialog'
9 import { cn } from '@/utils/className'
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 function CommandDialog({ children, ...props }: DialogProps) {
27 return (
28 <Dialog {...props}>
29 <DialogContent className="overflow-hidden p-0">
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-10 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, style, ...props }, ref) => (
61 <CommandPrimitive.List
62 ref={ref}
63 className={cn('max-h-[300px] overflow-y-auto overflow-x-hidden', className)}
64 style={{ overscrollBehavior: 'contain', ...style }}
65 {...props}
66 />
67 ))
68
69 CommandList.displayName = CommandPrimitive.List.displayName
70
71 const CommandEmpty = React.forwardRef<
72 React.ElementRef<typeof CommandPrimitive.Empty>,
73 React.ComponentPropsWithoutRef<typeof CommandPrimitive.Empty>
74 >((props, ref) => (
75 <CommandPrimitive.Empty ref={ref} className="py-6 text-center text-sm" {...props} />
76 ))
77
78 CommandEmpty.displayName = CommandPrimitive.Empty.displayName
79
80 const CommandGroup = React.forwardRef<
81 React.ElementRef<typeof CommandPrimitive.Group>,
82 React.ComponentPropsWithoutRef<typeof CommandPrimitive.Group>
83 >(({ className, ...props }, ref) => (
84 <CommandPrimitive.Group
85 ref={ref}
86 className={cn(
87 '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',
88 className,
89 )}
90 {...props}
91 />
92 ))
93
94 CommandGroup.displayName = CommandPrimitive.Group.displayName
95
96 const CommandSeparator = React.forwardRef<
97 React.ElementRef<typeof CommandPrimitive.Separator>,
98 React.ComponentPropsWithoutRef<typeof CommandPrimitive.Separator>
99 >(({ className, ...props }, ref) => (
100 <CommandPrimitive.Separator
101 ref={ref}
102 className={cn('-mx-1 h-px bg-border', className)}
103 {...props}
104 />
105 ))
106 CommandSeparator.displayName = CommandPrimitive.Separator.displayName
107
108 const CommandItem = React.forwardRef<
109 React.ElementRef<typeof CommandPrimitive.Item>,
110 React.ComponentPropsWithoutRef<typeof CommandPrimitive.Item>
111 >(({ className, ...props }, ref) => (
112 <CommandPrimitive.Item
113 ref={ref}
114 className={cn(
115 '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',
116 className,
117 )}
118 {...props}
119 />
120 ))
121
122 CommandItem.displayName = CommandPrimitive.Item.displayName
123
124 function CommandShortcut({ className, ...props }: React.HTMLAttributes<HTMLSpanElement>) {
125 return (
126 <span
127 className={cn('ml-auto text-xs tracking-widest text-muted-foreground', className)}
128 {...props}
129 />
130 )
131 }
132 CommandShortcut.displayName = 'CommandShortcut'
133
134 export {
135 Command,
136 CommandDialog,
137 CommandEmpty,
138 CommandGroup,
139 CommandInput,
140 CommandItem,
141 CommandList,
142 CommandSeparator,
143 CommandShortcut,
144 }
145
145 lines Plain Text