Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/web/src/components/settings/ThemeCssEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function ThemeCssEditor({
}

function handleKeyDown(event: KeyboardEvent<HTMLTextAreaElement>) {
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();
Expand Down
36 changes: 33 additions & 3 deletions apps/web/src/components/settings/ThemeEditorPanel.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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"]');
Expand All @@ -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", {
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/components/settings/ThemeEditorPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Loading