Skip to content

Build your personal knowledge base with Trilium Notes

client/ui/filterable menus: hold the width while filtering

Adorian Doran41d2d84b9be4
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 3cf626c5cf..62b8519e67 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -1178,6 +1178,40 @@ describe("contextMenu", () => {
}
});
+ it("holds its width while filtered, widening only for a wider match", async () => {
+ // As wide as its longest row, and no narrower than its `min-width`.
+ const width = function (this: HTMLElement) {
+ if (!this.matches(".dropdown-menu")) return 0;
+ const rows = [ ...this.querySelectorAll("li.dropdown-item") ].map((row) => row.textContent?.length ?? 0);
+ return Math.max(parseFloat(this.style.minWidth) || 0, 10 * Math.max(0, ...rows));
+ };
+ vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockImplementation(width);
+ const submenuLayer = () => menuElement()?.querySelector<HTMLElement>("div.dropdown-submenu > .dropdown-menu");
+ try {
+ await openFilterable();
+ await openSubmenuByKeys();
+ // "Meeting & notes".
+ expect(submenuLayer()?.offsetWidth).toBe(150);
+ expect(submenuLayer()?.style.minWidth).toBe("");
+
+ type("py");
+ await vi.waitFor(() => expect(layer().rows).toEqual([ "Python|Code" ]));
+ expect(submenuLayer()?.style.minWidth).toBe("150px");
+ // "Board" with its badge and path, wider than the submenu was.
+ edit("collection");
+ await vi.waitFor(() => expect(layer().rows).toHaveLength(2));
+ edit("tab");
+ await vi.waitFor(() => expect(layer().rows).toEqual([ "Table|Collection" ]));
+ expect(submenuLayer()?.style.minWidth).toBe("180px");
+
+ edit("");
+ await vi.waitFor(() => expect(layer().rows).toEqual(unfiltered));
+ expect(submenuLayer()?.style.minWidth).toBe("");
+ } finally {
+ vi.restoreAllMocks();
+ }
+ });
+
it("scrolls the list to the top when the keys wrap to the first row", async () => {
await openFilterable();
await openSubmenuByKeys();
diff --git a/apps/client/src/widgets/react/Menu.spec.tsx b/apps/client/src/widgets/react/Menu.spec.tsx
index a9d80cfe3c..7823b3a7ea 100644
--- a/apps/client/src/widgets/react/Menu.spec.tsx
+++ b/apps/client/src/widgets/react/Menu.spec.tsx
@@ -541,6 +541,7 @@ describe("Menu of items above its anchor", () => {
await vi.waitFor(() => expect(rowsOf(list()))
.toEqual([ "JavaScript", "Plain text", "Python" ]));
expect(activeRow(menu)).toBe("Python");
+ expect(menu.style.minWidth).toBe("200px");
key(menu, "ArrowUp");
await vi.waitFor(() => expect(activeRow(menu)).toBe("Plain text"));
});
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index 1e715caf71..10ce25b576 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -126,6 +126,9 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,
/** The filter as the keys last left it, ahead of the render that shows it: keys come faster. */
const filterRef = useRef<MenuFilter | undefined>(undefined);
const setFilter = useCallback((next: MenuFilter | undefined) => {
+ const previous = filterRef.current;
+ if (previous && previous.level !== next?.level) holdWidth(menuRef.current, previous.level, false);
+ if (next) holdWidth(menuRef.current, next.level, true);
filterRef.current = next;
setFilterState(next);
}, []);
@@ -760,6 +763,17 @@ function filterInputIn(menu: HTMLElement | null) {
return menu?.querySelector<HTMLInputElement>(FILTER_INPUT) ?? null;
}
+/**
+ * Holds the frame of the level a filter at `level` filters at no less than its current width, so
+ * the matches do not resize it as they change, or with `hold` false releases it. Called before the
+ * filter renders, while the frame still shows the rows the width is taken from.
+ */
+function holdWidth(menu: HTMLElement | null, level: number, hold: boolean) {
+ const input = menu?.querySelector<HTMLElement>(`${FILTER_INPUT}[data-level="${level + 1}"]`);
+ const frame = input?.closest<HTMLElement>(".dropdown-menu");
+ if (frame) frame.style.minWidth = hold ? `${frame.offsetWidth}px` : "";
+}
+
/**
* Whether the element focused in a custom row handles `key` itself, as listed in the
* space-separated `data-menu-keys` of it or an ancestor, as a date grid keeps Up and Down.