Skip to content

Build your personal knowledge base with Trilium Notes

Merge remote-tracking branch 'origin/main' into feature/dropdown_rewrite

Elian Doran1ae68ac1adb9
diff --cc apps/client/src/menus/context_menu.spec.ts
index 86a0da9f3f,d4dcb87efd..03f20abed2
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@@ -894,6 -884,32 +894,33 @@@ describe("contextMenu", () =>
}
});
+ it("keeps the keys on a phone's row that a tap folded, not on the hidden child", async () => {
+ layout.onMobile = true;
+ try {
+ const picked: string[] = [];
+ await openMenu((title) => picked.push(title));
+ key("End");
+ await vi.waitFor(() => expect(activeRow()).toBe("Templates"));
+ key("Enter");
+ await vi.waitFor(() => expect(activeRow()).toBe("Meeting"));
+ const parent = menuElement()?.querySelector<HTMLElement>("li.dropdown-submenu");
+ expect(parent).toBeTruthy();
+
+ parent?.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 0 }));
++ parent?.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, button: 0 }));
+ await vi.waitFor(() => expect(parent?.classList.contains("submenu-open")).toBe(false));
+ expect(activeRow()).toBe("Templates");
+
+ key("ArrowUp");
+ await vi.waitFor(() => expect(activeRow()).toBe("Paste"));
+ key("Enter");
+ expect(picked).not.toContain("Meeting");
+ expect(picked.at(-1)).toBe("Paste");
+ } finally {
+ layout.onMobile = false;
+ }
+ });
+
it("runs the active row on Enter, with focus back where it was", async () => {
const picked: string[] = [];
const { contextMenu, editor } = await openMenu((title) => {
diff --cc apps/client/src/widgets/react/FormList.tsx
index 0584c54c18,4608e045c3..c376f87193
--- a/apps/client/src/widgets/react/FormList.tsx
+++ b/apps/client/src/widgets/react/FormList.tsx
@@@ -345,115 -247,20 +345,117 @@@ export function FormListHeader({ text,
);
}
+/** A line between groups of rows. A click on it closes no menu. */
export function FormDropdownDivider() {
- return <div
- className="dropdown-divider"
- onClick={e => e.stopPropagation()}
- />;
+ return <li className="dropdown-divider" role="separator" onClick={(e) => e.stopPropagation()} />;
}
-export function FormDropdownSubmenu({ icon, title, children, dropStart, onDropdownToggleClicked }: {
- icon: string,
- title: ComponentChildren,
- children: ComponentChildren,
- onDropdownToggleClicked?: () => void,
- dropStart?: boolean
-}) {
+export interface FormDropdownSubmenuProps {
+ icon: string;
+ title: ComponentChildren;
+ children: ComponentChildren;
+ /** Called when the row is clicked, or run from the keyboard, on the desktop. */
+ onDropdownToggleClicked?: (e: MouseEvent) => void;
+ /** Opens the nested list towards the start. Inside a menu, its layer flips on its own. */
+ dropStart?: boolean;
+ /** Inside a menu, lays the submenu's rows out in this many columns. */
+ columns?: number;
+ disabled?: boolean;
+ className?: string;
+}
+
+/**
+ * A row that opens its children as a submenu. Inside a `Menu` the submenu is a layer of its own
+ * beside the row, which a scrolling menu neither clips nor scrolls away; elsewhere, as in a
+ * dropdown, it is a list nested in the row, shown on hover.
+ */
+export function FormDropdownSubmenu(props: FormDropdownSubmenuProps) {
+ const menu = useContext(MenuContext);
+ return menu ? <MenuSubmenu {...props} menu={menu} /> : <NestedSubmenu {...props} />;
+}
+
+function MenuSubmenu({ menu, icon, title, children, onDropdownToggleClicked, columns, disabled, className }: FormDropdownSubmenuProps & { menu: MenuContextValue }) {
+ const level = useContext(MenuLevelContext);
+ const id = useUniqueName("menu-row");
+ const openSubmenu = menu.open[level];
+ const open = openSubmenu?.id === id;
+ const isActive = menu.active?.id === id;
+
+ /** Runs the row's own action for the keys, with the modifiers of the key that ran it. */
+ function select(e: MouseEvent | KeyboardEvent) {
+ onDropdownToggleClicked?.(new MouseEvent("click", { ctrlKey: e.ctrlKey, shiftKey: e.shiftKey, altKey: e.altKey, metaKey: e.metaKey }));
+ }
+
+ function onPointed(e: { currentTarget: HTMLLIElement }) {
+ if (isMobile()) return;
+ // The keyboard goes on from the row the pointer last pointed at.
+ if (!disabled) menu.setActive(level, id);
+ menu.openSubmenu(level, disabled ? undefined : id, e.currentTarget);
+ }
+
+ return (
+ <li
+ id={id}
+ ref={(element) => {
+ menu.registerRow(id, element ? { level, element, custom: false, disabled: !!disabled, hasSubmenu: true, select } : undefined);
+ }}
+ className={clsx("dropdown-item dropdown-submenu", open && "submenu-open", isActive && "tn-menu-active",
+ disabled && "disabled", className)}
+ role="menuitem"
+ aria-disabled={disabled || undefined}
+ aria-haspopup="menu"
+ aria-expanded={open}
+ // While the keys drive the menu, a row entered by a pointer at rest, as when the menu
+ // appears under it, keeps the keys' row. See `pointerMoved`.
+ onPointerEnter={(e) => {
+ if (!menu.keyboardDriven) onPointed(e);
+ }}
+ // The keys can move the active row from under a pointer at rest, whose `:hover` would
+ // then mark a second row. The next move of the pointer makes its row the active one.
+ onPointerMove={(e) => {
+ if (!isActive && pointerMoved(e)) onPointed(e);
+ }}
+ // As any row: the press keeps focus where it was, and the release acts. An unfolded
+ // row holds its submenu's rows, whose presses and clicks reach it too; those are theirs.
+ onMouseDown={(e) => {
+ if (e.button === 0 && !inUnfoldedRows(e)) e.preventDefault();
+ }}
+ onClick={(e) => {
+ if (inUnfoldedRows(e)) return;
+ // The dropdown's `closeOnClickInside` would close the whole menu.
+ e.stopPropagation();
+ if (disabled) return;
- // Clicked again, an unfolded row folds its submenu back.
++ // Clicked again, an unfolded row folds its submenu back. The keys go on from this
++ // row, as a folded or replaced submenu's rows are gone.
+ if (isMobile()) {
+ menu.openSubmenu(level, open ? undefined : id, e.currentTarget, true);
++ menu.setActive(level, id);
+ return;
+ }
+ menu.openSubmenu(level, id, e.currentTarget, true);
+ onDropdownToggleClicked?.(e);
+ }}
+ >
+ <span className="dropdown-toggle">
+ <Icon icon={icon} />
+ <span className="tn-menu-gap" />
+ <span id={titleId(id)}>{title}</span>
+ </span>
+ {isMobile()
+ // A phone has no room beside the menu, so an open submenu unfolds under its row.
+ ? (
+ <ul className={clsx("dropdown-menu", open && "show")} role="menu" aria-labelledby={titleId(id)}>
+ <MenuLevelContext.Provider value={level + 1}>{open && children}</MenuLevelContext.Provider>
+ </ul>
+ )
+ : open && openSubmenu && menu.layerHost && createPortal((
+ <SubmenuLayer level={level + 1} submenu={openSubmenu} columns={columns}>{children}</SubmenuLayer>
+ ), menu.layerHost)}
+ </li>
+ );
+}
+
+function NestedSubmenu({ icon, title, children, dropStart, onDropdownToggleClicked, disabled, className }: FormDropdownSubmenuProps) {
const [ openOnMobile, setOpenOnMobile ] = useState(false);
return (