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