The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 03:15 UTC
fix(web): keep the folder path caret in place while editing (#31642)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: shivam <91240327+shivamhwp@users.noreply.github.com>
Julius Marminge committed
Nov 5, 2026 at 16:19 UTC
fb9f1c6f336d8119b0ac8799a5d1a845d19fbb2b
3 files changed
+68
-8
apps/web/src/components/CommandPalette.composition.browser.test.tsx
+14
@@ -133,3 +133,17 @@ describe.each(["repository", "folder", "destination"] as const)("add-project %s"
133
}
134
});
135
});
136
+
137
+it("keeps the caret in place when an edit briefly makes the folder path not a path", async () => {
138
+ await using _screen = await render(<CommandPalette>{null}</CommandPalette>);
139
+ await act(() => openCommandPalette({ open: "add-project" }));
140
+ await page.getByText("Local folder", { exact: true }).click();
141
+ const input = () => page.getByRole("combobox");
142
+ await input().fill("/workspace/project");
143
+ await userEvent.keyboard("{Home}{ArrowRight}{Backspace}");
144
+ await expect.element(input()).toHaveValue("workspace/project");
145
+ await userEvent.keyboard("/");
146
+ await expect.element(input()).toHaveValue("/workspace/project");
147
+ await userEvent.keyboard("w");
148
+ await expect.element(input()).toHaveValue("/wworkspace/project");
149
+});
apps/web/src/components/CommandPalette.tsx
+23
-4
@@ -149,6 +149,7 @@ import {
149
hasTrailingPathSeparator,
150
inferProjectTitleFromPath,
151
isExplicitRelativeProjectPath,
152
+ isFilesystemBrowseQuery,
153
isScratchProject,
154
isUnsupportedWindowsProjectPath,
155
resolveProjectPathForDispatch,
@@ -215,7 +216,7 @@ import {
216
} from "./CommandPalette.logic";
217
import { resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";
218
import { isSidebarHiddenThread } from "./Sidebar.logic";
218
-import { CommandPaletteContent } from "./CommandPaletteContent";
219
+import { CommandPaletteContent, type PendingInputSelection } from "./CommandPaletteContent";
220
import {
221
selectCommandPaletteClientSettings,
222
useEnvironmentLookups,
@@ -429,6 +430,8 @@ function OpenCommandPaletteDialog(props: {
430
const resultListRef = useRef<LegendListRef | null>(null);
431
// A pushed view drops the typed highlight until the user moves it again.
432
const highlightClearedRef = useRef(viewStack.length > 0);
433
+ // Entering or leaving browse mode remounts the input; carry the caret across it.
434
+ const remountSelectionRef = useRef<PendingInputSelection | null>(null);
435
function clearTypedHighlight(): void {
436
highlightClearedRef.current = true;
437
setHighlightedItemValue(null);
@@ -923,10 +926,25 @@ function OpenCommandPaletteDialog(props: {
926
setHighlightedItemValue(null);
927
}
928
926
- function handleQueryChange(nextQuery: string): void {
929
+ function handleQueryChange(nextQuery: string, source: EventTarget | null): void {
930
browseNavigation.invalidate();
931
clearTypedHighlight();
929
- setQuery(canBrowsePaths ? resolvePastedPath(query, nextQuery) : nextQuery);
932
+ const resolvedQuery = canBrowsePaths ? resolvePastedPath(query, nextQuery) : nextQuery;
933
+ // A rewritten paste has no matching offsets, so its caret lands at the end.
934
+ if (
935
+ source instanceof HTMLInputElement &&
936
+ resolvedQuery === nextQuery &&
937
+ (canBrowsePaths && isFilesystemBrowseQuery(resolvedQuery, browseEnvironmentPlatform)) !==
938
+ isBrowsing
939
+ ) {
940
+ remountSelectionRef.current = {
941
+ value: resolvedQuery,
942
+ start: source.selectionStart,
943
+ end: source.selectionEnd,
944
+ direction: source.selectionDirection,
945
+ };
946
+ }
947
+ setQuery(resolvedQuery);
948
if (nextQuery === "" && currentView?.initialQuery) {
949
popView();
950
}
@@ -2861,7 +2879,8 @@ function OpenCommandPaletteDialog(props: {
2879
scrollCommandPaletteRowIntoView(resultListRef.current, rowIndex);
2880
}
2881
}}
2864
- onValueChange={handleQueryChange}
2882
+ onValueChange={(nextQuery, { event }) => handleQueryChange(nextQuery, event.target)}
2883
+ pendingSelectionRef={remountSelectionRef}
2884
showBackHint={isSubmenu}
2885
value={query}
2886
>
apps/web/src/components/CommandPaletteContent.tsx
+31
-4
@@ -1,5 +1,5 @@
1
import { ArrowDownIcon, ArrowUpIcon } from "lucide-react";
2
-import { type ComponentProps, type ReactNode } from "react";
2
+import { type ComponentProps, type ReactNode, type RefObject } from "react";
3
4
import { Command, CommandFooter, CommandInput, CommandPanel } from "./ui/command";
5
import { Kbd, KbdGroup } from "./ui/kbd";
@@ -9,8 +9,7 @@ import { Kbd, KbdGroup } from "./ui/kbd";
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
*/
12
-function claimFocusInOpenDialog(input: HTMLInputElement | null) {
13
- if (!input) return;
12
+function claimFocusInOpenDialog(input: HTMLInputElement) {
13
const active = input.ownerDocument.activeElement;
14
if (
15
active === null ||
@@ -21,6 +20,24 @@ function claimFocusInOpenDialog(input: HTMLInputElement | null) {
20
}
21
}
22
23
+export interface PendingInputSelection {
24
+ readonly value: string;
25
+ readonly start: number | null;
26
+ readonly end: number | null;
27
+ readonly direction: "forward" | "backward" | "none" | null;
28
+}
29
+
30
+function 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);
39
+}
40
+
41
type CommandPaletteContentProps = Omit<ComponentProps<typeof Command>, "children"> & {
42
readonly children: ReactNode;
43
readonly escapeLabel?: ReactNode;
@@ -28,6 +45,7 @@ type CommandPaletteContentProps = Omit<ComponentProps<typeof Command>, "children
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;
@@ -40,6 +58,7 @@ export function CommandPaletteContent({
58
footerTrailing,
59
inputAccessory,
60
inputProps,
61
+ pendingSelectionRef,
62
panelSize = "list",
63
showBackHint,
64
testId,
@@ -49,7 +68,15 @@ export function CommandPaletteContent({
68
<div className="contents" data-testid={testId}>
69
<Command {...commandProps}>
70
<div className="relative">
52
- <CommandInput autoFocus={false} {...inputProps} ref={claimFocusInOpenDialog} />
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