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` when the palette tracks the highlighted row itself; the
178// primitive's hover and keyboard highlight are then ignored so the two never
179// disagree, and the row shows the active surface instead.
180function CommandItem({
181 className,
182 active,
183 ...props
184}: React.ComponentProps<typeof AutocompleteItem> & { active?: boolean }) {
185 return (
186 <AutocompleteItem
187 className={cn(
188 "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",
189 active !== undefined &&
190 "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",
191 active && "bg-accent! text-accent-foreground!",
192 className,
193 )}
194 data-active={active || undefined}
195 data-slot="command-item"
196 {...props}
197 />
198 );
199}
200
201function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
202 return (
203 <kbd
204 className={cn(
205 "ms-auto font-medium font-sans text-secondary-label text-xs tracking-widest",
206 className,
207 )}
208 data-slot="command-shortcut"
209 {...props}
210 />
211 );
212}
213
214function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
215 return (
216 <div
217 className={cn(
218 "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",
219 className,
220 )}
221 data-slot="command-footer"
222 {...props}
223 />
224 );
225}
226
227function CommandFooterAction({
228 className,
229 ...props
230}: Omit<React.ComponentProps<typeof Button>, "size" | "variant">) {
231 return (
232 <Button
233 {...props}
234 variant="ghost-muted"
235 size="xs"
236 className={cn("h-auto px-2 text-xs hover:bg-transparent", className)}
237 />
238 );
239}
240
241export {
242 Command,
243 CommandCollection,
244 CommandDialog,
245 CommandDialogPopup,
246 CommandDialogTrigger,
247 CommandFooter,
248 CommandFooterAction,
249 CommandGroup,
250 CommandGroupLabel,
251 CommandInput,
252 CommandItem,
253 CommandList,
254 CommandListHeading,
255 CommandListVirtualized,
256 CommandPanel,
257 CommandShortcut,
258};