Open History to see the last commit
1import { ArrowDownIcon, ArrowUpIcon } from "lucide-react";
2import { type ComponentProps, type ReactNode } from "react";
3
4import { Command, CommandFooter, CommandInput, CommandPanel } from "./ui/command";
5import { Kbd, KbdGroup } from "./ui/kbd";
6
7/**
8 * A view replacing another inside an open dialog mounts with focus on the body,
9 * left there by the removed input. Claim it so typing cannot reach the composer
10 * behind the modal. On first open the dialog's own initial focus handles it.
11 */
12function claimFocusInOpenDialog(input: HTMLInputElement | null) {
13 if (!input) return;
14 const active = input.ownerDocument.activeElement;
15 if (
16 active === null ||
17 active === input.ownerDocument.body ||
18 input.closest('[role="dialog"]')?.contains(active)
19 ) {
20 input.focus();
21 }
22}
23
24type CommandPaletteContentProps = Omit<ComponentProps<typeof Command>, "children"> & {
25 readonly children: ReactNode;
26 readonly escapeLabel?: ReactNode;
27 readonly footerActionLabel?: ReactNode;
28 readonly footerTrailing?: ReactNode;
29 readonly inputAccessory?: ReactNode;
30 readonly inputProps: ComponentProps<typeof CommandInput>;
31 readonly panelSize?: "list" | "tall-list" | "fill";
32 readonly showBackHint?: boolean;
33 readonly testId?: string;
34};
35
36export function CommandPaletteContent({
37 children,
38 escapeLabel = "Close",
39 footerActionLabel,
40 footerTrailing,
41 inputAccessory,
42 inputProps,
43 panelSize = "list",
44 showBackHint,
45 testId,
46 ...commandProps
47}: CommandPaletteContentProps) {
48 return (
49 <div className="contents" data-testid={testId}>
50 <Command {...commandProps}>
51 <div className="relative">
52 <CommandInput autoFocus={false} {...inputProps} ref={claimFocusInOpenDialog} />
53 {inputAccessory}
54 </div>
55 <CommandPanel
56 className={
57 panelSize === "fill"
58 ? "flex min-h-0 flex-1 flex-col"
59 : panelSize === "tall-list"
60 ? "max-h-[min(34rem,76vh)]"
61 : "flex max-h-[min(28rem,70vh)] min-h-0 flex-col"
62 }
63 >
64 {children}
65 </CommandPanel>
66 <CommandFooter className="max-sm:flex-col max-sm:items-start">
67 <div className="flex items-center gap-3">
68 <KbdGroup>
69 <Kbd>
70 <ArrowUpIcon />
71 </Kbd>
72 <Kbd>
73 <ArrowDownIcon />
74 </Kbd>
75 <span>Navigate</span>
76 </KbdGroup>
77 {footerActionLabel !== undefined ? (
78 <KbdGroup>
79 <Kbd>Enter</Kbd>
80 <span>{footerActionLabel}</span>
81 </KbdGroup>
82 ) : null}
83 {showBackHint ? (
84 <KbdGroup>
85 <Kbd>Backspace</Kbd>
86 <span>Back</span>
87 </KbdGroup>
88 ) : null}
89 <KbdGroup>
90 <Kbd>Esc</Kbd>
91 <span>{escapeLabel}</span>
92 </KbdGroup>
93 </div>
94 {footerTrailing}
95 </CommandFooter>
96 </Command>
97 </div>
98 );
99}