Skip to content

Build your personal knowledge base with Trilium Notes

feat(menu): show a context submenu at once when it replaces one or is chosen

Elian Doran9ee2bd6e61e7
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index c5ae52c6f8..c76b17d008 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -458,6 +458,28 @@ describe("contextMenu", () => {
});
});
+ it("waits before the first submenu it opens under the pointer, and not before one that replaces it", async () => {
+ /** Whether the open layer skips the stylesheet's opening delay and fade. */
+ const immediate = () => menuElement()?.querySelector("div.dropdown-submenu > ul")?.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));
+ expect(immediate()).toBe(false);
+
+ // One submenu is open, so the user is going through them: its sibling shows at once.
+ hover(row("Templates"));
+ await vi.waitFor(() => expect(layers()).toEqual([ [ "Meeting" ] ]));
+ expect(immediate()).toBe(true);
+
+ // So does one a press opens, which is a choice rather than a pass.
+ await openMenu();
+ press(row("Templates"));
+ await vi.waitFor(() => expect(layers()).toEqual([ [ "Meeting" ] ]));
+ expect(immediate()).toBe(true);
+ });
+
it("runs a submenu's row and closes everything, and keeps a submenu's row that only opens it", async () => {
const picked: string[] = [];
const contextMenu = await openMenu((title) => picked.push(title));
@@ -682,7 +704,8 @@ describe("contextMenu", () => {
await vi.waitFor(() => expect(activeRow()).toBe("Templates"));
key("ArrowRight");
await vi.waitFor(() => expect(activeRow()).toBe("Meeting"));
- expect(menuElement()?.querySelector("div.dropdown-submenu")).not.toBeNull();
+ // A key is a choice, so the submenu shows without the stylesheet's opening delay.
+ expect(menuElement()?.querySelector("div.dropdown-submenu > ul")?.classList.contains("tn-menu-immediate")).toBe(true);
key("ArrowLeft");
await vi.waitFor(() => expect(activeRow()).toBe("Templates"));
diff --git a/apps/client/src/widgets/react/Menu.css b/apps/client/src/widgets/react/Menu.css
index 04b87d1d78..c301aa3bc1 100644
--- a/apps/client/src/widgets/react/Menu.css
+++ b/apps/client/src/widgets/react/Menu.css
@@ -60,6 +60,11 @@ 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. */
+body.desktop .dropdown-submenu > .dropdown-menu.tn-menu.tn-menu-immediate {
+ animation: none;
+}
+
/* The menu holds focus for its keys; the row they act on shows where they are instead. */
.dropdown-menu.tn-menu:focus {
outline: none;
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index a824f70038..7e2542c9c7 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -44,6 +44,11 @@ interface OpenSubmenu<T> {
anchor: HTMLElement;
/** Remounts the layer when another submenu replaces it at the same level. */
key: number;
+ /**
+ * 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.
+ */
+ immediate: boolean;
}
/** A row the keyboard can stand on: a command, or a custom row with something to focus in it. */
@@ -69,7 +74,7 @@ interface MenuState<T> {
* Opens `item`'s submenu at `level`, closing whatever stood open at that level and deeper, or
* without an item only closes them.
*/
- openSubmenu(level: number, item?: MenuCommandItem<T>, anchor?: HTMLElement): void;
+ openSubmenu(level: number, item?: MenuCommandItem<T>, anchor?: HTMLElement, chosen?: boolean): void;
/** The item whose submenu stands open at each level. */
openItems: MenuCommandItem<T>[];
active?: ActiveRow<T>;
@@ -83,11 +88,12 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,
const [ submenus, setSubmenus ] = useState<OpenSubmenu<T>[]>([]);
const nextKey = useRef(0);
- const openSubmenu = useCallback((level: number, item?: MenuCommandItem<T>, anchor?: HTMLElement) => {
+ const openSubmenu = useCallback((level: number, item?: MenuCommandItem<T>, anchor?: HTMLElement, chosen = false) => {
setSubmenus((open) => {
if (item ? open[level]?.item === item : open.length <= level) return open;
const kept = open.slice(0, level);
- return item && anchor ? [ ...kept, { item, anchor, key: nextKey.current++ } ] : kept;
+ const immediate = chosen || !!open[level];
+ return item && anchor ? [ ...kept, { item, anchor, key: nextKey.current++, immediate } ] : kept;
});
}, []);
const [ active, setActiveRow ] = useState<ActiveRow<T>>();
@@ -160,7 +166,7 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,
const item = active && !isCustom(active.item) ? active.item : undefined;
const anchor = item && rows.get(item);
if (!item?.items || !anchor || isMobile()) return;
- openSubmenu(level, item, anchor);
+ openSubmenu(level, item, anchor, true);
const first = runnableRows(item.items)[0];
if (first) setActive(level + 1, first);
};
@@ -301,7 +307,7 @@ function SubmenuLayer<T>({ level, submenu, state }: { level: number, submenu: Op
// In a `.dropdown-submenu`, so the theme's submenu rules apply.
return (
<div className="dropdown-submenu">
- <ul ref={layerRef} className="dropdown-menu show tn-menu" role="menu">
+ <ul ref={layerRef} className={clsx("dropdown-menu show tn-menu", submenu.immediate && "tn-menu-immediate")} role="menu">
{/* Like the top level, so the blur on the layer's `::before` stays behind its rows. */}
<div className="tn-menu-scroll">
{columns
@@ -408,7 +414,7 @@ function MenuRow<T>({ level, item, state }: { level: number, item: MenuCommandIt
setUnfolded(!unfolded);
return;
}
- if (hasSubmenu) state.openSubmenu(level, item, e.currentTarget);
+ if (hasSubmenu) state.openSubmenu(level, item, e.currentTarget, true);
state.onSelect(item, e);
}}
>