Skip to content

Commit ab1afdb

Browse files
Aatman Jainclaude
authored andcommitted
fix(web): Esc closes the theme editor, not Settings
Esc while inspecting cancelled the inspector and also closed Settings, and Esc inside the editor closed Settings while the editor stayed open. Stop the inspector's Esc from reaching other listeners, and close the editor on an Esc pressed inside it. Popups opened from the editor are portaled outside it and keep their own Esc. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d414cb5 commit ab1afdb

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { Suspense } from "react";
2+
import { describe, expect, it, vi } from "vite-plus/test";
3+
import { userEvent } from "vite-plus/test/browser";
4+
5+
import { render } from "../../../test/render";
6+
import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
7+
import { ThemeEditorPanel } from "./ThemeEditorPanel";
8+
9+
// The modal Settings dialog marks everything outside it inert in this harness, so the
10+
// test drives the editor through the DOM instead of pointer actions.
11+
async function editorElement<T extends HTMLElement>(selector: string): Promise<T> {
12+
await expect
13+
.poll(() => document.querySelector(`[data-theme-editor-panel] ${selector}`))
14+
.toBeTruthy();
15+
return document.querySelector<T>(`[data-theme-editor-panel] ${selector}`)!;
16+
}
17+
18+
// The editor floats beside the Settings dialog, whose Esc handler sits on the document.
19+
function SettingsWithEditor(props: {
20+
onSettingsOpenChange: (open: boolean) => void;
21+
onEditorOpenChange: (open: boolean) => void;
22+
}) {
23+
return (
24+
<>
25+
<Dialog onOpenChange={props.onSettingsOpenChange} open>
26+
<DialogPopup>
27+
<DialogTitle>Settings</DialogTitle>
28+
</DialogPopup>
29+
</Dialog>
30+
<Suspense fallback={null}>
31+
<ThemeEditorPanel
32+
editingTheme={null}
33+
initialAppearance="light"
34+
onOpenChange={props.onEditorOpenChange}
35+
onSaved={() => true}
36+
open
37+
restoreTheme={() => undefined}
38+
/>
39+
</Suspense>
40+
</>
41+
);
42+
}
43+
44+
describe("Esc in the theme editor", () => {
45+
it("closes the editor, not Settings", async () => {
46+
const onSettingsOpenChange = vi.fn();
47+
const onEditorOpenChange = vi.fn();
48+
await using _screen = await render(
49+
<SettingsWithEditor
50+
onEditorOpenChange={onEditorOpenChange}
51+
onSettingsOpenChange={onSettingsOpenChange}
52+
/>,
53+
);
54+
55+
(await editorElement<HTMLInputElement>("input")).focus();
56+
await userEvent.keyboard("{Escape}");
57+
58+
expect(onEditorOpenChange).toHaveBeenCalledWith(false);
59+
expect(onSettingsOpenChange).not.toHaveBeenCalled();
60+
});
61+
62+
it("cancels inspecting only", async () => {
63+
const onSettingsOpenChange = vi.fn();
64+
const onEditorOpenChange = vi.fn();
65+
await using _screen = await render(
66+
<SettingsWithEditor
67+
onEditorOpenChange={onEditorOpenChange}
68+
onSettingsOpenChange={onSettingsOpenChange}
69+
/>,
70+
);
71+
72+
(await editorElement('[aria-label="Inspect app colors"]')).click();
73+
await editorElement('[aria-label="Cancel inspecting app colors"]');
74+
await userEvent.keyboard("{Escape}");
75+
76+
await editorElement('[aria-label="Inspect app colors"]');
77+
expect(onEditorOpenChange).not.toHaveBeenCalled();
78+
expect(onSettingsOpenChange).not.toHaveBeenCalled();
79+
});
80+
});

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,9 @@ export function ThemeEditorPanel({
667667
};
668668
const cancelInspection = (event: KeyboardEvent) => {
669669
if (event.key !== "Escape") return;
670+
// Esc leaves inspection only; Settings must not see it and close too.
670671
event.preventDefault();
672+
event.stopImmediatePropagation();
671673
clearHover();
672674
clearInspectorSelection();
673675
};
@@ -1077,6 +1079,13 @@ export function ThemeEditorPanel({
10771079
isMinimized && "max-h-none",
10781080
)}
10791081
data-theme-editor-panel
1082+
onKeyDown={(event) => {
1083+
// Popups opened from the editor are portaled outside it and handle their own Esc.
1084+
if (event.key !== "Escape" || event.defaultPrevented) return;
1085+
if (!event.currentTarget.contains(event.target as Node)) return;
1086+
event.stopPropagation();
1087+
onOpenChange(false);
1088+
}}
10801089
ref={panelRef}
10811090
role="dialog"
10821091
style={{

0 commit comments

Comments
 (0)