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

CommandPaletteContent.tsx

93 lines3.0 KB

1import { ArrowDownIcon, ArrowUpIcon } from "lucide-react";
2import { type ComponentProps, type ReactNode, useEffect, useRef } from "react";
3​
4import { Command, CommandFooter, CommandInput, CommandPanel } from "./ui/command";
5import { Kbd, KbdGroup } from "./ui/kbd";
6​
7type CommandPaletteContentProps = Omit<ComponentProps<typeof Command>, "children"> & {
8 readonly children: ReactNode;
9 readonly escapeLabel?: ReactNode;
10 readonly footerActionLabel?: ReactNode;
11 readonly footerTrailing?: ReactNode;
12 readonly inputAccessory?: ReactNode;
13 readonly inputProps: ComponentProps<typeof CommandInput>;
14 readonly panelSize?: "list" | "tall-list" | "fill";
15 readonly showBackHint?: boolean;
16 readonly testId?: string;
17};
18​
19export function CommandPaletteContent({
20 children,
21 escapeLabel = "Close",
22 footerActionLabel,
23 footerTrailing,
24 inputAccessory,
25 inputProps,
26 panelSize = "list",
27 showBackHint,
28 testId,
29 ...commandProps
30}: CommandPaletteContentProps) {
31 const inputRef = useRef<HTMLInputElement>(null);
32​
33 // Direct-open flows replace the initial palette view after the dialog has
34 // already moved focus. Reclaim it when the replacement input mounts so
35 // typing cannot continue in the composer behind the modal. Defer until the
36 // dialog has recorded the previous focus target for restoration on close.
37 useEffect(() => {
38 const frame = requestAnimationFrame(() => inputRef.current?.focus());
39 return () => cancelAnimationFrame(frame);
40 }, []);
41​
42 return (
43 <div className="contents" data-testid={testId}>
44 <Command {...commandProps}>
45 <div className="relative">
46 <CommandInput autoFocus={false} {...inputProps} ref={inputRef} />
47 {inputAccessory}
48 </div>
49 <CommandPanel
50 className={
51 panelSize === "fill"
52 ? "flex min-h-0 flex-1 flex-col"
53 : panelSize === "tall-list"
54 ? "max-h-[min(34rem,76vh)]"
55 : "flex max-h-[min(28rem,70vh)] min-h-0 flex-col"
56 }
57 >
58 {children}
59 </CommandPanel>
60 <CommandFooter className="max-sm:flex-col max-sm:items-start">
61 <div className="flex items-center gap-3">
62 <KbdGroup>
63 <Kbd>
64 <ArrowUpIcon />
65 </Kbd>
66 <Kbd>
67 <ArrowDownIcon />
68 </Kbd>
69 <span>Navigate</span>
70 </KbdGroup>
71 {footerActionLabel !== undefined ? (
72 <KbdGroup>
73 <Kbd>Enter</Kbd>
74 <span>{footerActionLabel}</span>
75 </KbdGroup>
76 ) : null}
77 {showBackHint ? (
78 <KbdGroup>
79 <Kbd>Backspace</Kbd>
80 <span>Back</span>
81 </KbdGroup>
82 ) : null}
83 <KbdGroup>
84 <Kbd>Esc</Kbd>
85 <span>{escapeLabel}</span>
86 </KbdGroup>
87 </div>
88 {footerTrailing}
89 </CommandFooter>
90 </Command>
91 </div>
92 );