Open History to see the last commit
1"use client";
2
3import { Dialog as CommandDialogPrimitive } from "@base-ui/react/dialog";
4import { SearchIcon } from "lucide-react";
5import type * as React from "react";
6import { cn } from "~/lib/utils";
7import {
8 Autocomplete,
9 AutocompleteCollection,
10 AutocompleteGroup,
11 AutocompleteGroupLabel,
12 AutocompleteInput,
13 AutocompleteItem,
14 AutocompleteList,
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 wrapperClassName,
97 placeholder,
98 ...props
99}: React.ComponentProps<typeof AutocompleteInput> & {
100 wrapperClassName?: string | undefined;
101}) {
102 return (
103 <div
104 className={cn(
105 "px-[var(--command-shell-inset)] py-1.5 [&_[data-slot=autocomplete-start-addon]]:ps-[calc(var(--command-shell-inset)+0.0625rem)]",
106 wrapperClassName,
107 )}
108 >
109 <AutocompleteInput
110 autoFocus
111 className={cn(
112 "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)]!",
113 className,
114 )}
115 placeholder={placeholder}
116 size="lg"
117 startAddon={<SearchIcon className="translate-x-0.5 text-icon-muted" />}
118 {...props}
119 />
120 </div>
121 );
122}
123
124function CommandList({ className, ...props }: React.ComponentProps<typeof AutocompleteList>) {
125 return (
126 <AutocompleteList
127 className={cn("not-empty:scroll-py-2 not-empty:p-2", className)}
128 data-slot="command-list"
129 {...props}
130 />
131 );
132}
133
134function CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
135 return (
136 <div
137 className={cn(
138 "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]",
139 className,
140 )}
141 {...props}
142 />
143 );
144}
145
146function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
147 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
148}
149
150function CommandGroupLabel({
151 className,
152 ...props
153}: React.ComponentProps<typeof AutocompleteGroupLabel>) {
154 return (
155 <AutocompleteGroupLabel className={className} data-slot="command-group-label" {...props} />
156 );
157}
158
159function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
160 return <AutocompleteCollection data-slot="command-collection" {...props} />;
161}
162
163// Pass `active` when the palette tracks the highlighted row itself; the
164// primitive's hover and keyboard highlight are then ignored so the two never
165// disagree, and the row shows the active surface instead.
166function CommandItem({
167 className,
168 active,
169 ...props
170}: React.ComponentProps<typeof AutocompleteItem> & { active?: boolean }) {
171 return (
172 <AutocompleteItem
173 className={cn(
174 "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",
175 active !== undefined &&
176 "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",
177 active && "bg-accent! text-accent-foreground!",
178 className,
179 )}
180 data-active={active || undefined}
181 data-slot="command-item"
182 {...props}
183 />
184 );
185}
186
187function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
188 return (
189 <kbd
190 className={cn(
191 "ms-auto font-medium font-sans text-secondary-label text-xs tracking-widest",
192 className,
193 )}
194 data-slot="command-shortcut"
195 {...props}
196 />
197 );
198}
199
200function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
201 return (
202 <div
203 className={cn(
204 "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",
205 className,
206 )}
207 data-slot="command-footer"
208 {...props}
209 />
210 );
211}
212
213function CommandFooterAction({
214 className,
215 ...props
216}: Omit<React.ComponentProps<typeof Button>, "size" | "variant">) {
217 return (
218 <Button
219 {...props}
220 variant="ghost-muted"
221 size="xs"
222 className={cn("h-auto px-2 text-xs hover:bg-transparent", className)}
223 />
224 );
225}
226
227export {
228 Command,
229 CommandCollection,
230 CommandDialog,
231 CommandDialogPopup,
232 CommandDialogTrigger,
233 CommandFooter,
234 CommandFooterAction,
235 CommandGroup,
236 CommandGroupLabel,
237 CommandInput,
238 CommandItem,
239 CommandList,
240 CommandPanel,
241 CommandShortcut,
242};