Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- Search boxes and other forms in plugin pages did nothing when you pressed Enter or the submit button
- Desktop app linked to a server: the setup screen could flash open and closed over and over after an update
- Dropdowns in plugin pages closed again the moment they opened

## 26.11.3

Expand Down
2 changes: 2 additions & 0 deletions src/ui/plugin-frame/install-shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import { installShims } from "./shims";
import { installFrameCapture } from "@/diagnostics/capture";
import { installFormSubmitShim } from "./form-submit";
import { installMenuResizeShim } from "./menu-resize";

installShims();
installFrameCapture();
installFormSubmitShim();
installMenuResizeShim();
24 changes: 24 additions & 0 deletions src/ui/plugin-frame/menu-resize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* A frame sized to its content grows while a menu is open, so the menu has
* room (withOpenMenus in frame-root). Growing resizes the frame's window,
* and a Radix Select closes on any window resize: it shut again the moment
* it opened. A resize that only changes the height while a menu is open is
* that growth, so it stops here before it reaches the menu. A real resize
* changes the width too and still closes it.
*/

const OPEN_MENU = "[data-radix-popper-content-wrapper]";

export function installMenuResizeShim(win: Window = window): () => void {
let width = win.innerWidth;
const onResize = (event: Event) => {
const sameWidth = win.innerWidth === width;
width = win.innerWidth;
if (sameWidth && win.document.querySelector(OPEN_MENU)) {
event.stopImmediatePropagation();
}
};
// Added before any plugin code runs, so it is the first resize listener.
win.addEventListener("resize", onResize);
return () => win.removeEventListener("resize", onResize);
}
52 changes: 52 additions & 0 deletions src/ui/tests/plugin-frame/menu-resize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { installMenuResizeShim } from "@/plugin-frame/menu-resize";

let uninstall: () => void = () => {};
const closed = vi.fn();

function setWidth(width: number) {
Object.defineProperty(window, "innerWidth", {
configurable: true,
value: width,
});
}

function openMenu() {
const wrapper = document.createElement("div");
wrapper.setAttribute("data-radix-popper-content-wrapper", "");
document.body.append(wrapper);
}

beforeEach(() => {
setWidth(800);
uninstall = installMenuResizeShim(window);
// Like Radix Select, which closes on a resize.
window.addEventListener("resize", closed);
});

afterEach(() => {
window.removeEventListener("resize", closed);
uninstall();
document.body.innerHTML = "";
closed.mockReset();
});

describe("menu resize shim", () => {
it("keeps a menu open while the frame grows taller to fit it", () => {
openMenu();
window.dispatchEvent(new Event("resize"));
expect(closed).not.toHaveBeenCalled();
});

it("still lets a real resize close the menu", () => {
openMenu();
setWidth(640);
window.dispatchEvent(new Event("resize"));
expect(closed).toHaveBeenCalledOnce();
});

it("passes every resize on while no menu is open", () => {
window.dispatchEvent(new Event("resize"));
expect(closed).toHaveBeenCalledOnce();
});
});
Loading