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 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 CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
127 return (
128 <div
129 className={cn(
130 "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]",
131 className,
132 )}
133 {...props}
134 />
135 );
136}
137
138function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
139 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
140}
141
142function CommandGroupLabel({
143 className,
144 ...props
145}: React.ComponentProps<typeof AutocompleteGroupLabel>) {
146 return (
147 <AutocompleteGroupLabel className={className} data-slot="command-group-label" {...props} />
148 );
149}
150
151function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
152 return <AutocompleteCollection data-slot="command-collection" {...props} />;
153}
154
155// Pass `active` when the palette tracks the highlighted row itself; the
156// primitive's hover and keyboard highlight are then ignored so the two never
157// disagree, and the row shows the active surface instead.
158function CommandItem({
159 className,
160 active,
161 ...props
162}: React.ComponentProps<typeof AutocompleteItem> & { active?: boolean }) {
163 return (
164 <AutocompleteItem
165 className={cn(
166 "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",
167 active !== undefined &&
168 "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",
169 active && "bg-accent! text-accent-foreground!",
170 className,
171 )}
172 data-active={active || undefined}
173 data-slot="command-item"
174 {...props}
175 />
176 );
177}
178
179function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
180 return (
181 <kbd
182 className={cn(
183 "ms-auto font-medium font-sans text-secondary-label text-xs tracking-widest",
184 className,
185 )}
186 data-slot="command-shortcut"
187 {...props}
188 />
189 );
190}
191
192function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
193 return (
194 <div
195 className={cn(
196 "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",
197 className,
198 )}
199 data-slot="command-footer"
200 {...props}
201 />
202 );
203}
204
205function CommandFooterAction({
206 className,
207 ...props
208}: Omit<React.ComponentProps<typeof Button>, "size" | "variant">) {
209 return (
210 <Button
211 {...props}
212 variant="ghost-muted"
213 size="xs"
214 className={cn("h-auto px-2 text-xs hover:bg-transparent", className)}
215 />
216 );
217}
218
219export {
220 Command,
221 CommandCollection,
222 CommandDialog,
223 CommandDialogPopup,
224 CommandDialogTrigger,
225 CommandFooter,
226 CommandFooterAction,
227 CommandGroup,
228 CommandGroupLabel,
229 CommandInput,
230 CommandItem,
231 CommandList,
232 CommandPanel,
233 CommandShortcut,
234};