Skip to content

Build your personal knowledge base with Trilium Notes

fix(menu): place the context menu again when the viewport changes size

Elian Doran34a08a1bd849
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 25cb27846d..f075c73972 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -235,6 +235,18 @@ describe("contextMenu", () => {
const menu = await place({ x: 10, y: 20, width: 200, height: 2000 });
expect(menu.style.maxHeight).toBe("790px");
});
+
+ it("places itself again when the viewport changes size", async () => {
+ const menu = await place({ x: 700, y: 20, width: 200, height: 300 });
+ expect([ menu.style.left, menu.style.maxHeight ]).toEqual([ "700px", "790px" ]);
+
+ vi.spyOn(document.documentElement, "clientWidth", "get").mockReturnValue(800);
+ vi.spyOn(document.documentElement, "clientHeight", "get").mockReturnValue(600);
+ window.dispatchEvent(new Event("resize"));
+
+ // It no longer fits right of the pointer, so it flips to its left, and its cap shrinks.
+ await vi.waitFor(() => expect([ menu.style.left, menu.style.maxHeight ]).toEqual([ "500px", "590px" ]));
+ });
});
it("hides on a click anywhere on the page", async () => {
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index d11210ada0..4f58e61f1b 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -1,12 +1,11 @@
import "./Menu.css";
-import { computePosition, flip, type Placement, shift, size, type VirtualElement } from "@floating-ui/dom";
+import { autoUpdate, computePosition, flip, type Placement, shift, size, type VirtualElement } from "@floating-ui/dom";
import clsx from "clsx";
-import { useCallback, useLayoutEffect, useRef } from "preact/hooks";
+import { useLayoutEffect, useRef } from "preact/hooks";
import type { MenuCommandItem, MenuItem, MenuSeparatorItem } from "../../menus/context_menu";
import { getActionSync } from "../../services/keyboard_actions";
-import { useResizeObserver } from "./hooks";
import { joinElements } from "./react_utils";
import { renderShortcutKbds } from "./shortcut_kbd";
@@ -33,14 +32,16 @@ const VIEWPORT_PADDING = 5;
export default function Menu<T>({ id, className, x, y, orientation, items, onSelect }: MenuProps<T>) {
const menuRef = useRef<HTMLDivElement>(null);
- const place = useCallback(() => {
- if (!menuRef.current) return;
- void placeMenu(menuRef.current, pointAt(x, y), orientation === "left" ? "left-start" : "right-start");
- }, [ x, y, orientation ]);
- useLayoutEffect(place, [ place ]);
- // The menu's size can change after it opens, e.g. once asynchronously loaded items arrive.
- useResizeObserver(menuRef, place);
+ useLayoutEffect(() => {
+ const menu = menuRef.current;
+ if (!menu) return;
+
+ const anchor = pointAt(x, y);
+ const placement = orientation === "left" ? "left-start" : "right-start";
+ // Places the menu now, and again whenever the viewport or the menu itself changes size.
+ return autoUpdate(anchor, menu, () => void placeMenu(menu, anchor, placement));
+ }, [ x, y, orientation ]);
return (
<div ref={menuRef} id={id} className={clsx("dropdown-menu show tn-menu", className)} role="menu">