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

CommandPaletteContent.tsx

126 lines3.9 KB

1import { ArrowDownIcon, ArrowUpIcon } from "lucide-react";
2import { type ComponentProps, type ReactNode, type RefObject } 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) {
13 const active = input.ownerDocument.activeElement;
14 if (
15 active === null ||
16 active === input.ownerDocument.body ||
17 input.closest('[role="dialog"]')?.contains(active)
18 ) {
19 input.focus();
20 }
22​
23export interface PendingInputSelection {
24 readonly value: string;
25 readonly start: number | null;
26 readonly end: number | null;
27 readonly direction: "forward" | "backward" | "none" | null;
29​
30function restorePendingSelection(
31 input: HTMLInputElement,
32 pendingSelectionRef: RefObject<PendingInputSelection | null> | undefined,
33) {
34 const selection = pendingSelectionRef?.current;
35 if (!pendingSelectionRef || !selection) return;
36 pendingSelectionRef.current = null;
37 if (input.value !== selection.value) return;
38 input.setSelectionRange(selection.start, selection.end, selection.direction ?? undefined);
40​
41type CommandPaletteContentProps = Omit<ComponentProps<typeof Command>, "children"> & {
42 readonly children: ReactNode;
43 readonly escapeLabel?: ReactNode;
44 readonly footerActionLabel?: ReactNode;
45 readonly footerTrailing?: ReactNode;
46 readonly inputAccessory?: ReactNode;
47 readonly inputProps: ComponentProps<typeof CommandInput>;
48 readonly pendingSelectionRef?: RefObject<PendingInputSelection | null>;
49 readonly panelSize?: "list" | "tall-list" | "fill";
50 readonly showBackHint?: boolean;
51 readonly testId?: string;
52};
53​
54export function CommandPaletteContent({
55 children,
56 escapeLabel = "Close",
57 footerActionLabel,
58 footerTrailing,
59 inputAccessory,
60 inputProps,
61 pendingSelectionRef,
62 panelSize = "list",
63 showBackHint,
64 testId,
65 ...commandProps
66}: CommandPaletteContentProps) {
67 return (
68 <div className="contents" data-testid={testId}>
69 <Command {...commandProps}>
70 <div className="relative">
71 <CommandInput
72 autoFocus={false}
73 {...inputProps}
74 ref={(input) => {
75 if (!input) return;
76 restorePendingSelection(input, pendingSelectionRef);
77 claimFocusInOpenDialog(input);
78 }}
79 />
80 {inputAccessory}
81 </div>
82 <CommandPanel
83 className={
84 panelSize === "fill"
85 ? "flex min-h-0 flex-1 flex-col"
86 : panelSize === "tall-list"
87 ? "max-h-[min(34rem,76vh)]"
88 : "flex max-h-[min(28rem,70vh)] min-h-0 flex-col"
89 }
90 >
91 {children}
92 </CommandPanel>
93 <CommandFooter className="max-sm:flex-col max-sm:items-start">
94 <div className="flex min-w-0 flex-wrap items-center gap-x-3 gap-y-1.5">
95 <KbdGroup>
96 <Kbd>
97 <ArrowUpIcon />
98 </Kbd>
99 <Kbd>
100 <ArrowDownIcon />
101 </Kbd>
102 <span>Navigate</span>
103 </KbdGroup>
104 {footerActionLabel !== undefined ? (
105 <KbdGroup>
106 <Kbd>Enter</Kbd>
107 <span>{footerActionLabel}</span>
108 </KbdGroup>
109 ) : null}
110 {showBackHint ? (
111 <KbdGroup>
112 <Kbd>Backspace</Kbd>
113 <span>Back</span>
114 </KbdGroup>
115 ) : null}
116 <KbdGroup>
117 <Kbd>Esc</Kbd>
118 <span>{escapeLabel}</span>
119 </KbdGroup>
120 </div>
121 {footerTrailing}
122 </CommandFooter>
123 </Command>
124 </div>
125 );