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