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

command.tsx

234 lines7.2 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";
18​
19const CommandDialog = CommandDialogPrimitive.Root;
20​
21const CommandDialogPortal = CommandDialogPrimitive.Portal;
22​
23const CommandCreateHandle = CommandDialogPrimitive.createHandle;
24​
25function CommandDialogTrigger(props: CommandDialogPrimitive.Trigger.Props) {
26 return <CommandDialogPrimitive.Trigger data-slot="command-dialog-trigger" {...props} />;
28​
29function CommandDialogBackdrop({ className, ...props }: CommandDialogPrimitive.Backdrop.Props) {
30 return (
31 <CommandDialogPrimitive.Backdrop
32 className={cn(
33 "fixed inset-0 z-50 bg-background/60 backdrop-blur-xs transition-all duration-200 data-ending-style:opacity-0 data-starting-style:opacity-0",
34 className,
35 )}
36 data-slot="command-dialog-backdrop"
37 {...props}
38 />
39 );
41​
42function CommandDialogViewport({ className, ...props }: CommandDialogPrimitive.Viewport.Props) {
43 return (
44 <CommandDialogPrimitive.Viewport
45 className={cn(
46 "pointer-events-none fixed inset-0 z-50 flex flex-col items-center px-4 py-[max(--spacing(4),4vh)] sm:py-[10vh]",
47 className,
48 )}
49 data-slot="command-dialog-viewport"
50 {...props}
51 />
52 );
54​
55function CommandDialogPopup({
56 className,
57 children,
58 onBackdropPointerDown,
59 ...props
60}: CommandDialogPrimitive.Popup.Props & {
61 onBackdropPointerDown?: React.PointerEventHandler<HTMLDivElement>;
62}) {
63 return (
64 <CommandDialogPortal>
65 <CommandDialogBackdrop onPointerDown={onBackdropPointerDown} />
66 <CommandDialogViewport>
67 <CommandDialogPrimitive.Popup
68 className={cn(
69 "pointer-events-auto -translate-y-[calc(1.25rem*var(--nested-dialogs))] relative row-start-2 flex max-h-105 min-h-0 w-full min-w-0 max-w-xl scale-[calc(1-0.1*var(--nested-dialogs))] flex-col rounded-2xl border bg-popover not-dark:bg-clip-padding text-popover-foreground opacity-[calc(1-0.1*var(--nested-dialogs))] shadow-lg/5 outline-none transition-[scale,opacity,translate] duration-200 ease-in-out will-change-transform before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-2xl)-1px)] before:bg-muted/72 before:shadow-[0_1px_--theme(--color-black/4%)] data-nested:data-ending-style:translate-y-8 data-nested:data-starting-style:translate-y-8 data-nested-dialog-open:origin-top data-ending-style:scale-98 data-starting-style:scale-98 data-ending-style:opacity-0 data-starting-style:opacity-0 **:data-[slot=scroll-area-viewport]:data-has-overflow-y:pe-1 dark:before:shadow-[0_-1px_--theme(--color-white/6%)]",
70 className,
71 )}
72 data-slot="command-dialog-popup"
73 {...props}
74 >
75 {children}
76 </CommandDialogPrimitive.Popup>
77 </CommandDialogViewport>
78 </CommandDialogPortal>
79 );
81​
82function Command({
83 autoHighlight = "always",
84 keepHighlight = true,
85 ...props
86}: React.ComponentProps<typeof Autocomplete>) {
87 return (
88 <Autocomplete
89 autoHighlight={autoHighlight}
90 inline
91 keepHighlight={keepHighlight}
92 open
93 {...props}
94 />
95 );
97​
98function CommandInput({
99 className,
100 wrapperClassName,
101 placeholder,
102 ...props
103}: React.ComponentProps<typeof AutocompleteInput> & {
104 wrapperClassName?: string | undefined;
105}) {
106 return (
107 <div className={cn("px-2.5 py-1.5", wrapperClassName)}>
108 <AutocompleteInput
109 autoFocus
110 className={cn(
111 "border-transparent! bg-transparent! shadow-none before:hidden has-focus-visible:ring-0",
112 className,
113 )}
114 placeholder={placeholder}
115 size="lg"
116 startAddon={<SearchIcon />}
117 {...props}
118 />
119 </div>
120 );
122​
123function CommandList({ className, ...props }: React.ComponentProps<typeof AutocompleteList>) {
124 return (
125 <AutocompleteList
126 className={cn("not-empty:scroll-py-2 not-empty:p-2", className)}
127 data-slot="command-list"
128 {...props}
129 />
130 );
132​
133function CommandEmpty({ className, ...props }: React.ComponentProps<typeof AutocompleteEmpty>) {
134 return (
135 <AutocompleteEmpty
136 className={cn("not-empty:py-6", className)}
137 data-slot="command-empty"
138 {...props}
139 />
140 );
142​
143function CommandPanel({ className, ...props }: React.ComponentProps<"div">) {
144 return (
145 <div
146 className={cn(
147 "-mx-px not-has-[+[data-slot=command-footer]]:-mb-px relative min-h-0 overflow-hidden rounded-t-xl not-has-[+[data-slot=command-footer]]:rounded-b-2xl border border-b-0 bg-popover bg-clip-padding shadow-xs/5 [clip-path:inset(0_1px)] not-has-[+[data-slot=command-footer]]:[clip-path:inset(0_1px_1px_1px_round_0_0_calc(var(--radius-2xl)-1px)_calc(var(--radius-2xl)-1px))] before:pointer-events-none before:absolute before:inset-0 before:rounded-t-[calc(var(--radius-xl)-1px)] **:data-[slot=scroll-area-scrollbar]:mt-2 [touch-action:pan-y]",
148 className,
149 )}
150 {...props}
151 />
152 );
154​
155function CommandGroup({ className, ...props }: React.ComponentProps<typeof AutocompleteGroup>) {
156 return <AutocompleteGroup className={className} data-slot="command-group" {...props} />;
158​
159function CommandGroupLabel({
160 className,
161 ...props
162}: React.ComponentProps<typeof AutocompleteGroupLabel>) {
163 return (
164 <AutocompleteGroupLabel className={className} data-slot="command-group-label" {...props} />
165 );
167​
168function CommandCollection({ ...props }: React.ComponentProps<typeof AutocompleteCollection>) {
169 return <AutocompleteCollection data-slot="command-collection" {...props} />;
171​
172function CommandItem({ className, ...props }: React.ComponentProps<typeof AutocompleteItem>) {
173 return (
174 <AutocompleteItem className={cn("py-1.5", className)} data-slot="command-item" {...props} />
175 );
177​
178function CommandSeparator({
179 className,
180 ...props
181}: React.ComponentProps<typeof AutocompleteSeparator>) {
182 return (
183 <AutocompleteSeparator
184 className={cn("my-2", className)}
185 data-slot="command-separator"
186 {...props}
187 />
188 );
190​
191function CommandShortcut({ className, ...props }: React.ComponentProps<"kbd">) {
192 return (
193 <kbd
194 className={cn(
195 "ms-auto font-medium font-sans text-muted-foreground/72 text-xs tracking-widest",
196 className,
197 )}
198 data-slot="command-shortcut"
199 {...props}
200 />
201 );
203​
204function CommandFooter({ className, ...props }: React.ComponentProps<"div">) {
205 return (
206 <div
207 className={cn(
208 "flex items-center justify-between gap-2 rounded-b-[calc(var(--radius-2xl)-1px)] border-t px-5 py-3 text-muted-foreground text-xs",
209 className,
210 )}
211 data-slot="command-footer"
212 {...props}
213 />
214 );
216​
217export {
218 CommandCreateHandle,
219 Command,
220 CommandCollection,
221 CommandDialog,
222 CommandDialogPopup,
223 CommandDialogTrigger,
224 CommandEmpty,
225 CommandFooter,
226 CommandGroup,
227 CommandGroupLabel,
228 CommandInput,
229 CommandItem,
230 CommandList,
231 CommandPanel,
232 CommandSeparator,
233 CommandShortcut,
234};