Skip to content

Build your personal knowledge base with Trilium Notes

fix(menu): open a tablet's context menu at the press instead of as a sheet

Elian Doran26d5301e9bdd
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 18333d7b32..1711c1030f 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -7,15 +7,12 @@ vi.mock("../services/keyboard_actions", () => ({
effectiveShortcuts: name === "copyNotesToClipboard" ? [ "Ctrl+C", "Ctrl+Insert" ] : []
})
}));
-const layout = vi.hoisted(() => ({ onMobile: false }));
+const layout = vi.hoisted(() => ({ onMobile: false, narrow: true }));
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 }
- };
+ const overrides = { isMobile: () => layout.onMobile, isNarrowLayout: () => layout.narrow };
+ return { ...original, ...overrides, default: { ...original.default, ...overrides } };
});
const focusTraps = vi.hoisted(() => ({ restore: vi.fn(), suspend: vi.fn() }));
vi.mock("../widgets/react/modal_focustrap", () => ({
@@ -1069,6 +1066,7 @@ describe("contextMenu", () => {
describe("on a phone", () => {
afterEach(() => {
layout.onMobile = false;
+ layout.narrow = true;
});
it("rises from the bottom as a sheet, which the stylesheet places and caps", async () => {
@@ -1085,6 +1083,21 @@ describe("contextMenu", () => {
expect([ menu?.style.left, menu?.style.top, menu?.style.maxHeight ]).toEqual([ "", "", "" ]);
});
+ it("opens at the press on a tablet's wider mobile layout, capped", async () => {
+ layout.onMobile = true;
+ layout.narrow = false;
+ 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(false);
+ await vi.waitFor(() => expect(menu?.style.visibility).toBe("visible"));
+ expect([ menu?.style.left, menu?.style.top ]).not.toEqual([ "", "" ]);
+ expect(menu?.style.maxHeight).not.toBe("");
+ });
+
it("opens at the pointer instead when the caller asks", async () => {
layout.onMobile = true;
buildPage();
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.ts
index a1437435d5..2f93f93f2c 100644
--- a/apps/client/src/menus/context_menu.ts
+++ b/apps/client/src/menus/context_menu.ts
@@ -157,7 +157,8 @@ class ContextMenu {
placement: options.orientation === "left" ? "left-start" : "right-start",
// Already where the screen shows it: see the host above.
container: this.host,
- bottomSheet: utils.isMobile() && !options.forcePositionOnMobile,
+ bottomSheet: utils.isMobile() && utils.isNarrowLayout()
+ && !options.forcePositionOnMobile,
// As a native menu opened from the keyboard, it starts at its first row.
startAt: this.lastInputWasKey ? "first" : undefined,
items: options.items,
diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts
index 846438aedb..b6a4edf5de 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -308,6 +308,15 @@ export function isMobile() {
);
}
+/**
+ * Whether the viewport is as narrow as a phone's. In the mobile layout, a menu is a bottom sheet
+ * only then; a tablet's wider layout places it beside what opened it. Must match the media query
+ * around the `.mobile-bottom-menu` rules in style.css.
+ */
+export function isNarrowLayout() {
+ return window.matchMedia("(max-width: 991px)").matches;
+}
+
/**
* Returns true if the client device is an Apple iOS one (iPad, iPhone, iPod).
* Does not check if the user requested the mobile or desktop layout, use {@link isMobile} for that.
@@ -980,6 +989,7 @@ export default {
toObject,
randomString,
isMobile,
+ isNarrowLayout,
isDesktop,
setCookie,
getNoteTypeClass,
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index 12a3579ca8..1d8bc60916 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -5,7 +5,7 @@ import { ComponentChildren, HTMLAttributes } from "preact";
import { CSSProperties, HTMLProps } from "preact/compat";
import { MutableRef, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
-import { isMobile } from "../../services/utils";
+import { isMobile, isNarrowLayout } from "../../services/utils";
import { focusListItem } from "./FormList";
import { useTooltip, useUniqueName } from "./hooks";
import { suspendModalFocusTraps } from "./modal_focustrap";
@@ -75,9 +75,6 @@ export interface DropdownHandle {
toggle(): void;
}
-/** The phone layout. Must match the media query around `.mobile-bottom-menu` in style.css. */
-const NARROW_LAYOUT = "(max-width: 991px)";
-
/** The gap, in pixels, between the toggle and its popup, as Bootstrap's dropdowns kept. */
const TOGGLE_GAP = 2;
@@ -162,10 +159,8 @@ function DropdownToggle({ id, className, buttonClassName, title, text, dropdownC
popup(slot: PopupSlot): ComponentChildren;
}) {
// The sheet is placed by the app's own rule and dims what is behind it, so it is asked for as
- // one thing and unpacked here. The `.mobile-bottom-menu` rules apply only below
- // `NARROW_LAYOUT`, so a wider mobile layout places the popup beside its toggle.
- const bottomSheet = !!mobileBottomSheet && isMobile()
- && window.matchMedia(NARROW_LAYOUT).matches;
+ // one thing and unpacked here.
+ const bottomSheet = !!mobileBottomSheet && isMobile() && isNarrowLayout();
const mobileBackdrop = (!!mobileBackdropProp || !!mobileBottomSheet) && isMobile();
const containerRef = useRef<HTMLDivElement | null>(null);
const triggerRef = useRef<HTMLButtonElement | null>(null);