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

command.tsx

240 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 AutocompleteEmpty,
11 AutocompleteGroup,
12 AutocompleteGroupLabel,
13 AutocompleteInput,
14 AutocompleteItem,
15 AutocompleteList,
16 AutocompleteSeparator,
17} from "~/components/ui/autocomplete";
18import { DIALOG_BACKDROP_CLASS, DIALOG_POPUP_CLASS } from "~/components/ui/dialog-styles";
19​
20const CommandDialog = CommandDialogPrimitive.Root;
21​
22const CommandDialogPortal = CommandDialogPrimitive.Portal;
23​
24const CommandCreateHandle = CommandDialogPrimitive.createHandle;
25​
26function CommandDialogTrigger(props: CommandDialogPrimitive.Trigger.Props) {
27 return <CommandDialogPrimitive.Trigger data-slot="command-dialog-trigger" {...props} />;
29​
30function CommandDialogBackdrop({ className, ...props }: CommandDialogPrimitive.Backdrop.Props) {
31 return (
32 <CommandDialogPrimitive.Backdrop
33 className={cn(DIALOG_BACKDROP_CLASS, className)}
34 data-slot="command-dialog-backdrop"
35 {...props}
36 />
37 );
39​
40function CommandDialogViewport({ className, ...props }: CommandDialogPrimitive.Viewport.Props) {
41 return (
42 <CommandDialogPrimitive.Viewport
43 className={cn(
44 "pointer-events-none fixed inset-0 z-50 flex flex-col items-center px-4 py-[max(--spacing(4),4vh)] sm:py-[10vh]",
45 className,
46 )}
47 data-slot="command-dialog-viewport"
48 {...props}
49 />
50 );
52​
53function CommandDialogPopup({
54 className,
55 children,
56 onBackdropPointerDown,
57 ...props
58}: CommandDialogPrimitive.Popup.Props & {
59 onBackdropPointerDown?: React.PointerEventHandler<HTMLDivElement>;
60}) {
61 return (
62 <CommandDialogPortal>
63 <CommandDialogBackdrop onPointerDown={onBackdropPointerDown} />
64 <CommandDialogViewport>
65 <CommandDialogPrimitive.Popup
66 className={cn(
67 DIALOG_POPUP_CLASS,
68 "pointer-events-auto max-h-105 max-w-xl text-foreground **:data-[slot=scroll-area-viewport]:data-has-overflow-y:pe-1",
69 className,
70 )}
71 data-slot="command-dialog-popup"
72 {...props}
73 >
74 {children}
75 </CommandDialogPrimitive.Popup>
76 </CommandDialogViewport>
77 </CommandDialogPortal>
78 );
80​
81function Command({
82 autoHighlight = "always",
83 keepHighlight = true,
84 ...props
85}: React.ComponentProps<typeof Autocomplete>) {
86 return (
87 <Autocomplete
88 autoHighlight={autoHighlight}
89 inline
90 keepHighlight={keepHighlight}
91 open
92 {...props}
93 />
94 );
96​
97function CommandInput({
98 className,
99 wrapperClassName,
100 placeholder,
101 ...props
102}: React.ComponentProps<typeof AutocompleteInput> & {
103 wrapperClassName?: string | undefined;
104}) {
105 return (
106 <div className={cn("px-2.5 py-1.5", wrapperClassName)}>
107 <AutocompleteInput
108 autoFocus
109 className={cn(
110 "border-transparent! bg-transparent! shadow-none before:hidden has-focus-visible:ring-0 placeholder:text-muted-foreground/80",
111 className,
112 )}
113 placeholder={placeholder}
114 size="lg"
115 startAddon={<SearchIcon />}
116 {...props}
117 />
118 </div>
119 );
121​
122function CommandList({ className, ...props }: React.ComponentProps<typeof AutocompleteList>) {
123 return (
124 <AutocompleteList
125 className={cn("not-empty:scroll-py-2 not-empty:p-2", className)}
126 data-slot="command-list"
127 {...props}
128 />
129 );
131​
132function CommandEmpty({ className, ...props }: React.ComponentProps<typeof AutocompleteEmpty>) {
133 return (
134 <AutocompleteEmpty
135 className={cn("not-empty:py-6", className)}
136 data-slot="command-empty"
137 {...props}
138 />
139 );
141​
142function CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
143 return (
144 <div
145 className={cn(
146 "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]",
147 className,
148 )}
149 {...props}
150 />
151 );
153​
154function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
155 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
157​
158function CommandGroupLabel({
159 className,
160 ...props
161}: React.ComponentProps<typeof AutocompleteGroupLabel>) {
162 return (
163 <AutocompleteGroupLabel className={className} data-slot="command-group-label" {...props} />
164 );
166​
167function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
168 return <AutocompleteCollection data-slot="command-collection" {...props} />;
170​
171function CommandItem({ className, ...props }: React.ComponentProps<typeof AutocompleteItem>) {
172 return (
173 <AutocompleteItem
174 className={cn(
175 "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",
176 className,
177 )}
178 data-slot="command-item"
179 {...props}
180 />
181 );
183​
184function CommandSeparator({
185 className,
186 ...props
187}: React.ComponentProps<typeof AutocompleteSeparator>) {
188 return (
189 <AutocompleteSeparator
190 className={cn("my-2", className)}
191 data-slot="command-separator"
192 {...props}
193 />
194 );
196​
197function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
198 return (
199 <kbd
200 className={cn(
201 "ms-auto font-medium font-sans text-muted-foreground text-xs tracking-widest",
202 className,
203 )}
204 data-slot="command-shortcut"
205 {...props}
206 />
207 );
209​
210function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
211 return (
212 <div
213 className={cn(
214 "relative flex items-center justify-between gap-2 rounded-b-[calc(var(--radius-2xl)-1px)] bg-foreground/[0.025] px-5 py-3 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",
215 className,
216 )}
217 data-slot="command-footer"
218 {...props}
219 />
220 );
222​
223export {
224 CommandCreateHandle,
225 Command,
226 CommandCollection,
227 CommandDialog,
228 CommandDialogPopup,
229 CommandDialogTrigger,
230 CommandEmpty,
231 CommandFooter,
232 CommandGroup,
233 CommandGroupLabel,
234 CommandInput,
235 CommandItem,
236 CommandList,
237 CommandPanel,
238 CommandSeparator,
239 CommandShortcut,
240};