Skip to content

Commit d64888f

Browse files
treibermaria-rcks
andauthored
fix(web): Escape while recording a shortcut no longer closes Settings (#27151)
Co-authored-by: Nik <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 22ee8a4 commit d64888f

2 files changed

Lines changed: 106 additions & 12 deletions

File tree

‎apps/web/src/components/settings/KeybindingsSettings.browser.test.tsx‎

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
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";
44

55
import { render } from "../../../test/render";
66

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+
}));
718
vi.mock(import("./SettingsScopeContext"), async (original) => ({
819
...(await original()),
920
useSettingsScope: (() => {
@@ -22,16 +33,78 @@ vi.mock(import("./SettingsScopeContext"), async (original) => ({
2233
};
2334
}) as never,
2435
}));
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-
}));
3236

37+
import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
3338
import { KeybindingsSettingsPanel } from "./KeybindingsSettings";
3439

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+
35108
it("re-enables Save when a When editor closes on an expression it could not parse", async () => {
36109
await using screen = await render(
37110
<RegistryProvider>

‎apps/web/src/components/settings/KeybindingsSettings.tsx‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
useRef,
2121
useState,
2222
} from "react";
23+
import { flushSync } from "react-dom";
2324
import {
2425
AuthOrchestrationOperateScope,
2526
AuthSettingsWriteScope,
@@ -717,6 +718,7 @@ function useKeybindingRowEditor({
717718
onSave: (input: ServerUpsertKeybindingInput) => void;
718719
}) {
719720
const [draft, setDraft] = useReducer(keybindingRowDraftReducer, row, createKeybindingRowDraft);
721+
const pillRef = useRef<HTMLButtonElement>(null);
720722
const { keyDraft, whenDraft, isRecording, isWhenDraftValid } = draft;
721723
const whenDraftExpression = whenAstToExpression(whenDraft);
722724
const isDirty = keyDraft !== row.key || whenDraftExpression !== row.when;
@@ -739,7 +741,13 @@ function useKeybindingRowEditor({
739741
if (event.key === "Tab" && !isRecording) return;
740742
event.preventDefault();
741743
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();
743751
return;
744752
}
745753
const next = keybindingFromKeyboardEvent(event.nativeEvent, navigator.platform, {
@@ -760,6 +768,7 @@ function useKeybindingRowEditor({
760768
setDraft,
761769
save,
762770
captureKeybinding,
771+
pillRef,
763772
};
764773
}
765774

@@ -790,8 +799,16 @@ function KeybindingKeyControl({
790799
isSaving: boolean;
791800
pillClassName?: string | undefined;
792801
}) {
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;
795812
const showPill = !isRecording && keyDraft === row.key && row.key.length > 0 && !isDirty;
796813

797814
return (
@@ -807,6 +824,7 @@ function KeybindingKeyControl({
807824
) : null}
808825
{showPill ? (
809826
<button
827+
ref={pillRef}
810828
type="button"
811829
onClick={() => setDraft({ isRecording: true })}
812830
aria-label={`Edit shortcut for ${commandLabel(row.command)}: ${formatShortcutLabel(row.binding.shortcut)}`}
@@ -1056,7 +1074,10 @@ function useNewKeybindingDraft({
10561074
if (event.key === "Tab" && !isRecording) return;
10571075
event.preventDefault();
10581076
if (event.key === "Escape") {
1077+
// An unhandled Escape closes the settings page.
1078+
event.stopPropagation();
10591079
setDraft({ keyDraft: "", isRecording: false });
1080+
event.currentTarget.blur();
10601081
return;
10611082
}
10621083
const next = keybindingFromKeyboardEvent(event.nativeEvent, navigator.platform, {

0 commit comments

Comments
 (0)