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
feat(menu): show the context menu as a bottom sheet on a phone
Elian Doran4bda19af563b
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex 9f5e88eca4..17a3c9da91 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -7,10 +7,15 @@ vi.mock("../services/keyboard_actions", () => ({})}));const layout = vi.hoisted(() => ({ onMobile: false }));-vi.mock("../services/utils", async (importOriginal) => ({- ...(await importOriginal<typeof import("../services/utils")>()),- isMobile: () => layout.onMobile-}));+vi.mock("../services/utils", async (importOriginal) => {+ const original = await importOriginal<typeof import("../services/utils")>();+ // `Menu` imports `isMobile()` by name, `contextMenu` through the default export.+ return {+ ...original,+ isMobile: () => layout.onMobile,+ default: { ...original.default, isMobile: () => layout.onMobile }+ };+});// Key names are translated; the formatting has specs of its own.vi.mock("../services/keyboard_shortcut_display", () => ({formatShortcut: (shortcut: string) => shortcut.split("+"),@@ -507,6 +512,39 @@ describe("contextMenu", () => {});});+ describe("on a phone", () => {+ afterEach(() => {+ layout.onMobile = false;+ });++ it("rises from the bottom as a sheet, which the stylesheet places and caps", async () => {+ layout.onMobile = true;+ buildPage();+ const contextMenu = await buildContextMenu();++ await contextMenu.show({ x: 10, y: 20, items, selectMenuItemHandler: () => {} });++ const menu = menuElement();+ expect(menu?.classList.contains("mobile-bottom-menu")).toBe(true);+ await vi.waitFor(() => expect(menu?.style.visibility).toBe("visible"));+ // Nothing inline for the sheet's rules to contend with: its `max-height` is not `!important`.+ expect([ menu?.style.left, menu?.style.top, menu?.style.maxHeight ]).toEqual([ "", "", "" ]);+ });++ it("opens at the pointer instead when the caller asks", async () => {+ layout.onMobile = true;+ buildPage();+ const contextMenu = await buildContextMenu();++ await contextMenu.show({ x: 10, y: 20, items, selectMenuItemHandler: () => {}, forcePositionOnMobile: true });++ const menu = menuElement();+ expect(menu?.classList.contains("mobile-bottom-menu")).toBe(false);+ await vi.waitFor(() => expect(menu?.style.visibility).toBe("visible"));+ expect(menu?.style.left).not.toBe("");+ });+ });+describe("placement", () => {/** Opens the menu with the size a browser would lay it out at; happy-dom lays out nothing. */async function place(options: { x: number, y: number, width: number, height: number, orientation?: "left" }) {diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.tsindex 6d96d780cc..713a4860aa 100644--- a/apps/client/src/menus/context_menu.ts+++ b/apps/client/src/menus/context_menu.ts@@ -133,6 +133,7 @@ class ContextMenu {x: options.x,y: options.y,orientation: options.orientation,+ bottomSheet: utils.isMobile() && !options.forcePositionOnMobile,items: options.items,onSelect: (item, e) => {// A submenu's row stays up to be opened, unless it runs something of its own.diff --git a/apps/client/src/menus/context_menu_legacy.ts b/apps/client/src/menus/context_menu_legacy.tsindex b3e79fcadf..f4cf45febf 100644--- a/apps/client/src/menus/context_menu_legacy.ts+++ b/apps/client/src/menus/context_menu_legacy.ts@@ -6,7 +6,6 @@ import { Tooltip } from "bootstrap";import { JSX } from "preact";import note_tooltip from "../services/note_tooltip.js";-import utils from "../services/utils.js";export interface ContextMenuOptions<T> {x: number;@@ -82,17 +81,10 @@ export type ContextMenuEvent = PointerEvent | MouseEvent | JQuery.ContextMenuEveclass ContextMenu {private $widget: JQuery<HTMLElement>;- private $cover?: JQuery<HTMLElement>;private options?: ContextMenuOptions<any>;- private isMobile: boolean;constructor() {this.$widget = $("#context-menu-container");- this.isMobile = utils.isMobile();-- if (this.isMobile) {- this.$cover = $("#context-menu-cover");- }}async show<T>(options: ContextMenuOptions<T>) {@@ -107,8 +99,6 @@ class ContextMenu {await this.hide();}- this.$widget.toggleClass("mobile-bottom-menu", !this.options.forcePositionOnMobile);- this.$cover?.addClass("show");$("body").addClass("context-menu-shown");this.$widget.empty();@@ -145,7 +135,6 @@ class ContextMenu {async hide() {this.options?.onHide?.();this.$widget.removeClass("show");- this.$cover?.removeClass("show");$("body").removeClass("context-menu-shown");this.$widget.hide();}diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 2ada6933d5..1ce536e5ef 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -23,6 +23,11 @@ export interface MenuProps<T> {y: number;/** Opens the menu towards the left of {@link x} instead of towards the right. */orientation?: "left";+ /**+ * Shows the menu as a sheet along the bottom of a phone's screen instead of at {@link x} and+ * {@link y}. The `.mobile-bottom-menu` rules place and cap it.+ */+ bottomSheet?: boolean;items: MenuItem<T>[];/** Called when an item is pressed with the primary button. */onSelect(item: MenuCommandItem<T>, e: MouseEvent): void;@@ -54,7 +59,7 @@ interface MenuState<T> {openItems: MenuCommandItem<T>[];}-export default function Menu<T>({ id, className, x, y, orientation, items, onSelect, onClose }: MenuProps<T>) {+export default function Menu<T>({ id, className, x, y, orientation, bottomSheet, items, onSelect, onClose }: MenuProps<T>) {const menuRef = useRef<HTMLDivElement>(null);const [ submenus, setSubmenus ] = useState<OpenSubmenu<T>[]>([]);const nextKey = useRef(0);@@ -71,15 +76,23 @@ export default function Menu<T>({ id, className, x, y, orientation, items, onSeluseLayoutEffect(() => {const menu = menuRef.current;if (!menu) return;+ if (bottomSheet) {+ // An inline `max-height` would override the sheet's own, which is not `!important`.+ menu.style.visibility = "visible";+ return;+ }const anchor = pointAt(x, y);const placement = orientation === "left" ? "left-start" : "right-start";// Places the menu now, and again whenever the viewport or the menu itself changes size.return autoUpdate(anchor, menu, () => void placeMenu(menu, anchor, placement));- }, [ x, y, orientation ]);+ }, [ x, y, orientation, bottomSheet ]);return (- <div ref={menuRef} id={id} className={clsx("dropdown-menu show tn-menu", className)} role="menu">+ <div+ ref={menuRef} id={id} role="menu"+ className={clsx("dropdown-menu show tn-menu", bottomSheet && "mobile-bottom-menu", className)}+ ><MenuList level={0} items={items} state={state} />{/* Inside the menu, so the rules scoped to it apply, but none inside another: a fixedlayer escapes a scrolling menu only while no ancestor carries a filter. */}