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

CommandPaletteContent.tsx

128 lines4.1 KB

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