Open History to see the last commit
1import { ArrowDownIcon, ArrowUpIcon } from "lucide-react";
2import { type ComponentProps, type ReactNode, useLayoutEffect, 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.
36 useLayoutEffect(() => {
37 inputRef.current?.focus();
38 }, []);
39
40 return (
41 <div className="contents" data-testid={testId}>
42 <Command {...commandProps}>
43 <div className="relative">
44 <CommandInput {...inputProps} ref={inputRef} />
45 {inputAccessory}
46 </div>
47 <CommandPanel
48 className={
49 panelSize === "fill"
50 ? "flex min-h-0 flex-1 flex-col"
51 : panelSize === "tall-list"
52 ? "max-h-[min(34rem,76vh)]"
53 : "max-h-[min(28rem,70vh)]"
54 }
55 >
56 {children}
57 </CommandPanel>
58 <CommandFooter className="max-sm:flex-col max-sm:items-start">
59 <div className="flex items-center gap-3">
60 <KbdGroup>
61 <Kbd>
62 <ArrowUpIcon />
63 </Kbd>
64 <Kbd>
65 <ArrowDownIcon />
66 </Kbd>
67 <span>Navigate</span>
68 </KbdGroup>
69 {footerActionLabel !== undefined ? (
70 <KbdGroup>
71 <Kbd>Enter</Kbd>
72 <span>{footerActionLabel}</span>
73 </KbdGroup>
74 ) : null}
75 {showBackHint ? (
76 <KbdGroup>
77 <Kbd>Backspace</Kbd>
78 <span>Back</span>
79 </KbdGroup>
80 ) : null}
81 <KbdGroup>
82 <Kbd>Esc</Kbd>
83 <span>{escapeLabel}</span>
84 </KbdGroup>
85 </div>
86 {footerTrailing}
87 </CommandFooter>
88 </Command>
89 </div>
90 );
91}