@devries-cameron20 / t3code Mirror · read-only / commits / d64888fbc4
Fork 0

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, 08:15 UTC

fix(web): Escape while recording a shortcut no longer closes Settings (#27151)

Co-authored-by: Nik <5053591+treiber@users.noreply.github.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Nik committed Nov 1, 2026 at 03:35 UTC d64888fbc403b52081761140d045542acc0391f2
2 files changed +106 -12
apps/web/src/components/settings/KeybindingsSettings.browser.test.tsx
+82 -9
@@ -1,9 +1,20 @@
1 import { RegistryProvider } from "@effect/atom-react";
2 -import { userEvent } from "vite-plus/test/browser";
3 -import { expect, it, vi } from "vite-plus/test";
2 +import { describe, expect, it, vi } from "vite-plus/test";
3 +import { page, userEvent, type Locator } from "vite-plus/test/browser";
4
5 import { render } from "../../../test/render";
6
7 +vi.mock(import("../../state/session"), async (original) => ({
8 + ...(await original()),
9 + useEnvironmentScope: () => true,
10 + useEnvironmentsWithScope: ((environments: ReadonlyArray<{ environmentId: string }>) =>
11 + new Set(environments.map(({ environmentId }) => environmentId))) as never,
12 + readEnvironmentScope: () => true,
13 +}));
14 +vi.mock(import("../../state/use-atom-command"), async (original) => ({
15 + ...(await original()),
16 + useAtomCommand: (() => vi.fn()) as never,
17 +}));
18 vi.mock(import("./SettingsScopeContext"), async (original) => ({
19 ...(await original()),
20 useSettingsScope: (() => {
@@ -22,16 +33,78 @@ vi.mock(import("./SettingsScopeContext"), async (original) => ({
33 };
34 }) as never,
35 }));
25 -vi.mock(import("../../state/session"), async (original) => ({
26 - ...(await original()),
27 - useEnvironmentScope: () => true,
28 - useEnvironmentsWithScope: ((environments: ReadonlyArray<{ environmentId: string }>) =>
29 - new Set(environments.map((environment) => environment.environmentId))) as never,
30 - readEnvironmentScope: () => true,
31 -}));
36
37 +import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
38 import { KeybindingsSettingsPanel } from "./KeybindingsSettings";
39
40 +// The modal dialog's inert layer swallows pointer events in this harness, so drive the DOM.
41 +async function activate(locator: Locator, action: "click" | "focus") {
42 + await expect.element(locator).toBeVisible();
43 + (locator.element() as HTMLElement)[action]();
44 +}
45 +
46 +const existingField = page.getByRole("textbox", { name: "Keybinding for Sidebar: Toggle" });
47 +const existingPill = page.getByRole("button", { name: /^Edit shortcut for Sidebar: Toggle:/ });
48 +const newField = page.getByRole("textbox", { name: "Keybinding for new keybinding" });
49 +
50 +describe("Esc in a shortcut recorder", () => {
51 + it.each<{
52 + name: string;
53 + field: Locator;
54 + open: () => Promise<void>;
55 + expectReset: () => Promise<void>;
56 + }>([
57 + {
58 + name: "existing binding",
59 + field: existingField,
60 + open: () => activate(existingPill, "click"),
61 + expectReset: async () => {
62 + await expect.element(existingField).not.toBeInTheDocument();
63 + await expect.element(existingPill).toHaveFocus();
64 + await expect.element(page.getByRole("button", { name: "Save" })).not.toBeInTheDocument();
65 + },
66 + },
67 + {
68 + name: "new binding",
69 + field: newField,
70 + open: async () => {
71 + await activate(page.getByRole("button", { name: "Add keybinding" }), "click");
72 + await activate(newField, "focus");
73 + },
74 + expectReset: async () => {
75 + await expect.element(newField).toHaveValue("");
76 + await expect.element(newField).toHaveAttribute("placeholder", "Unassigned");
77 + },
78 + },
79 + ])("leaves the $name field without closing Settings", async ({ field, open, expectReset }) => {
80 + const onOpenChange = vi.fn();
81 + await using _screen = await render(
82 + <Dialog open onOpenChange={onOpenChange}>
83 + <DialogPopup>
84 + <DialogTitle>Settings</DialogTitle>
85 + <KeybindingsSettingsPanel />
86 + </DialogPopup>
87 + </Dialog>,
88 + );
89 +
90 + await open();
91 + await userEvent.keyboard("{Control>}k{/Control}");
92 + await expect.element(field).not.toHaveValue("");
93 + await userEvent.tab();
94 + await activate(field, "focus");
95 + await expect.element(field).toHaveAttribute("placeholder", "Press shortcut");
96 +
97 + await userEvent.keyboard("{Escape}");
98 +
99 + expect(onOpenChange).not.toHaveBeenCalled();
100 + expect(document.activeElement?.matches("[data-keybinding-capture]")).toBe(false);
101 + await expectReset();
102 +
103 + await userEvent.keyboard("{Escape}");
104 + expect(onOpenChange).toHaveBeenCalledWith(false, expect.anything());
105 + });
106 +});
107 +
108 it("re-enables Save when a When editor closes on an expression it could not parse", async () => {
109 await using screen = await render(
110 <RegistryProvider>
apps/web/src/components/settings/KeybindingsSettings.tsx
+24 -3
@@ -20,6 +20,7 @@ import {
20 useRef,
21 useState,
22 } from "react";
23 +import { flushSync } from "react-dom";
24 import {
25 AuthOrchestrationOperateScope,
26 AuthSettingsWriteScope,
@@ -717,6 +718,7 @@ function useKeybindingRowEditor({
718 onSave: (input: ServerUpsertKeybindingInput) => void;
719 }) {
720 const [draft, setDraft] = useReducer(keybindingRowDraftReducer, row, createKeybindingRowDraft);
721 + const pillRef = useRef<HTMLButtonElement>(null);
722 const { keyDraft, whenDraft, isRecording, isWhenDraftValid } = draft;
723 const whenDraftExpression = whenAstToExpression(whenDraft);
724 const isDirty = keyDraft !== row.key || whenDraftExpression !== row.when;
@@ -739,7 +741,13 @@ function useKeybindingRowEditor({
741 if (event.key === "Tab" && !isRecording) return;
742 event.preventDefault();
743 if (event.key === "Escape") {
742 - setDraft({ keyDraft: row.key, isRecording: false });
744 + // An unhandled Escape closes the settings page.
745 + event.stopPropagation();
746 + const field = event.currentTarget;
747 + flushSync(() => setDraft({ keyDraft: row.key, isRecording: false }));
748 + // The field may unmount for the pill; focus would otherwise drop to the page.
749 + if (pillRef.current) pillRef.current.focus();
750 + else field.blur();
751 return;
752 }
753 const next = keybindingFromKeyboardEvent(event.nativeEvent, navigator.platform, {
@@ -760,6 +768,7 @@ function useKeybindingRowEditor({
768 setDraft,
769 save,
770 captureKeybinding,
771 + pillRef,
772 };
773 }
774
@@ -790,8 +799,16 @@ function KeybindingKeyControl({
799 isSaving: boolean;
800 pillClassName?: string | undefined;
801 }) {
793 - const { keyDraft, isRecording, isDirty, isWhenDraftValid, setDraft, save, captureKeybinding } =
794 - editor;
802 + const {
803 + keyDraft,
804 + isRecording,
805 + isDirty,
806 + isWhenDraftValid,
807 + setDraft,
808 + save,
809 + captureKeybinding,
810 + pillRef,
811 + } = editor;
812 const showPill = !isRecording && keyDraft === row.key && row.key.length > 0 && !isDirty;
813
814 return (
@@ -807,6 +824,7 @@ function KeybindingKeyControl({
824 ) : null}
825 {showPill ? (
826 <button
827 + ref={pillRef}
828 type="button"
829 onClick={() => setDraft({ isRecording: true })}
830 aria-label={`Edit shortcut for ${commandLabel(row.command)}: ${formatShortcutLabel(row.binding.shortcut)}`}
@@ -1056,7 +1074,10 @@ function useNewKeybindingDraft({
1074 if (event.key === "Tab" && !isRecording) return;
1075 event.preventDefault();
1076 if (event.key === "Escape") {
1077 + // An unhandled Escape closes the settings page.
1078 + event.stopPropagation();
1079 setDraft({ keyDraft: "", isRecording: false });
1080 + event.currentTarget.blur();
1081 return;
1082 }
1083 const next = keybindingFromKeyboardEvent(event.nativeEvent, navigator.platform, {