Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

command.tsx

231 lines6.4 KB

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} />;
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 );
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 );
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 );
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 );
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 );
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 );
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 );
145​
146function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
147 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
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 );
158​
159function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
160 return <AutocompleteCollection data-slot="command-collection" {...props} />;
162​
163function CommandItem({ className, ...props }: React.ComponentProps<typeof AutocompleteItem>) {
164 return (
165 <AutocompleteItem
166 className={cn(
167 "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",
168 className,
169 )}
170 data-slot="command-item"
171 {...props}
172 />
173 );
175​
176function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
177 return (
178 <kbd
179 className={cn(
180 "ms-auto font-medium font-sans text-secondary-label text-xs tracking-widest",
181 className,
182 )}
183 data-slot="command-shortcut"
184 {...props}
185 />
186 );
188​
189function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
190 return (
191 <div
192 className={cn(
193 "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",
194 className,
195 )}
196 data-slot="command-footer"
197 {...props}
198 />
199 );
201​
202function CommandFooterAction({
203 className,
204 ...props
205}: Omit<React.ComponentProps<typeof Button>, "size" | "variant">) {
206 return (
207 <Button
208 {...props}
209 variant="ghost-muted"
210 size="xs"
211 className={cn("h-auto px-2 text-xs hover:bg-transparent", className)}
212 />
213 );
215​
216export {
217 Command,
218 CommandCollection,
219 CommandDialog,
220 CommandDialogPopup,
221 CommandDialogTrigger,
222 CommandFooter,
223 CommandFooterAction,
224 CommandGroup,
225 CommandGroupLabel,
226 CommandInput,
227 CommandItem,
228 CommandList,
229 CommandPanel,
230 CommandShortcut,
231};