Skip to content

Build your personal knowledge base with Trilium Notes

fix(menu): keep the blur behind a context menu that scrolls

Elian Doran7ad3982a317b
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 6d812de9b2..006ffb27ce 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -32,6 +32,11 @@ function menuElement() {
return document.getElementById("context-menu-container");
}
+/** The top level's rows, which scroll inside the menu. */
+function menuRows() {
+ return [ ...menuElement()?.querySelector(":scope > .tn-menu-scroll")?.children ?? [] ];
+}
+
/** Puts an element on a screen of its own, as the browser reports one. */
function setFullscreenElement(element: Element | null) {
Object.defineProperty(document, "fullscreenElement", { value: element, configurable: true });
@@ -93,7 +98,7 @@ describe("contextMenu", () => {
]
});
- const rows = [ ...menuElement()?.children ?? [] ]
+ const rows = menuRows()
.map(row => `${row.tagName.toLowerCase()}.${row.className} ${row.textContent}`);
expect(rows).toEqual([
"li.dropdown-item Cut",
@@ -192,7 +197,7 @@ describe("contextMenu", () => {
]
});
- const rows = [ ...menuElement()?.children ?? [] ].map(row => `${row.tagName.toLowerCase()}.${row.className}`);
+ const rows = menuRows().map(row => `${row.tagName.toLowerCase()}.${row.className}`);
expect(rows).toEqual([
"li.dropdown-item", "div.dropdown-divider", "li.dropdown-custom-item", "div.dropdown-divider", "li.dropdown-item"
]);
@@ -246,7 +251,7 @@ describe("contextMenu", () => {
]
});
- const rows = [ ...menuElement()?.children ?? [] ]
+ const rows = menuRows()
.map(row => `${row.tagName.toLowerCase()}.${row.className} ${row.innerHTML.includes("<b>") ? "(markup)" : row.textContent}`);
expect(rows).toEqual([
"h6.dropdown-header <b>Doing</b>",
@@ -381,6 +386,20 @@ describe("contextMenu", () => {
expect(parent.classList.contains("submenu-open")).toBe(false);
});
+ it("scrolls the top level's rows inside the menu, and keeps its submenus out of that scroller", async () => {
+ await openMenu();
+ const menu = menuElement();
+ const [ scroller, ...others ] = [ ...menu?.children ?? [] ];
+ // The menu itself does not scroll, so the blur on its `::before` stays behind every row.
+ expect(scroller?.className).toBe("tn-menu-scroll");
+ expect(others).toEqual([]);
+ expect(scroller?.querySelector("li.dropdown-item")).not.toBeNull();
+
+ hover(row("Templates"));
+ await vi.waitFor(() => expect(layers()).toEqual([ [ "Meeting" ] ]));
+ expect(scroller?.querySelector("div.dropdown-submenu")).toBeNull();
+ });
+
it("places a layer beside its row, flipped where it does not fit, and hides it while its row is scrolled away", async () => {
vi.spyOn(document.documentElement, "clientWidth", "get").mockReturnValue(1000);
vi.spyOn(document.documentElement, "clientHeight", "get").mockReturnValue(800);
@@ -388,9 +407,9 @@ describe("contextMenu", () => {
vi.spyOn(HTMLUListElement.prototype, "offsetWidth", "get").mockReturnValue(150);
vi.spyOn(HTMLUListElement.prototype, "offsetHeight", "get").mockReturnValue(100);
await openMenu();
- const root = menuElement();
+ const root = menuElement()?.querySelector<HTMLElement>(":scope > .tn-menu-scroll");
if (!root) throw new Error("expected the menu to render");
- // happy-dom loads no stylesheet, so the menu is made to scroll as `Menu.css` makes it.
+ // happy-dom loads no stylesheet, so the rows are made to scroll as `Menu.css` makes them.
root.style.overflowY = "auto";
vi.spyOn(root, "getBoundingClientRect").mockReturnValue(DOMRect.fromRect({ x: 0, y: 0, width: 1000, height: 300 }));
// Floating UI clips to a scrolling ancestor's client box, which happy-dom leaves at 0.
diff --git a/apps/client/src/widgets/react/Menu.css b/apps/client/src/widgets/react/Menu.css
index cea2493b25..c6dc41bf6c 100644
--- a/apps/client/src/widgets/react/Menu.css
+++ b/apps/client/src/widgets/react/Menu.css
@@ -5,13 +5,40 @@
left: 0;
/* `placeMenu()` shows the menu once it has a position. */
visibility: hidden;
+}
+
+/*
+ * The top level does not scroll, so the blur the theme puts on its `::before` covers it whatever
+ * its rows scroll to. Its rows scroll inside, in the height `placeMenu()` caps it to.
+ */
+.dropdown-menu.tn-menu:has(> .tn-menu-scroll) {
+ display: flex;
+ flex-direction: column;
+}
+
+.tn-menu-scroll {
+ /* Matches the menu's own padding in the theme's `.dropdown-menu:not(.static)` rule. */
+ --tn-menu-inline-padding: var(--padding, var(--menu-padding-size));
+ flex: 1 1 auto;
+ min-height: 0;
+ /*
+ * Spans the menu's inline padding, so the separator and header lines, which the theme draws
+ * that far past a row, stay inside the scroller instead of scrolling it sideways. Without the
+ * theme's variables both fall back to 0, as that theme's menu has no inline padding.
+ */
+ margin-inline: calc(0px - var(--tn-menu-inline-padding));
+ padding-inline: var(--tn-menu-inline-padding);
overflow-y: auto;
}
-/* A submenu is placed by `placeMenu()`, not beside a parent it is nested in. */
+/*
+ * A submenu is placed by `placeMenu()`, not beside a parent it is nested in. The theme blurs it on
+ * the element itself rather than a `::before`, so it can scroll as a whole.
+ */
.dropdown-submenu > .dropdown-menu.tn-menu {
inset: 0 auto auto 0;
margin: 0;
+ overflow-y: auto;
}
/* The pointer leaves the row for its submenu, so the row stays marked while the submenu is open. */
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index c926bb1934..ea44d8a372 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -99,7 +99,11 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,
e.stopPropagation();
}}
>
- <MenuList level={0} items={items} state={state} />
+ {/* The rows scroll in here rather than the menu itself, so the theme's blur on the
+ menu's `::before` stays behind them. */}
+ <div className="tn-menu-scroll">
+ <MenuList level={0} items={items} state={state} />
+ </div>
{/* Inside the menu, so the rules scoped to it apply, but none inside another: a fixed
layer escapes a scrolling menu only while no ancestor carries a filter. */}
{submenus.map((submenu, index) => (