From 829af7b73a960d390a0b32becd37ad6483627aa4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 22 Sep 2026 22:52:25 -0700 Subject: [PATCH] feat(web): navigate back and forward with mod+[ and mod+] (#13212) Co-authored-by: Claude Opus 5.5 --- apps/web/src/components/AppSidebarLayout.tsx | 60 +++++++++++++++++++- apps/web/src/keybindings.test.ts | 19 +++++++ docs/user/keybindings.md | 3 + packages/contracts/src/keybindings.ts | 2 + packages/shared/src/keybindings.ts | 2 + 5 files changed, 85 insertions(+), 1 deletion(-) diff --git a/apps/web/src/components/AppSidebarLayout.tsx b/apps/web/src/components/AppSidebarLayout.tsx index 1b67af90ef..eccf3ef213 100644 --- a/apps/web/src/components/AppSidebarLayout.tsx +++ b/apps/web/src/components/AppSidebarLayout.tsx @@ -7,7 +7,7 @@ import { type CSSProperties, type ReactNode, } from "react"; -import { useLocation, useNavigate } from "@tanstack/react-router"; +import { useLocation, useNavigate, useParams } from "@tanstack/react-router"; import { isElectron } from "../env"; import { getLocalStorageItem, removeLocalStorageItem } from "../hooks/useLocalStorage"; @@ -16,6 +16,13 @@ import { resolveShortcutCommand, shortcutLabelForCommand, } from "../keybindings"; +import { isEditableFocused } from "../lib/editableFocus"; +import { isPreviewFocused } from "../lib/previewFocus"; +import { isTerminalFocused } from "../lib/terminalFocus"; +import { isModelPickerOpen } from "../modelPickerVisibility"; +import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore"; +import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; +import { resolveThreadRouteRef } from "../threadRoutes"; import { cn, isMacPlatform } from "../lib/utils"; import { primaryServerKeybindingsAtom } from "../state/server"; import { useEnvironmentIdentificationMode, useLegacySidebarEnabled } from "../hooks/useSettings"; @@ -147,6 +154,56 @@ function SidebarControl() { ); } +// Moves through the app's route history like a browser's back/forward buttons. +function NavigationHistoryShortcuts() { + const keybindings = useAtomValue(primaryServerKeybindingsAtom); + const routeThreadRef = useParams({ + strict: false, + select: (params) => resolveThreadRouteRef(params), + }); + + useEffect(() => { + const onKeyDown = (event: KeyboardEvent) => { + if (event.defaultPrevented) return; + if ( + event.target instanceof HTMLElement && + event.target.closest("[data-keybinding-capture]") + ) { + return; + } + const command = resolveShortcutCommand(event, keybindings, { + context: { + terminalFocus: isTerminalFocused(), + terminalOpen: routeThreadRef + ? selectThreadTerminalUiState( + useTerminalUiStateStore.getState().terminalUiStateByThreadKey, + routeThreadRef, + ).terminalOpen + : false, + previewFocus: isPreviewFocused(), + previewOpen: routeThreadRef + ? selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, routeThreadRef) === + "preview" + : false, + editableFocus: isEditableFocused(event.target), + modelPickerOpen: isModelPickerOpen(), + }, + }); + if (command !== "navigation.back" && command !== "navigation.forward") return; + + event.preventDefault(); + event.stopPropagation(); + if (command === "navigation.back") window.history.back(); + else window.history.forward(); + }; + + window.addEventListener("keydown", onKeyDown); + return () => window.removeEventListener("keydown", onKeyDown); + }, [keybindings, routeThreadRef]); + + return null; +} + // Settings swaps the thread sidebar out of the tree. Keep the lightweight // project projection subscribed so returning to a draft never renders the // zero-project state while the environment snapshot reconnects. @@ -269,6 +326,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) { {children} + ); diff --git a/apps/web/src/keybindings.test.ts b/apps/web/src/keybindings.test.ts index 82348be3b6..2d9da2e5eb 100644 --- a/apps/web/src/keybindings.test.ts +++ b/apps/web/src/keybindings.test.ts @@ -913,6 +913,25 @@ describe("resolveShortcutCommand", () => { ); }); + it("navigates history with mod+[ and mod+] outside the terminal", () => { + const back = event({ key: "[", code: "BracketLeft", metaKey: true }); + const forward = event({ key: "]", code: "BracketRight", ctrlKey: true }); + assert.strictEqual( + resolveShortcutCommand(back, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "MacIntel" }), + "navigation.back", + ); + assert.strictEqual( + resolveShortcutCommand(forward, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "Linux" }), + "navigation.forward", + ); + assert.isNull( + resolveShortcutCommand(back, DEFAULT_RESOLVED_KEYBINDINGS, { + platform: "MacIntel", + context: { terminalFocus: true }, + }), + ); + }); + it("matches bracket shortcuts using the physical key code", () => { assert.strictEqual( resolveShortcutCommand( diff --git a/docs/user/keybindings.md b/docs/user/keybindings.md index 548b03fd1a..ad1b157bbb 100644 --- a/docs/user/keybindings.md +++ b/docs/user/keybindings.md @@ -117,6 +117,9 @@ actions of the same kind undo together. The notice remains available for five seconds after the latest action. The default shortcut skips text fields and terminals so native undo keeps working there. +`navigation.back` (`mod+[` by default) and `navigation.forward` (`mod+]`) move +through the pages you have visited, like a browser's back and forward buttons. + `chat.new` may ask you to choose a project when there is more than one. `chat.newLocal` skips that chooser. Both use your [new-thread defaults](./thread-sidebar.md#start-a-thread). diff --git a/packages/contracts/src/keybindings.ts b/packages/contracts/src/keybindings.ts index 5093ba9a25..75dc891959 100644 --- a/packages/contracts/src/keybindings.ts +++ b/packages/contracts/src/keybindings.ts @@ -57,6 +57,8 @@ export type ModelPickerKeybindingCommand = (typeof MODEL_PICKER_KEYBINDING_COMMA export const STATIC_KEYBINDING_COMMANDS = [ "sidebar.toggle", + "navigation.back", + "navigation.forward", "terminal.toggle", "terminal.split", "terminal.splitVertical", diff --git a/packages/shared/src/keybindings.ts b/packages/shared/src/keybindings.ts index ee159b6838..6f1071767a 100644 --- a/packages/shared/src/keybindings.ts +++ b/packages/shared/src/keybindings.ts @@ -20,6 +20,8 @@ type WhenToken = export const DEFAULT_KEYBINDINGS: ReadonlyArray = [ { key: "mod+b", command: "sidebar.toggle" }, + { key: "mod+[", command: "navigation.back", when: "!terminalFocus" }, + { key: "mod+]", command: "navigation.forward", when: "!terminalFocus" }, { key: "mod+j", command: "terminal.toggle" }, { key: "mod+alt+b", command: "rightPanel.toggle" }, { key: "mod+d", command: "terminal.split", when: "terminalFocus" },