diff --git a/apps/web/src/components/settings/ThemeCssEditor.tsx b/apps/web/src/components/settings/ThemeCssEditor.tsx index 314a5e26f5cba..1361356df0fa4 100644 --- a/apps/web/src/components/settings/ThemeCssEditor.tsx +++ b/apps/web/src/components/settings/ThemeCssEditor.tsx @@ -30,7 +30,8 @@ export function ThemeCssEditor({ } function handleKeyDown(event: KeyboardEvent) { - if (event.nativeEvent.isComposing || event.ctrlKey || event.metaKey || event.altKey) return; + if (event.nativeEvent.isComposing || event.keyCode === 229) return; + if (event.ctrlKey || event.metaKey || event.altKey) return; const editor = event.currentTarget; if (event.key === "Escape" && (tabIndents || event.repeat)) { event.preventDefault(); diff --git a/apps/web/src/components/settings/ThemeEditorPanel.browser.test.tsx b/apps/web/src/components/settings/ThemeEditorPanel.browser.test.tsx index 78a68f40936ff..af9133b1c9417 100644 --- a/apps/web/src/components/settings/ThemeEditorPanel.browser.test.tsx +++ b/apps/web/src/components/settings/ThemeEditorPanel.browser.test.tsx @@ -122,6 +122,29 @@ async function editorElement(selector: string) { return query()!; } +async function imeEscape(target: HTMLElement) { + await act(() => { + target.dispatchEvent( + new KeyboardEvent("keydown", { + bubbles: true, + cancelable: true, + key: "Escape", + isComposing: true, + }), + ); + // Safari ends the composition just before the Escape keydown. + target.dispatchEvent(new CompositionEvent("compositionend", { bubbles: true })); + target.dispatchEvent( + new KeyboardEvent("keydown", { + bubbles: true, + cancelable: true, + key: "Escape", + keyCode: 229, + }), + ); + }); +} + describe("ThemeEditorPanel", () => { it("keeps its left edge when resized after the window shrank", async () => { await using view = await renderPanel(); @@ -232,19 +255,25 @@ describe("ThemeEditorPanel", () => { .toHaveValue("#aabbcc"); }); - it("closes on Escape without closing Settings", async () => { + it("closes on Escape, but not an IME Escape, without closing Settings", async () => { await using view = await renderBesideSettings(); - (await editorElement("input")).focus(); + const input = await editorElement("input"); + input.focus(); + await imeEscape(input); + expect(view.onEditorOpenChange).not.toHaveBeenCalled(); + await userEvent.keyboard("{Escape}"); expect(view.onEditorOpenChange).toHaveBeenCalledWith(false); expect(view.onSettingsOpenChange).not.toHaveBeenCalled(); }); - it("leaves inspection on Escape without closing the editor or Settings", async () => { + it("leaves inspection on Escape, but not an IME Escape, without closing the editor or Settings", async () => { await using view = await renderBesideSettings(); (await editorElement('[aria-label="Inspect app colors"]')).click(); await editorElement('[aria-label="Cancel inspecting app colors"]'); + await imeEscape(await editorElement("input")); + expect(document.querySelector('[aria-label="Cancel inspecting app colors"]')).not.toBeNull(); await userEvent.keyboard("{Escape}"); await editorElement('[aria-label="Inspect app colors"]'); @@ -265,6 +294,7 @@ describe("ThemeEditorPanel", () => { const editor = await editorElement('textarea[aria-label="Custom CSS"]'); editor.focus(); + await imeEscape(editor); await userEvent.keyboard("{Escape}"); editor.dispatchEvent( new KeyboardEvent("keydown", { diff --git a/apps/web/src/components/settings/ThemeEditorPanel.tsx b/apps/web/src/components/settings/ThemeEditorPanel.tsx index af5490d901064..dd0947c90c8f9 100644 --- a/apps/web/src/components/settings/ThemeEditorPanel.tsx +++ b/apps/web/src/components/settings/ThemeEditorPanel.tsx @@ -709,7 +709,7 @@ export function ThemeEditorPanel({ shouldDisarmAfterClick = false; }; const cancelInspection = (event: KeyboardEvent) => { - if (event.key !== "Escape") return; + if (event.key !== "Escape" || event.isComposing || event.keyCode === 229) return; // Esc leaves inspection only; Settings must not see it and close too. event.preventDefault(); event.stopImmediatePropagation(); @@ -1183,6 +1183,7 @@ export function ThemeEditorPanel({ onKeyDown={(event) => { // Popups opened from the editor are portaled outside it and handle their own Esc. if (event.key !== "Escape" || event.defaultPrevented) return; + if (event.nativeEvent.isComposing || event.keyCode === 229) return; if (!event.currentTarget.contains(event.target as Node)) return; event.stopPropagation(); onOpenChange(false);