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

command.tsx

261 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";
18import { DIALOG_BACKDROP_CLASS, DIALOG_POPUP_CLASS } from "~/components/ui/dialog-styles";
19import { Button } from "~/components/ui/button";
20​
21const CommandDialog = CommandDialogPrimitive.Root;
22​
23const CommandDialogPortal = CommandDialogPrimitive.Portal;
24​
25const CommandCreateHandle = CommandDialogPrimitive.createHandle;
26​
27function CommandDialogTrigger(props: CommandDialogPrimitive.Trigger.Props) {
28 return <CommandDialogPrimitive.Trigger data-slot="command-dialog-trigger" {...props} />;
30​
31function CommandDialogBackdrop({ className, ...props }: CommandDialogPrimitive.Backdrop.Props) {
32 return (
33 <CommandDialogPrimitive.Backdrop
34 className={cn(DIALOG_BACKDROP_CLASS, className)}
35 data-slot="command-dialog-backdrop"
36 {...props}
37 />
38 );
40​
41function CommandDialogViewport({ className, ...props }: CommandDialogPrimitive.Viewport.Props) {
42 return (
43 <CommandDialogPrimitive.Viewport
44 className={cn(
45 "pointer-events-none fixed inset-0 z-50 flex flex-col items-center px-4 py-[max(--spacing(4),4vh)] sm:py-[10vh]",
46 className,
47 )}
48 data-slot="command-dialog-viewport"
49 {...props}
50 />
51 );
53​
54function CommandDialogPopup({
55 className,
56 children,
57 onBackdropPointerDown,
58 ...props
59}: CommandDialogPrimitive.Popup.Props & {
60 onBackdropPointerDown?: React.PointerEventHandler<HTMLDivElement>;
61}) {
62 return (
63 <CommandDialogPortal>
64 <CommandDialogBackdrop onPointerDown={onBackdropPointerDown} />
65 <CommandDialogViewport>
66 <CommandDialogPrimitive.Popup
67 className={cn(
68 DIALOG_POPUP_CLASS,
69 "pointer-events-auto max-h-105 max-w-xl text-foreground",
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
108 className={cn(
109 "px-[var(--command-shell-inset)] py-1.5 [&_[data-slot=autocomplete-start-addon]]:ps-[calc(var(--command-shell-inset)+0.0625rem)]",
110 wrapperClassName,
111 )}
112 >
113 <AutocompleteInput
114 autoFocus
115 className={cn(
116 "border-transparent! bg-transparent! shadow-none before:hidden has-focus-visible:ring-0 placeholder:text-placeholder *:data-[slot=autocomplete-input]:ps-9! sm:*:data-[slot=autocomplete-input]:ps-[calc(var(--command-shell-inset)+1.5rem)]!",
117 className,
118 )}
119 placeholder={placeholder}
120 size="lg"
121 startAddon={<SearchIcon className="translate-x-0.5 text-icon-muted" />}
122 {...props}
123 />
124 </div>
125 );
127​
128function CommandList({ className, ...props }: React.ComponentProps<typeof AutocompleteList>) {
129 return (
130 <AutocompleteList
131 className={cn("not-empty:scroll-py-2 not-empty:p-2", className)}
132 data-slot="command-list"
133 {...props}
134 />
135 );
137​
138function CommandEmpty({ className, ...props }: React.ComponentProps<typeof AutocompleteEmpty>) {
139 return (
140 <AutocompleteEmpty
141 className={cn("not-empty:py-6", className)}
142 data-slot="command-empty"
143 {...props}
144 />
145 );
147​
148function CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
149 return (
150 <div
151 className={cn(
152 "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]",
153 className,
154 )}
155 {...props}
156 />
157 );
159​
160function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
161 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
163​
164function CommandGroupLabel({
165 className,
166 ...props
167}: React.ComponentProps<typeof AutocompleteGroupLabel>) {
168 return (
169 <AutocompleteGroupLabel className={className} data-slot="command-group-label" {...props} />
170 );
172​
173function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
174 return <AutocompleteCollection data-slot="command-collection" {...props} />;
176​
177function CommandItem({ className, ...props }: React.ComponentProps<typeof AutocompleteItem>) {
178 return (
179 <AutocompleteItem
180 className={cn(
181 "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",
182 className,
183 )}
184 data-slot="command-item"
185 {...props}
186 />
187 );
189​
190function CommandSeparator({
191 className,
192 ...props
193}: React.ComponentProps<typeof AutocompleteSeparator>) {
194 return (
195 <AutocompleteSeparator
196 className={cn("my-2", className)}
197 data-slot="command-separator"
198 {...props}
199 />
200 );
202​
203function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
204 return (
205 <kbd
206 className={cn(
207 "ms-auto font-medium font-sans text-secondary-label text-xs tracking-widest",
208 className,
209 )}
210 data-slot="command-shortcut"
211 {...props}
212 />
213 );
215​
216function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
217 return (
218 <div
219 className={cn(
220 "relative flex items-center justify-between gap-2 rounded-b-[calc(var(--radius-2xl)-1px)] bg-foreground/[0.025] px-[var(--command-content-inset)] py-2.5 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",
221 className,
222 )}
223 data-slot="command-footer"
224 {...props}
225 />
226 );
228​
229function CommandFooterAction({
230 className,
231 ...props
232}: Omit<React.ComponentProps<typeof Button>, "size" | "variant">) {
233 return (
234 <Button
235 {...props}
236 variant="ghost-muted"
237 size="xs"
238 className={cn("h-auto px-2 text-xs hover:bg-transparent", className)}
239 />
240 );
242​
243export {
244 CommandCreateHandle,
245 Command,
246 CommandCollection,
247 CommandDialog,
248 CommandDialogPopup,
249 CommandDialogTrigger,
250 CommandEmpty,
251 CommandFooter,
252 CommandFooterAction,
253 CommandGroup,
254 CommandGroupLabel,
255 CommandInput,
256 CommandItem,
257 CommandList,
258 CommandPanel,
259 CommandSeparator,
260 CommandShortcut,
261};