The best way to control Claude Code, Codex and more from any computer or phone
fix(web): preserve focus and prioritize picker shortcuts (#9795)
maria committed
Sep 4, 2026 at 21:11 UTC
896fe82f2f191eb8ac0fd620e19c25dd48253592
7 files changed
+52
-34
apps/web/src/components/CommandPalette.tsx
+7
-4
@@ -508,7 +508,10 @@ export function CommandPalette({ children }: { children: ReactNode }) {
508
setOpen(open);
509
}}
510
>
511
- {children}
511
+ {/* Block background focus calls for the entire time the palette is open. */}
512
+ <div className="contents" inert={state.open}>
513
+ {children}
514
+ </div>
515
<CommandPaletteDialog
516
mode={state.mode}
517
openIntent={state.openIntent}
@@ -2289,15 +2292,15 @@ function OpenCommandPaletteDialog(props: {
2292
context: { modelPickerOpen: false },
2293
});
2294
if (threadJumpIndexFromCommand(command ?? "") !== null) {
2295
+ event.preventDefault();
2296
+ event.stopPropagation();
2297
const matchingItem = displayedGroups
2298
.flatMap((group) => group.items)
2299
.find((item) => item.shortcutCommand === command);
2300
if (matchingItem) {
2296
- event.preventDefault();
2297
- event.stopPropagation();
2301
executeItem(matchingItem);
2299
- return;
2302
}
2303
+ return;
2304
}
2305
if (command === "thread.copyReference" && activeThreadReferenceCopyTarget !== null) {
2306
event.preventDefault();
apps/web/src/components/CommandPaletteContent.tsx
+11
-2
@@ -1,5 +1,5 @@
1
import { ArrowDownIcon, ArrowUpIcon } from "lucide-react";
2
-import type { ComponentProps, ReactNode } from "react";
2
+import { type ComponentProps, type ReactNode, useLayoutEffect, useRef } from "react";
3
4
import { Command, CommandFooter, CommandInput, CommandPanel } from "./ui/command";
5
import { Kbd, KbdGroup } from "./ui/kbd";
@@ -33,11 +33,20 @@ export function CommandPaletteContent({
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">
40
- <CommandInput {...inputProps} />
49
+ <CommandInput {...inputProps} ref={inputRef} />
50
{inputAccessory}
51
</div>
52
<CommandPanel className={panelClassName}>{children}</CommandPanel>
apps/web/src/components/LegacySidebar.tsx
+2
-2
@@ -172,7 +172,7 @@ import {
172
useSidebar,
173
} from "./ui/sidebar";
174
import { useThreadSelectionStore } from "../threadSelectionStore";
175
-import { openCommandPalette } from "../commandPaletteBus";
175
+import { isCommandPaletteOpen, openCommandPalette } from "../commandPaletteBus";
176
import {
177
archiveSelectedThreadEntries,
178
buildMultiSelectThreadContextMenuItems,
@@ -3510,7 +3510,7 @@ export default function LegacySidebar() {
3510
const onWindowKeyDown = (event: globalThis.KeyboardEvent) => {
3511
const shortcutContext = getCurrentSidebarShortcutContext();
3512
3513
- if (event.defaultPrevented || event.repeat) {
3513
+ if (event.defaultPrevented || event.repeat || isCommandPaletteOpen() || isModelPickerOpen()) {
3514
return;
3515
}
3516
apps/web/src/components/Sidebar.tsx
+4
-2
@@ -104,7 +104,7 @@ import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore"
104
import { useThreadSelectionStore } from "../threadSelectionStore";
105
import { useThreadActions } from "../hooks/useThreadActions";
106
import { useHandleNewThread } from "../hooks/useHandleNewThread";
107
-import { openCommandPalette } from "../commandPaletteBus";
107
+import { isCommandPaletteOpen, openCommandPalette } from "../commandPaletteBus";
108
import { startNewThreadFromContext } from "../lib/chatThreadActions";
109
import { useClientSettings } from "../hooks/useSettings";
110
import { useCopyToClipboard } from "../hooks/useCopyToClipboard";
@@ -3505,7 +3505,9 @@ export default function Sidebar() {
3505
);
3506
useEffect(() => {
3507
const onWindowKeyDown = (event: KeyboardEvent) => {
3508
- if (event.defaultPrevented || event.repeat) return;
3508
+ if (event.defaultPrevented || event.repeat || isCommandPaletteOpen() || isModelPickerOpen()) {
3509
+ return;
3510
+ }
3511
const command = resolveShortcutCommand(event, keybindings, {
3512
platform: navigator.platform,
3513
context: {
apps/web/src/components/ThreadTerminalDrawer.tsx
+11
-17
@@ -516,7 +516,10 @@ export function TerminalViewport({
516
// never started, so only "exited" triggers the message — as with xterm.)
517
synchronizedStatusRef.current = "closed";
518
synchronizeTerminalStatus(terminal, latestSession.status);
519
- if (autoFocus && visibleRef.current) window.requestAnimationFrame(() => terminal.focus());
519
+ // Startup may finish after the user has returned to the composer.
520
+ if (visibleRef.current && mount.contains(document.activeElement)) {
521
+ terminal.focus();
522
+ }
523
524
const dismissSelectionAction = (supersede = false) => {
525
const ownsMenu =
@@ -870,10 +873,10 @@ export function TerminalViewport({
873
874
return () => {
875
cancelled = true;
876
+ const hadFocus = mount.contains(document.activeElement);
877
teardown?.();
878
+ if (hadFocus && mount.isConnected) mount.focus({ preventScroll: true });
879
};
875
- // autoFocus is intentionally omitted;
876
- // it is only read at mount time and must not trigger terminal teardown/recreation.
880
}, [cwd, environmentId, runtimeEnvKey, terminalId, threadId, worktreePath]);
881
882
useEffect(() => {
@@ -904,24 +907,14 @@ export function TerminalViewport({
907
writeSystemMessage(terminal, current.error);
908
}
909
907
- if (previous.version === 0 && autoFocus && visibleRef.current) {
908
- window.requestAnimationFrame(() => {
909
- terminal.focus();
910
- });
911
- }
910
previousSessionRef.current = current;
913
- }, [autoFocus, terminalOutput, terminalError, terminalStatus, terminalVersion]);
911
+ }, [terminalOutput, terminalError, terminalStatus, terminalVersion]);
912
913
useEffect(() => {
914
if (!autoFocus || !visible) return;
917
- const terminal = terminalRef.current;
918
- if (!terminal) return;
919
- const frame = window.requestAnimationFrame(() => {
920
- terminal.focus();
921
- });
922
- return () => {
923
- window.cancelAnimationFrame(frame);
924
- };
915
+ // Claim focus when requested, then hand it to the terminal once ready only
916
+ // if the user has not focused something else in the meantime.
917
+ (terminalRef.current ?? containerRef.current)?.focus();
918
}, [autoFocus, focusRequestId, visible]);
919
920
useEffect(() => {
@@ -944,6 +937,7 @@ export function TerminalViewport({
937
return (
938
<div
939
ref={containerRef}
940
+ tabIndex={-1}
941
className="relative h-full w-full overflow-hidden bg-[var(--terminal-background)]"
942
/>
943
);
apps/web/src/components/chat/ModelPickerContent.tsx
+8
-7
@@ -5,6 +5,7 @@ import {
5
type ResolvedKeybindingsConfig,
6
} from "@t3tools/contracts";
7
import { resolveSelectableModel } from "@t3tools/shared/model";
8
+import { useAtomValue } from "@effect/atom-react";
9
import { LegendList, type LegendListRef } from "@legendapp/list/react";
10
import { memo, useMemo, useState, useCallback, useEffect, useLayoutEffect, useRef } from "react";
11
import { ChevronRightIcon, SearchIcon } from "lucide-react";
@@ -26,6 +27,8 @@ import {
27
ComboboxListVirtualized,
28
} from "../ui/combobox";
29
import { ModelEsque } from "./providerIconUtils";
30
+import { isCommandPaletteOpen } from "../../commandPaletteBus";
31
+import { primaryServerKeybindingsAtom } from "../../state/server";
32
import {
33
modelPickerJumpCommandForIndex,
34
modelPickerJumpIndexFromCommand,
@@ -217,10 +220,8 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
220
: [],
221
),
222
);
220
- const keybindings = useMemo<ResolvedKeybindingsConfig>(
221
- () => providedKeybindings ?? [],
222
- [providedKeybindings],
223
- );
223
+ const serverKeybindings = useAtomValue(primaryServerKeybindingsAtom);
224
+ const keybindings = providedKeybindings ?? serverKeybindings;
225
const updateSettings = useUpdateClientSettings();
226
227
const focusSearchInput = useCallback(() => {
@@ -678,7 +679,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
679
680
useEffect(() => {
681
const onWindowKeyDown = (event: globalThis.KeyboardEvent) => {
681
- if (event.defaultPrevented || event.repeat) {
682
+ if (event.defaultPrevented || event.repeat || isCommandPaletteOpen()) {
683
return;
684
}
685
@@ -690,6 +691,8 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
691
if (jumpIndex === null) {
692
return;
693
}
694
+ event.preventDefault();
695
+ event.stopPropagation();
696
697
const targetModelKey = modelJumpModelKeys[jumpIndex];
698
if (!targetModelKey) {
@@ -699,8 +702,6 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
702
if (!model) {
703
return;
704
}
702
- event.preventDefault();
703
- event.stopPropagation();
705
handleModelSelect(model.slug, model.instanceId);
706
};
707
docs/user/keyboard-focus.md
new
+9
@@ -0,0 +1,9 @@
1
+# Keyboard focus
2
+
3
+The command palette keeps focus while open. Closing it returns focus to the composer.
4
+While the palette or model picker is open, number shortcuts select its entries instead of
5
+switching threads. Model shortcuts work in Settings as well as the composer.
6
+See [Keybindings](./keybindings.md) to customize these shortcuts.
7
+
8
+If you return to typing while a terminal is starting, the composer keeps focus when the terminal
9
+becomes ready. Opening or switching to a terminal explicitly still focuses it.