Open History to see the last commit
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 { useNativeTitlebarHeight } from "~/lib/windowControlsOverlay";
7import {
8 Autocomplete,
9 AutocompleteCollection,
10 AutocompleteGroup,
11 AutocompleteGroupLabel,
12 AutocompleteInput,
13 AutocompleteItem,
14 AutocompleteList,
15 AutocompleteListHeading,
16 AutocompleteListVirtualized,
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
25function CommandDialogTrigger(props: CommandDialogPrimitive.Trigger.Props) {
26 return <CommandDialogPrimitive.Trigger data-slot="command-dialog-trigger" {...props} />;
27}
28
29function CommandDialogBackdrop({ className, ...props }: CommandDialogPrimitive.Backdrop.Props) {
30 return (
31 <CommandDialogPrimitive.Backdrop
32 className={cn(DIALOG_BACKDROP_CLASS, "data-ending-style:pointer-events-none", className)}
33 data-slot="command-dialog-backdrop"
34 {...props}
35 />
36 );
37}
38
39function CommandDialogViewport({ className, ...props }: CommandDialogPrimitive.Viewport.Props) {
40 // Short desktop windows would put the search field under the native window controls.
41 const nativeTitlebarHeight = useNativeTitlebarHeight();
42 const titlebarStyle = {
43 "--command-titlebar-height": `${nativeTitlebarHeight}px`,
44 } as React.CSSProperties;
45 return (
46 <CommandDialogPrimitive.Viewport
47 className={cn(
48 "pointer-events-none fixed inset-0 z-50 flex flex-col items-center px-4 pt-[max(--spacing(4),4vh,calc(var(--command-titlebar-height)+--spacing(2)))] pb-[max(--spacing(4),4vh)] sm:pt-[max(10vh,calc(var(--command-titlebar-height)+--spacing(2)))] sm:pb-[10vh]",
49 className,
50 )}
51 data-slot="command-dialog-viewport"
52 style={titlebarStyle}
53 {...props}
54 />
55 );
56}
57
58function CommandDialogPopup({
59 className,
60 children,
61 onBackdropPointerDown,
62 ...props
63}: CommandDialogPrimitive.Popup.Props & {
64 onBackdropPointerDown?: React.PointerEventHandler<HTMLDivElement>;
65}) {
66 const popupRef = useRef<HTMLDivElement>(null);
67 return (
68 <CommandDialogPortal>
69 <CommandDialogBackdrop onPointerDown={onBackdropPointerDown} />
70 <CommandDialogViewport>
71 <CommandDialogPrimitive.Popup
72 // The first tabbable can be an input addon, such as a submenu's Back button.
73 initialFocus={() => popupRef.current?.querySelector("input") ?? true}
74 ref={popupRef}
75 className={cn(
76 DIALOG_POPUP_CLASS,
77 "pointer-events-auto max-h-105 max-w-xl text-foreground data-ending-style:pointer-events-none",
78 className,
79 )}
80 data-slot="command-dialog-popup"
81 {...props}
82 >
83 {children}
84 </CommandDialogPrimitive.Popup>
85 </CommandDialogViewport>
86 </CommandDialogPortal>
87 );
88}
89
90function Command({
91 autoHighlight = "always",
92 keepHighlight = true,
93 ...props
94}: React.ComponentProps<typeof Autocomplete>) {
95 return (
96 <Autocomplete
97 autoHighlight={autoHighlight}
98 inline
99 keepHighlight={keepHighlight}
100 open
101 {...props}
102 />
103 );
104}
105
106function CommandInput({
107 className,
108 placeholder,
109 ...props
110}: React.ComponentProps<typeof AutocompleteInput>) {
111 return (
112 <div className="px-[var(--command-shell-inset)] py-1.5 [&_[data-slot=autocomplete-start-addon]]:ps-[calc(var(--command-shell-inset)+0.0625rem)]">
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 );
126}
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 );
136}
137
138function CommandListVirtualized({
139 className,
140 ...props
141}: React.ComponentProps<typeof AutocompleteListVirtualized>) {
142 return (
143 <AutocompleteListVirtualized
144 className={cn("flex max-h-[inherit] min-h-0 flex-col", className)}
145 data-slot="command-list"
146 {...props}
147 />
148 );
149}
150
151function CommandListHeading({
152 className,
153 ...props
154}: React.ComponentProps<typeof AutocompleteListHeading>) {
155 return (
156 <AutocompleteListHeading className={className} data-slot="command-group-label" {...props} />
157 );
158}
159
160function CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
161 return (
162 <div
163 className={cn(
164 "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]",
165 className,
166 )}
167 {...props}
168 />
169 );
170}
171
172function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
173 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
174}
175
176function CommandGroupLabel({
177 className,
178 ...props
179}: React.ComponentProps<typeof AutocompleteGroupLabel>) {
180 return (
181 <AutocompleteGroupLabel className={className} data-slot="command-group-label" {...props} />
182 );
183}
184
185function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
186 return <AutocompleteCollection data-slot="command-collection" {...props} />;
187}
188
189// Pass active to override primitive hover/keyboard highlights when the palette tracks its own row.
190function CommandItem({
191 className,
192 active,
193 ...props
194}: React.ComponentProps<typeof AutocompleteItem> & { active?: boolean }) {
195 return (
196 <AutocompleteItem
197 className={cn(
198 "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",
199 active !== undefined &&
200 "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",
201 active && "bg-accent! text-accent-foreground!",
202 className,
203 )}
204 data-active={active || undefined}
205 data-slot="command-item"
206 {...props}
207 />
208 );
209}
210
211function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
212 return (
213 <kbd
214 className={cn(
215 "ms-auto font-medium font-sans text-secondary-label text-xs tracking-widest",
216 className,
217 )}
218 data-slot="command-shortcut"
219 {...props}
220 />
221 );
222}
223
224function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
225 return (
226 <div
227 className={cn(
228 "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",
229 className,
230 )}
231 data-slot="command-footer"
232 {...props}
233 />
234 );
235}
236
237function CommandFooterAction({
238 className,
239 ...props
240}: Omit<React.ComponentProps<typeof Button>, "size" | "variant">) {
241 return (
242 <Button
243 {...props}
244 variant="ghost-muted"
245 size="xs"
246 className={cn("h-auto px-2 text-xs hover:bg-transparent", className)}
247 />
248 );
249}
250
251export {
252 Command,
253 CommandCollection,
254 CommandDialog,
255 CommandDialogPopup,
256 CommandDialogTrigger,
257 CommandFooter,
258 CommandFooterAction,
259 CommandGroup,
260 CommandGroupLabel,
261 CommandInput,
262 CommandItem,
263 CommandList,
264 CommandListHeading,
265 CommandListVirtualized,
266 CommandPanel,
267 CommandShortcut,
268};