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

command.tsx

261 lines7.7 KB

1import { Dialog as CommandDialogPrimitive } from "@base-ui/react/dialog";
2import { SearchIcon } from "lucide-react";
3import { useRef } from "react";
4import type * as React from "react";
5import { cn } from "~/lib/utils";
6import {
7 Autocomplete,
8 AutocompleteCollection,
9 AutocompleteGroup,
10 AutocompleteGroupLabel,
11 AutocompleteInput,
12 AutocompleteItem,
13 AutocompleteList,
14 AutocompleteListHeading,
15 AutocompleteListVirtualized,
16} from "~/components/ui/autocomplete";
17import { DIALOG_BACKDROP_CLASS, DIALOG_POPUP_CLASS } from "~/components/ui/dialog-styles";
18import { Button } from "~/components/ui/button";
19​
20const CommandDialog = CommandDialogPrimitive.Root;
21​
22const CommandDialogPortal = CommandDialogPrimitive.Portal;
23​
24function CommandDialogTrigger(props: CommandDialogPrimitive.Trigger.Props) {
25 return <CommandDialogPrimitive.Trigger data-slot="command-dialog-trigger" {...props} />;
27​
28function CommandDialogBackdrop({ className, ...props }: CommandDialogPrimitive.Backdrop.Props) {
29 return (
30 <CommandDialogPrimitive.Backdrop
31 className={cn(DIALOG_BACKDROP_CLASS, className)}
32 data-slot="command-dialog-backdrop"
33 {...props}
34 />
35 );
37​
38function CommandDialogViewport({ className, ...props }: CommandDialogPrimitive.Viewport.Props) {
39 return (
40 <CommandDialogPrimitive.Viewport
41 className={cn(
42 "pointer-events-none fixed inset-0 z-50 flex flex-col items-center px-4 py-[max(--spacing(4),4vh)] sm:py-[10vh]",
43 className,
44 )}
45 data-slot="command-dialog-viewport"
46 {...props}
47 />
48 );
50​
51function CommandDialogPopup({
52 className,
53 children,
54 onBackdropPointerDown,
55 ...props
56}: CommandDialogPrimitive.Popup.Props & {
57 onBackdropPointerDown?: React.PointerEventHandler<HTMLDivElement>;
58}) {
59 const popupRef = useRef<HTMLDivElement>(null);
60 return (
61 <CommandDialogPortal>
62 <CommandDialogBackdrop onPointerDown={onBackdropPointerDown} />
63 <CommandDialogViewport>
64 <CommandDialogPrimitive.Popup
65 // The first tabbable can be an input addon, such as a submenu's Back button.
66 initialFocus={() => popupRef.current?.querySelector("input") ?? true}
67 ref={popupRef}
68 className={cn(
69 DIALOG_POPUP_CLASS,
70 "pointer-events-auto max-h-105 max-w-xl text-foreground",
71 className,
72 )}
73 data-slot="command-dialog-popup"
74 {...props}
75 >
76 {children}
77 </CommandDialogPrimitive.Popup>
78 </CommandDialogViewport>
79 </CommandDialogPortal>
80 );
82​
83function Command({
84 autoHighlight = "always",
85 keepHighlight = true,
86 ...props
87}: React.ComponentProps<typeof Autocomplete>) {
88 return (
89 <Autocomplete
90 autoHighlight={autoHighlight}
91 inline
92 keepHighlight={keepHighlight}
93 open
94 {...props}
95 />
96 );
98​
99function CommandInput({
100 className,
101 placeholder,
102 ...props
103}: React.ComponentProps<typeof AutocompleteInput>) {
104 return (
105 <div className="px-[var(--command-shell-inset)] py-1.5 [&_[data-slot=autocomplete-start-addon]]:ps-[calc(var(--command-shell-inset)+0.0625rem)]">
106 <AutocompleteInput
107 autoFocus
108 className={cn(
109 "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)]!",
110 className,
111 )}
112 placeholder={placeholder}
113 size="lg"
114 startAddon={<SearchIcon className="translate-x-0.5 text-icon-muted" />}
115 {...props}
116 />
117 </div>
118 );
120​
121function CommandList({ className, ...props }: React.ComponentProps<typeof AutocompleteList>) {
122 return (
123 <AutocompleteList
124 className={cn("not-empty:scroll-py-2 not-empty:p-2", className)}
125 data-slot="command-list"
126 {...props}
127 />
128 );
130​
131function CommandListVirtualized({
132 className,
133 ...props
134}: React.ComponentProps<typeof AutocompleteListVirtualized>) {
135 return (
136 <AutocompleteListVirtualized
137 className={cn("flex max-h-[inherit] min-h-0 flex-col", className)}
138 data-slot="command-list"
139 {...props}
140 />
141 );
143​
144function CommandListHeading({
145 className,
146 ...props
147}: React.ComponentProps<typeof AutocompleteListHeading>) {
148 return (
149 <AutocompleteListHeading className={className} data-slot="command-group-label" {...props} />
150 );
152​
153function CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
154 return (
155 <div
156 className={cn(
157 "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]",
158 className,
159 )}
160 {...props}
161 />
162 );
164​
165function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
166 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
168​
169function CommandGroupLabel({
170 className,
171 ...props
172}: React.ComponentProps<typeof AutocompleteGroupLabel>) {
173 return (
174 <AutocompleteGroupLabel className={className} data-slot="command-group-label" {...props} />
175 );
177​
178function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
179 return <AutocompleteCollection data-slot="command-collection" {...props} />;
181​
182// Pass active to override primitive hover/keyboard highlights when the palette tracks its own row.
183function CommandItem({
184 className,
185 active,
186 ...props
187}: React.ComponentProps<typeof AutocompleteItem> & { active?: boolean }) {
188 return (
189 <AutocompleteItem
190 className={cn(
191 "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",
192 active !== undefined &&
193 "cursor-pointer hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit data-selected:bg-transparent data-selected:text-inherit [&[data-highlighted][data-selected]]:bg-transparent [&[data-highlighted][data-selected]]:text-inherit",
194 active && "bg-accent! text-accent-foreground!",
195 className,
196 )}
197 data-active={active || undefined}
198 data-slot="command-item"
199 {...props}
200 />
201 );
203​
204function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
205 return (
206 <kbd
207 className={cn(
208 "ms-auto font-medium font-sans text-secondary-label text-xs tracking-widest",
209 className,
210 )}
211 data-slot="command-shortcut"
212 {...props}
213 />
214 );
216​
217function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
218 return (
219 <div
220 className={cn(
221 "relative flex items-center justify-between gap-2 rounded-b-[calc(var(--radius-2xl)-1px)] border-t border-foreground/[0.07] 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",
222 className,
223 )}
224 data-slot="command-footer"
225 {...props}
226 />
227 );
229​
230function CommandFooterAction({
231 className,
232 ...props
233}: Omit<React.ComponentProps<typeof Button>, "size" | "variant">) {
234 return (
235 <Button
236 {...props}
237 variant="ghost-muted"
238 size="xs"
239 className={cn("h-auto px-2 text-xs hover:bg-transparent", className)}
240 />
241 );
243​
244export {
245 Command,
246 CommandCollection,
247 CommandDialog,
248 CommandDialogPopup,
249 CommandDialogTrigger,
250 CommandFooter,
251 CommandFooterAction,
252 CommandGroup,
253 CommandGroupLabel,
254 CommandInput,
255 CommandItem,
256 CommandList,
257 CommandListHeading,
258 CommandListVirtualized,
259 CommandPanel,
260 CommandShortcut,
261};