Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/ui/context menus: open submenus on the opposite side if there is not enough space available
Adorian Doran194b9fd1622e
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex 6020fbc580..a6f385de02 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -60,6 +60,58 @@ describe("contextMenu", () => {expect(menu?.parentElement).toBe(document.body);});+ /**+ * A submenu opens towards the trailing edge, and a menu raised near that edge would draw it off+ * the screen. It goes on the other side of its parent instead, which is what the page already+ * does for the launcher bar lying on its side.+ */+ it("opens a submenu on the other side of a menu standing at the edge", async () => {+ buildPage();+ const contextMenu = await buildContextMenu();+ Object.defineProperty(document.documentElement, "clientWidth",+ { value: 1000, configurable: true });+ Object.defineProperty(document.documentElement, "clientHeight",+ { value: 800, configurable: true });++ await contextMenu.show({+ x: 900, y: 10,+ items: [ { title: "More states", items } ],+ selectMenuItemHandler: () => {}+ });++ const parent = document.querySelector<HTMLElement>(".dropdown-submenu");+ const submenu = parent?.querySelector<HTMLElement>(".dropdown-menu");+ if (!parent || !submenu) throw new Error("expected a submenu");++ // jQuery binds `mouseenter` as a native `mouseover`, which is what the hover has to be.+ const hover = () =>+ parent.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));++ // happy-dom measures nothing, so the submenu is told where it stands.+ const place = (left: number, width: number) =>+ Object.defineProperty(submenu, "getBoundingClientRect", {+ value: () => ({+ left, right: left + width, width,+ top: 10, bottom: 110, height: 100+ }),+ configurable: true+ });++ place(910, 300);+ hover();+ expect(submenu.classList.contains("submenu-flip-start")).toBe(true);++ // Where it fits as it is, it is left where it opens.+ place(500, 300);+ hover();+ expect(submenu.classList.contains("submenu-flip-start")).toBe(false);++ // And where it fits on neither side, since flipping would only clip the other end.+ place(910, 950);+ hover();+ expect(submenu.classList.contains("submenu-flip-start")).toBe(false);+ });+it("tints an item's icon with the colour class it carries, and only the icon", async () => {const menu = buildPage();const contextMenu = await buildContextMenu();diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.tsindex 07d0c4c9a0..05850f40e4 100644--- a/apps/client/src/menus/context_menu.ts+++ b/apps/client/src/menus/context_menu.ts@@ -203,11 +203,13 @@ class ContextMenu {private repositionSubmenu(submenuEl: HTMLElement) {const CONTEXT_MENU_PADDING = 5;- // Reset so the natural (downward) placement is measured on every hover.+ // Reset so the natural (downward, trailing) placement is measured on every hover.submenuEl.classList.remove("submenu-flip-up");+ submenuEl.classList.remove("submenu-flip-start");const rect = submenuEl.getBoundingClientRect();const clientHeight = document.documentElement.clientHeight;+ const clientWidth = document.documentElement.clientWidth;const overflowsBottom = rect.bottom > clientHeight - CONTEXT_MENU_PADDING;// Only flip up if there is actually more room above the parent than below, otherwise flipping// would just clip the other end.@@ -216,6 +218,17 @@ class ContextMenu {if (overflowsBottom && fitsWhenFlippedUp) {submenuEl.classList.add("submenu-flip-up");}++ // The same for the side it opens on, which is the trailing one by default and the leading+ // one in a right-to-left page. Whichever edge it runs past, the flip puts it on the other+ // side of its parent, and only where the whole submenu fits there.+ const fitsWhenFlippedToStart = rect.left - rect.width >= CONTEXT_MENU_PADDING;+ const fitsWhenFlippedToEnd = rect.right + rect.width <= clientWidth - CONTEXT_MENU_PADDING;++ if ((rect.right > clientWidth - CONTEXT_MENU_PADDING && fitsWhenFlippedToStart)+ || (rect.left < CONTEXT_MENU_PADDING && fitsWhenFlippedToEnd)) {+ submenuEl.classList.add("submenu-flip-start");+ }}addItems($parent: JQuery<HTMLElement>, items: MenuItem<any>[], multicolumn = false) {diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex 980fddd4b6..65fc4add3e 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -1729,7 +1729,10 @@ body.desktop .dropdown-submenu > .dropdown-menu {max-width: 300px;}+/* Opened on the other side (`submenu-flip-start` added by ContextMenu.repositionSubmenu) when the+ submenu would overflow the side it opens on. */.dropdown-submenu.dropstart > .dropdown-menu,+.dropdown-submenu > .dropdown-menu.submenu-flip-start,body:not(.mobile) #launcher-pane.horizontal .dropdown-submenu > .dropdown-menu {inset-inline-start: auto;inset-inline-end: calc(100% - 2px);