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/menus: tweak submenu opening delay
Adorian Doranea51f75b040f
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex 62b8519e67..3f0767e7c1 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -640,17 +640,28 @@ describe("contextMenu", () => {});});- it("waits before the first submenu it opens under the pointer, and not before one that replaces it", async () => {+ it("waits before a submenu it opens under the pointer, unless it replaces one in view", async () => {+ const layer = () => menuElement()?.querySelector("div.dropdown-submenu > .dropdown-menu");/** Whether the open layer skips the stylesheet's opening delay and fade. */- const immediate = () => menuElement()?.querySelector("div.dropdown-submenu > .dropdown-menu")?.classList.contains("tn-menu-immediate");+ const immediate = () => layer()?.classList.contains("tn-menu-immediate");await openMenu();// The pointer might only be passing over the row.hover(row("Insert child note"));- await vi.waitFor(() => expect(layers()).toHaveLength(1));+ await vi.waitFor(() => expect(layers()).toEqual([ [ "Text", "Code" ] ]));expect(immediate()).toBe(false);- // One submenu is open, so the user is going through them: its sibling shows at once.+ // It passed on before that submenu showed, so the next one waits as well.+ hover(row("Templates"));+ await vi.waitFor(() => expect(layers()).toEqual([ [ "Meeting" ] ]));+ expect(immediate()).toBe(false);++ // The browser starts the fade once the delay is over. With a submenu in view, the user+ // is going through them, so its sibling shows at once, and so does the one after.+ layer()?.dispatchEvent(new Event("animationstart", { bubbles: true }));+ hover(row("Insert child note"));+ await vi.waitFor(() => expect(layers()).toEqual([ [ "Text", "Code" ] ]));+ expect(immediate()).toBe(true);hover(row("Templates"));await vi.waitFor(() => expect(layers()).toEqual([ [ "Meeting" ] ]));expect(immediate()).toBe(true);diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex f54cd62ab8..1eafc087a3 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -63,7 +63,7 @@ body#trilium-app.backdrop-effects-disabled *::after {}:root {- --submenu-opening-delay: 150ms;+ --submenu-opening-delay: 250ms;--launcher-pane-size: 53px;--bs-body-color: var(--main-text-color);/* What a menu, the sidebar or a dialog dims the page behind it with in the phone layout. */diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsxindex 7ec5b8932a..d5e2b1f50c 100644--- a/apps/client/src/widgets/react/FormList.tsx+++ b/apps/client/src/widgets/react/FormList.tsx@@ -617,7 +617,7 @@ function SubmenuLayer({ level, submenu, filter, children }: {children: ComponentChildren}) {const layerRef = useRef<HTMLDivElement>(null);- const { dropStart: dropStartLevels, setDropStart } = useMenu();+ const { dropStart: dropStartLevels, setDropStart, submenuShown } = useMenu();const dropStart = !!dropStartLevels[level - 1];const pinned = !!filter?.text;/** How far below its row's top the layer stands while pinned. */@@ -677,6 +677,10 @@ function SubmenuLayer({ level, submenu, filter, children }: {<divref={layerRef} role="menu" aria-labelledby={titleId(submenu.anchor.id)}className={clsx("dropdown-menu show tn-menu", submenu.immediate && "tn-menu-immediate")}+ // The fade starts once the opening delay is over.+ onAnimationStart={(e) => {+ if (e.target === e.currentTarget) submenuShown(submenu);+ }}><MenuList filter={filter}>{children}</MenuList></div>diff --git a/apps/client/src/widgets/react/Menu.css b/apps/client/src/widgets/react/Menu.cssindex cbb8ad07a7..80298766a2 100644--- a/apps/client/src/widgets/react/Menu.css+++ b/apps/client/src/widgets/react/Menu.css@@ -46,7 +46,7 @@ body.desktop:not(.motion-disabled) .dropdown-submenu > .dropdown-menu.tn-menu {animation-fill-mode: backwards;}-/* A submenu that replaces an open one, or that a key or a press opened, shows at once. */+/* A submenu that replaces one in view, or that a key or a press opened, shows at once. */body.desktop .dropdown-submenu > .dropdown-menu.tn-menu.tn-menu-immediate {animation: none;}diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 10ce25b576..7e07020c81 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -99,15 +99,22 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,else if (elementRef) elementRef.current = element;}, [ elementRef ]);const [ open, setOpen ] = useState<OpenSubmenu[]>([]);+ /** The submenus whose layer waited out the stylesheet's opening delay. */+ const shownSubmenus = useRef(new WeakSet<OpenSubmenu>()).current;const openSubmenu = useCallback((level: number, rowId?: string, anchor?: HTMLElement, chosen = false) => {setOpen((current) => {if (rowId ? current[level]?.id === rowId : current.length <= level) return current;const kept = current.slice(0, level);- const immediate = chosen || !!current[level];+ const replaced = current[level];+ const immediate = chosen+ || (!!replaced && (replaced.immediate || shownSubmenus.has(replaced)));return rowId && anchor ? [ ...kept, { id: rowId, anchor, immediate } ] : kept;});- }, []);+ }, [ shownSubmenus ]);+ const submenuShown = useCallback((submenu: OpenSubmenu) => {+ shownSubmenus.add(submenu);+ }, [ shownSubmenus ]);const [ dropStart, setDropStartLevels ] = useState<boolean[]>([]);const setDropStart = useCallback((level: number, value: boolean) => {setDropStartLevels((current) => {@@ -171,7 +178,8 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,});}, [ rows ]);const context: MenuContextValue = {- open, openSubmenu, active, setActive, rows, registerRow, keyboardDriven, isReversed, dropStart, setDropStart, close, filter, setFilter, layerHost+ open, openSubmenu, submenuShown, active, setActive, rows, registerRow, keyboardDriven,+ isReversed, dropStart, setDropStart, close, filter, setFilter, layerHost};/** The ids of the rows the keys can stand on at `level`, in the order they stand in. */diff --git a/apps/client/src/widgets/react/menu_context.ts b/apps/client/src/widgets/react/menu_context.tsindex c121156924..9585f7acf7 100644--- a/apps/client/src/widgets/react/menu_context.ts+++ b/apps/client/src/widgets/react/menu_context.ts@@ -15,7 +15,7 @@ export interface OpenSubmenu {anchor: HTMLElement;/*** Shows the layer without the stylesheet's opening delay and fade: it replaces a submenu that- * was open, or a key or a press chose it, so it is not a pointer passing over its row.+ * was in view, or a key or a press chose it, so it is not a pointer passing over its row.*/immediate: boolean;}@@ -63,6 +63,8 @@ export interface MenuContextValue {* deeper, or without a row only closes them.*/openSubmenu(level: number, id?: string, anchor?: HTMLElement, chosen?: boolean): void;+ /** Records that the layer of `submenu` is past the stylesheet's opening delay, and in view. */+ submenuShown(submenu: OpenSubmenu): void;active?: ActiveRow;setActive(level: number, id: string): void;/** The rows by id, as they registered. */