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

command.tsx

245 lines6.7 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";
19​
20const CommandDialog = CommandDialogPrimitive.Root;
21​
22const CommandDialogPortal = CommandDialogPrimitive.Portal;
23​
24const CommandCreateHandle = CommandDialogPrimitive.createHandle;
25​
26function CommandDialogTrigger(props: CommandDialogPrimitive.Trigger.Props) {
27 return <CommandDialogPrimitive.Trigger data-slot="command-dialog-trigger" {...props} />;
29​
30function CommandDialogBackdrop({ className, ...props }: CommandDialogPrimitive.Backdrop.Props) {
31 return (
32 <CommandDialogPrimitive.Backdrop
33 className={cn(DIALOG_BACKDROP_CLASS, className)}
34 data-slot="command-dialog-backdrop"
35 {...props}
36 />
37 );
39​
40function CommandDialogViewport({ className, ...props }: CommandDialogPrimitive.Viewport.Props) {
41 return (
42 <CommandDialogPrimitive.Viewport
43 className={cn(
44 "pointer-events-none fixed inset-0 z-50 flex flex-col items-center px-4 py-[max(--spacing(4),4vh)] sm:py-[10vh]",
45 className,
46 )}
47 data-slot="command-dialog-viewport"
48 {...props}
49 />
50 );
52​
53function CommandDialogPopup({
54 className,
55 children,
56 onBackdropPointerDown,
57 ...props
58}: CommandDialogPrimitive.Popup.Props & {
59 onBackdropPointerDown?: React.PointerEventHandler<HTMLDivElement>;
60}) {
61 return (
62 <CommandDialogPortal>
63 <CommandDialogBackdrop onPointerDown={onBackdropPointerDown} />
64 <CommandDialogViewport>
65 <CommandDialogPrimitive.Popup
66 className={cn(
67 DIALOG_POPUP_CLASS,
68 "pointer-events-auto max-h-105 max-w-xl text-foreground",
69 className,
70 )}
71 data-slot="command-dialog-popup"
72 {...props}
73 >
74 {children}
75 </CommandDialogPrimitive.Popup>
76 </CommandDialogViewport>
77 </CommandDialogPortal>
78 );
80​
81function Command({
82 autoHighlight = "always",
83 keepHighlight = true,
84 ...props
85}: React.ComponentProps<typeof Autocomplete>) {
86 return (
87 <Autocomplete
88 autoHighlight={autoHighlight}
89 inline
90 keepHighlight={keepHighlight}
91 open
92 {...props}
93 />
94 );
96​
97function CommandInput({
98 className,
99 wrapperClassName,
100 placeholder,
101 ...props
102}: React.ComponentProps<typeof AutocompleteInput> & {
103 wrapperClassName?: string | undefined;
104}) {
105 return (
106 <div
107 className={cn(
108 "px-[var(--command-shell-inset)] py-1.5 [&_[data-slot=autocomplete-start-addon]]:ps-[calc(var(--command-shell-inset)+0.0625rem)]",
109 wrapperClassName,
110 )}
111 >
112 <AutocompleteInput
113 autoFocus
114 className={cn(
115 "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)]!",
116 className,
117 )}
118 placeholder={placeholder}
119 size="lg"
120 startAddon={<SearchIcon className="translate-x-0.5 text-icon-muted" />}
121 {...props}
122 />
123 </div>
124 );
126​
127function CommandList({ className, ...props }: React.ComponentProps<typeof AutocompleteList>) {
128 return (
129 <AutocompleteList
130 className={cn("not-empty:scroll-py-2 not-empty:p-2", className)}
131 data-slot="command-list"
132 {...props}
133 />
134 );
136​
137function CommandEmpty({ className, ...props }: React.ComponentProps<typeof AutocompleteEmpty>) {
138 return (
139 <AutocompleteEmpty
140 className={cn("not-empty:py-6", className)}
141 data-slot="command-empty"
142 {...props}
143 />
144 );
146​
147function CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
148 return (
149 <div
150 className={cn(
151 "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]",
152 className,
153 )}
154 {...props}
155 />
156 );
158​
159function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
160 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
162​
163function CommandGroupLabel({
164 className,
165 ...props
166}: React.ComponentProps<typeof AutocompleteGroupLabel>) {
167 return (
168 <AutocompleteGroupLabel className={className} data-slot="command-group-label" {...props} />
169 );
171​
172function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
173 return <AutocompleteCollection data-slot="command-collection" {...props} />;
175​
176function CommandItem({ className, ...props }: React.ComponentProps<typeof AutocompleteItem>) {
177 return (
178 <AutocompleteItem
179 className={cn(
180 "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",
181 className,
182 )}
183 data-slot="command-item"
184 {...props}
185 />
186 );
188​
189function CommandSeparator({
190 className,
191 ...props
192}: React.ComponentProps<typeof AutocompleteSeparator>) {
193 return (
194 <AutocompleteSeparator
195 className={cn("my-2", className)}
196 data-slot="command-separator"
197 {...props}
198 />
199 );
201​
202function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
203 return (
204 <kbd
205 className={cn(
206 "ms-auto font-medium font-sans text-secondary-label text-xs tracking-widest",
207 className,
208 )}
209 data-slot="command-shortcut"
210 {...props}
211 />
212 );
214​
215function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
216 return (
217 <div
218 className={cn(
219 "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",
220 className,
221 )}
222 data-slot="command-footer"
223 {...props}
224 />
225 );
227​
228export {
229 CommandCreateHandle,
230 Command,
231 CommandCollection,
232 CommandDialog,
233 CommandDialogPopup,
234 CommandDialogTrigger,
235 CommandEmpty,
236 CommandFooter,
237 CommandGroup,
238 CommandGroupLabel,
239 CommandInput,
240 CommandItem,
241 CommandList,
242 CommandPanel,
243 CommandSeparator,
244 CommandShortcut,
245};