Skip to content

Commit 663f4e7

Browse files
clawsweepersteipeteshakkernerd
authored
fix(ui): make Settings Escape contextual (#128099)
* fix(ui): make Settings Escape contextual Co-authored-by: shakkernerd <[email protected]> * fix(ui): scope Settings Escape to search --------- Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: shakkernerd <[email protected]>
1 parent 3651289 commit 663f4e7

3 files changed

Lines changed: 95 additions & 2 deletions

File tree

‎ui/src/app/app-host-native-shell.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ type ShellNavigationState = {
2020
updated: () => void;
2121
};
2222

23+
type ShellSettingsEscapeState = ShellKeyboardState & {
24+
lastWorkspaceLocation: { routeId: "usage"; pathname: string; search: string };
25+
navDrawerOpen: boolean;
26+
routeState: { routeId: "appearance" };
27+
};
28+
2329
type TestWebKitWindow = Window & {
2430
webkit?: {
2531
messageHandlers: {
@@ -107,6 +113,41 @@ describe("OpenClaw native shell", () => {
107113
expect(navigate).toHaveBeenCalledWith("appearance", undefined);
108114
});
109115

116+
it("keeps the raw config editor unchanged when Escape is pressed", () => {
117+
const navigate = vi.fn();
118+
const shell = document.createElement(
119+
"openclaw-app-shell",
120+
) as unknown as ShellSettingsEscapeState;
121+
shell.runtime = {
122+
context: {
123+
navigate,
124+
overlays: { snapshot: { devicePairSetupOpen: false } },
125+
} as unknown as ApplicationContext,
126+
};
127+
shell.lastWorkspaceLocation = { routeId: "usage", pathname: "/usage", search: "" };
128+
shell.navDrawerOpen = false;
129+
shell.routeState = { routeId: "appearance" };
130+
const rawField = document.body.appendChild(document.createElement("label"));
131+
rawField.className = "config-raw-field";
132+
const rawEditor = rawField.appendChild(document.createElement("textarea"));
133+
rawEditor.value = '{ "gateway": { "port": 18789 } }';
134+
rawEditor.focus();
135+
const onInput = vi.fn();
136+
rawEditor.addEventListener("input", onInput);
137+
rawEditor.addEventListener("keydown", (event) => shell.handleDocumentKeydown(event));
138+
139+
try {
140+
const event = new KeyboardEvent("keydown", { key: "Escape", cancelable: true });
141+
rawEditor.dispatchEvent(event);
142+
143+
expect(rawEditor.value).toBe('{ "gateway": { "port": 18789 } }');
144+
expect(onInput).not.toHaveBeenCalled();
145+
expect(navigate).not.toHaveBeenCalled();
146+
} finally {
147+
rawField.remove();
148+
}
149+
});
150+
110151
it("toggles the navigation sidebar when the native macOS titlebar button fires", () => {
111152
const snapshot = { navCollapsed: false };
112153
const update = vi.fn((next: { navCollapsed: boolean }) => {

‎ui/src/components/settings-sidebar.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,54 @@ describe("settings sidebar search", () => {
440440
expect(onNavigate).toHaveBeenCalledWith("channels");
441441
});
442442

443+
it("clears a focused search before Escape exits Settings", () => {
444+
let searchQuery = "gateway";
445+
const onExit = vi.fn();
446+
const rerender = () => {
447+
render(
448+
renderSettingsSidebar({
449+
basePath: "",
450+
activeRouteId: "appearance",
451+
offline: false,
452+
lastError: null,
453+
gatewayVersion: "",
454+
updateAvailable: null,
455+
updateBusy: false,
456+
onUpdate: vi.fn(),
457+
...inactiveRefresh,
458+
searchQuery,
459+
onExit,
460+
onRetryConnect: vi.fn(),
461+
onNavigate: vi.fn(),
462+
onSearchQueryChange: (nextQuery) => {
463+
searchQuery = nextQuery;
464+
rerender();
465+
},
466+
preloadTimers: new Map(),
467+
saveIndicator: saveIndicator(),
468+
}),
469+
container,
470+
);
471+
};
472+
473+
rerender();
474+
const input = container.querySelector<HTMLInputElement>(".settings-sidebar__search-input");
475+
expect(input).not.toBeNull();
476+
input?.focus();
477+
478+
input?.dispatchEvent(
479+
new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }),
480+
);
481+
expect(searchQuery).toBe("");
482+
expect(document.activeElement).toBe(input);
483+
expect(onExit).not.toHaveBeenCalled();
484+
485+
input?.dispatchEvent(
486+
new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }),
487+
);
488+
expect(onExit).toHaveBeenCalledOnce();
489+
});
490+
443491
it("renders refreshed settings route titles from the active locale", async () => {
444492
i18n.registerTranslation("pt-BR", {
445493
routeTitles: {

‎ui/src/components/settings-sidebar.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,15 @@ export function renderSettingsSidebar(props: SettingsSidebarProps) {
272272
@input=${(event: Event) =>
273273
props.onSearchQueryChange((event.currentTarget as HTMLInputElement).value)}
274274
@keydown=${(event: KeyboardEvent) => {
275-
if (event.key !== "Escape" || !props.searchQuery) {
275+
if (event.key !== "Escape") {
276276
return;
277277
}
278278
event.preventDefault();
279-
props.onSearchQueryChange("");
279+
if (props.searchQuery) {
280+
props.onSearchQueryChange("");
281+
return;
282+
}
283+
props.onExit();
280284
}}
281285
/>
282286
${props.searchQuery

0 commit comments

Comments
 (0)