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