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
fix(menu): scroll a list to its top when the keys reach its first row
Elian Doranb8e115a9a2fb
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex 0233de049f..09cce1c804 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -1033,6 +1033,34 @@ describe("contextMenu", () => {expect(document.activeElement).toBe(menuElement());});+ it("scrolls back to the input when the keys come round to the first row", async () => {+ await openFilterable();+ await openSubmenuByKeys();+ const list = menuElement()?.querySelector<HTMLElement>("div.dropdown-submenu .tn-menu-scroll");+ if (!list) throw new Error("no submenu");+ const scrollTo = vi.spyOn(list, "scrollTo");++ key("ArrowDown");+ await vi.waitFor(() => expect(activeRow()).toBe("Code"));+ expect(scrollTo).not.toHaveBeenCalled();+ key("End");+ await vi.waitFor(() => expect(activeRow()).toBe("Meeting & notes"));+ key("ArrowDown");+ await vi.waitFor(() => expect(activeRow()).toBe("Text"));+ // As far up as the list goes, so the input above the first row shows again.+ expect(scrollTo).toHaveBeenLastCalledWith({ top: 0 });++ // From the input, as Up and Down go through the rows from there too.+ filterInput()?.focus();+ key("ArrowUp");+ await vi.waitFor(() => expect(activeRow()).toBe("Meeting & notes"));+ scrollTo.mockClear();+ key("ArrowDown");+ await vi.waitFor(() => expect(activeRow()).toBe("Text"));+ expect(scrollTo).toHaveBeenCalledWith({ top: 0 });+ vi.restoreAllMocks();+ });+it("takes a press into the input, as a phone needs to bring up its keyboard", async () => {await openFilterable();const parent = [ ...menuElement()?.querySelectorAll<HTMLElement>("li.dropdown-submenu") ?? [] ]diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 85e09a9b93..0344607eb7 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -110,7 +110,12 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,const setActive = useCallback((level: number, rowId: string) => {setActiveRow({ level, id: rowId });- rows.get(rowId)?.element.scrollIntoView?.({ block: "nearest" });+ const element = rows.get(rowId)?.element;+ // The first row of a scrolling list brings what stands above it back into view, such as a+ // filter input or a header, which scrolling only as far as the row would leave hidden.+ const list = element?.parentElement;+ if (list?.classList.contains("tn-menu-scroll") && !hasRowBefore(element)) list.scrollTo?.({ top: 0 });+ else element?.scrollIntoView?.({ block: "nearest" });}, [ rows ]);const registerRow = useCallback((rowId: string, row: RowEntry | undefined) => {if (!row) {@@ -576,6 +581,14 @@ function isFilterKey(input: HTMLInputElement, key: string) {return key === (rtl ? "ArrowRight" : "ArrowLeft") && input.value === "";}+/** Whether a row, enabled or not, stands before `row` in its list. */+function hasRowBefore(row: Element | undefined) {+ for (let sibling = row?.previousElementSibling; sibling; sibling = sibling.previousElementSibling) {+ if (sibling.classList.contains("dropdown-item")) return true;+ }+ return false;+}+/** The filter input of the submenu open in `menu`, if one is. */function filterInputIn(menu: HTMLElement | null) {return menu?.querySelector<HTMLInputElement>(FILTER_INPUT) ?? null;