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 panelClassName?: string;
15 readonly showBackHint?: boolean;
16 readonly testId?: string;
17};
18
19/**
20 * Shared command palette chrome. Palette modes provide their query behavior,
21 * results, and optional input accessory while retaining one input, panel, and
22 * keyboard-hint gutter.
23 */
24export function CommandPaletteContent({
25 children,
26 escapeLabel = "Close",
27 footerActionLabel,
28 footerTrailing,
29 inputAccessory,
30 inputProps,
31 panelClassName,
32 showBackHint,
33 testId,
34 ...commandProps
35}: CommandPaletteContentProps) {
36 const inputRef = useRef<HTMLInputElement>(null);
37
38 // Direct-open flows replace the initial palette view after the dialog has
39 // already moved focus. Reclaim it when the replacement input mounts so
40 // typing cannot continue in the composer behind the modal.
41 useLayoutEffect(() => {
42 inputRef.current?.focus();
43 }, []);
44
45 return (
46 <div className="contents" data-testid={testId}>
47 <Command {...commandProps}>
48 <div className="relative">
49 <CommandInput {...inputProps} ref={inputRef} />
50 {inputAccessory}
51 </div>
52 <CommandPanel className={panelClassName}>{children}</CommandPanel>
53 <CommandFooter className="gap-3 max-sm:flex-col max-sm:items-start">
54 <div className="flex items-center gap-3">
55 <KbdGroup className="items-center gap-1.5">
56 <Kbd>
57 <ArrowUpIcon />
58 </Kbd>
59 <Kbd>
60 <ArrowDownIcon />
61 </Kbd>
62 <span>Navigate</span>
63 </KbdGroup>
64 {footerActionLabel !== undefined ? (
65 <KbdGroup className="items-center gap-1.5">
66 <Kbd>Enter</Kbd>
67 <span>{footerActionLabel}</span>
68 </KbdGroup>
69 ) : null}
70 {showBackHint ? (
71 <KbdGroup className="items-center gap-1.5">
72 <Kbd>Backspace</Kbd>
73 <span>Back</span>
74 </KbdGroup>
75 ) : null}
76 <KbdGroup className="items-center gap-1.5">
77 <Kbd>Esc</Kbd>
78 <span>{escapeLabel}</span>
79 </KbdGroup>
80 </div>
81 {footerTrailing}
82 </CommandFooter>
83 </Command>
84 </div>
85 );
86}