Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

command.tsx

241 lines7.1 KB

1"use client";
2​
3import { Dialog as CommandDialogPrimitive } from "@base-ui/react/dialog";
4import { SearchIcon } from "lucide-react";
5import type * as React from "react";
6import { cn } from "~/lib/utils";
7import {
8 Autocomplete,
9 AutocompleteCollection,
10 AutocompleteEmpty,
11 AutocompleteGroup,
12 AutocompleteGroupLabel,
13 AutocompleteInput,
14 AutocompleteItem,
15 AutocompleteList,
16 AutocompleteSeparator,
17} from "~/components/ui/autocomplete";
18​
19const CommandDialog = CommandDialogPrimitive.Root;
20​
21const CommandDialogPortal = CommandDialogPrimitive.Portal;
22​
23const CommandCreateHandle = CommandDialogPrimitive.createHandle;
24​
25function CommandDialogTrigger(props: CommandDialogPrimitive.Trigger.Props) {
26 return <CommandDialogPrimitive.Trigger data-slot="command-dialog-trigger" {...props} />;
28​
29function CommandDialogBackdrop({ className, ...props }: CommandDialogPrimitive.Backdrop.Props) {
30 return (
31 <CommandDialogPrimitive.Backdrop
32 className={cn(
33 "fixed inset-0 z-50 bg-black/20 backdrop-blur-[2px] transition-opacity duration-200 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:bg-black/55",
34 className,
35 )}
36 data-slot="command-dialog-backdrop"
37 {...props}
38 />
39 );
41​
42function CommandDialogViewport({ className, ...props }: CommandDialogPrimitive.Viewport.Props) {
43 return (
44 <CommandDialogPrimitive.Viewport
45 className={cn(
46 "pointer-events-none fixed inset-0 z-50 flex flex-col items-center px-4 py-[max(--spacing(4),4vh)] sm:py-[10vh]",
47 className,
48 )}
49 data-slot="command-dialog-viewport"
50 {...props}
51 />
52 );
54​
55function CommandDialogPopup({
56 className,
57 children,
58 onBackdropPointerDown,
59 ...props
60}: CommandDialogPrimitive.Popup.Props & {
61 onBackdropPointerDown?: React.PointerEventHandler<HTMLDivElement>;
62}) {
63 return (
64 <CommandDialogPortal>
65 <CommandDialogBackdrop onPointerDown={onBackdropPointerDown} />
66 <CommandDialogViewport>
67 <CommandDialogPrimitive.Popup
68 className={cn(
69 "chat-composer-glass command-palette-glass pointer-events-auto -translate-y-[calc(1.25rem*var(--nested-dialogs))] relative row-start-2 flex max-h-105 min-h-0 w-full min-w-0 max-w-xl scale-[calc(1-0.1*var(--nested-dialogs))] flex-col rounded-2xl bg-[color-mix(in_srgb,var(--background)_var(--glass-opacity),transparent)] text-foreground opacity-[calc(1-0.1*var(--nested-dialogs))] outline-none transition-[scale,opacity,translate] duration-200 ease-in-out will-change-transform data-nested:data-ending-style:translate-y-8 data-nested:data-starting-style:translate-y-8 data-nested-dialog-open:origin-top data-ending-style:scale-98 data-starting-style:scale-98 data-ending-style:opacity-0 data-starting-style:opacity-0 **:data-[slot=scroll-area-viewport]:data-has-overflow-y:pe-1",
70 className,
71 )}
72 data-slot="command-dialog-popup"
73 {...props}
74 >
75 {children}
76 </CommandDialogPrimitive.Popup>
77 </CommandDialogViewport>
78 </CommandDialogPortal>
79 );
81​
82function Command({
83 autoHighlight = "always",
84 keepHighlight = true,
85 ...props
86}: React.ComponentProps<typeof Autocomplete>) {
87 return (
88 <Autocomplete
89 autoHighlight={autoHighlight}
90 inline
91 keepHighlight={keepHighlight}
92 open
93 {...props}
94 />
95 );
97​
98function CommandInput({
99 className,
100 wrapperClassName,
101 placeholder,
102 ...props
103}: React.ComponentProps<typeof AutocompleteInput> & {
104 wrapperClassName?: string | undefined;
105}) {
106 return (
107 <div className={cn("px-2.5 py-1.5", wrapperClassName)}>
108 <AutocompleteInput
109 autoFocus
110 className={cn(
111 "border-transparent! bg-transparent! shadow-none before:hidden has-focus-visible:ring-0 placeholder:text-muted-foreground/80",
112 className,
113 )}
114 placeholder={placeholder}
115 size="lg"
116 startAddon={<SearchIcon />}
117 {...props}
118 />
119 </div>
120 );
122​
123function CommandList({ className, ...props }: React.ComponentProps<typeof AutocompleteList>) {
124 return (
125 <AutocompleteList
126 className={cn("not-empty:scroll-py-2 not-empty:p-2", className)}
127 data-slot="command-list"
128 {...props}
129 />
130 );
132​
133function CommandEmpty({ className, ...props }: React.ComponentProps<typeof AutocompleteEmpty>) {
134 return (
135 <AutocompleteEmpty
136 className={cn("not-empty:py-6", className)}
137 data-slot="command-empty"
138 {...props}
139 />
140 );
142​
143function CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
144 return (
145 <div
146 className={cn(
147 "relative min-h-0 overflow-hidden rounded-t-xl not-has-[+[data-slot=command-footer]]:rounded-b-2xl bg-transparent **:data-[slot=scroll-area-scrollbar]:mt-2 [touch-action:pan-y]",
148 className,
149 )}
150 {...props}
151 />
152 );
154​
155function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
156 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
158​
159function CommandGroupLabel({
160 className,
161 ...props
162}: React.ComponentProps<typeof AutocompleteGroupLabel>) {
163 return (
164 <AutocompleteGroupLabel className={className} data-slot="command-group-label" {...props} />
165 );
167​
168function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
169 return <AutocompleteCollection data-slot="command-collection" {...props} />;
171​
172function CommandItem({ className, ...props }: React.ComponentProps<typeof AutocompleteItem>) {
173 return (
174 <AutocompleteItem
175 className={cn(
176 "py-1.5 data-selected:bg-foreground/[0.06] data-highlighted:bg-foreground/[0.09] data-highlighted:text-foreground [&[data-highlighted][data-selected]]:bg-foreground/[0.09] [&[data-highlighted][data-selected]]:text-foreground",
177 className,
178 )}
179 data-slot="command-item"
180 {...props}
181 />
182 );
184​
185function CommandSeparator({
186 className,
187 ...props
188}: React.ComponentProps<typeof AutocompleteSeparator>) {
189 return (
190 <AutocompleteSeparator
191 className={cn("my-2", className)}
192 data-slot="command-separator"
193 {...props}
194 />
195 );
197​
198function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
199 return (
200 <kbd
201 className={cn(
202 "ms-auto font-medium font-sans text-muted-foreground text-xs tracking-widest",
203 className,
204 )}
205 data-slot="command-shortcut"
206 {...props}
207 />
208 );
210​
211function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
212 return (
213 <div
214 className={cn(
215 "relative flex items-center justify-between gap-2 rounded-b-[calc(var(--radius-2xl)-1px)] bg-foreground/[0.025] px-5 py-3 font-medium text-sm text-muted-foreground [&_[data-slot=kbd-group]]:font-sans [&_[data-slot=kbd]]:bg-foreground/[0.08] [&_[data-slot=kbd]]:text-foreground [&_[data-slot=kbd]]:ring-0",
216 className,
217 )}
218 data-slot="command-footer"
219 {...props}
220 />
221 );
223​
224export {
225 CommandCreateHandle,
226 Command,
227 CommandCollection,
228 CommandDialog,
229 CommandDialogPopup,
230 CommandDialogTrigger,
231 CommandEmpty,
232 CommandFooter,
233 CommandGroup,
234 CommandGroupLabel,
235 CommandInput,
236 CommandItem,
237 CommandList,
238 CommandPanel,
239 CommandSeparator,
240 CommandShortcut,
241};