Skip to content

Build your personal knowledge base with Trilium Notes

fix(dropdown): open a tablet's mobile menus beside their toggle, capped and scrolling (closes #11106)

Elian Doranaaff353ccfb6
diff --git a/apps/client/src/stylesheets/theme-next/base.css b/apps/client/src/stylesheets/theme-next/base.css
index 9183a203d0..9aa8188b95 100644
--- a/apps/client/src/stylesheets/theme-next/base.css
+++ b/apps/client/src/stylesheets/theme-next/base.css
@@ -431,11 +431,24 @@ body.mobile #context-menu-cover {
}
}
-body.mobile .dropdown-menu.mobile-bottom-menu,
-body.mobile .tn-dropdown-portal.global-menu .dropdown-menu {
+body.mobile .dropdown-menu.mobile-bottom-menu {
border-radius: var(--dropdown-border-radius) var(--dropdown-border-radius) 0 0;
}
+/* Pinned to the bottom like a sheet only in the phone layout; see style.css. */
+@media (max-width: 991px) {
+ body.mobile .tn-dropdown-portal.global-menu .dropdown-menu {
+ border-radius: var(--dropdown-border-radius) var(--dropdown-border-radius) 0 0;
+ }
+}
+
+/* Beside its toggle in a tablet's wider mobile layout, wide enough for its rows to be tapped. */
+@media (min-width: 992px) {
+ body.mobile .tn-popup.tn-menu {
+ min-width: 260px;
+ }
+}
+
body.mobile .dropdown-menu {
--dropdown-menu-padding-vertical: 0.7em;
--dropdown-menu-padding-horizontal: 1em;
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
index 17e5a0fbae..269abfd37f 100644
--- a/apps/client/src/widgets/react/Dropdown.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -9,7 +9,7 @@ import { FormDropdownSubmenu, FormListItem } from "./FormList";
const focusTraps = vi.hoisted(() => ({ suspend: vi.fn(() => () => {}) }));
vi.mock("./modal_focustrap", () => ({ suspendModalFocusTraps: focusTraps.suspend }));
-const layout = vi.hoisted(() => ({ onMobile: false }));
+const layout = vi.hoisted(() => ({ onMobile: false, narrow: true }));
vi.mock("../../services/utils", async (importOriginal) => ({
...(await importOriginal<typeof import("../../services/utils")>()),
isMobile: () => layout.onMobile
@@ -24,12 +24,17 @@ describe("Dropdown", () => {
vi.spyOn(document.documentElement, "clientHeight", "get").mockReturnValue(800);
vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockReturnValue(200);
vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(100);
+ // A phone's width unless a spec says otherwise; happy-dom's window is 1024px wide.
+ vi.spyOn(window, "matchMedia").mockImplementation((media) => ({
+ matches: layout.narrow, media, addEventListener() {}, removeEventListener() {}
+ }) as unknown as MediaQueryList);
});
afterEach(() => {
render(null, host);
vi.restoreAllMocks();
layout.onMobile = false;
+ layout.narrow = true;
});
/** Renders a `Dropdown`, or a `DropdownPanel` for `panel`. */
@@ -400,6 +405,21 @@ describe("Dropdown", () => {
cover.remove();
});
+ it("opens beside its toggle, capped, on a tablet's wider mobile layout", async () => {
+ layout.onMobile = true;
+ layout.narrow = false;
+ const cover = addCover();
+ const { toggle } = renderDropdown({ mobileBottomSheet: true });
+
+ click(toggle);
+ await vi.waitFor(() => expect(popup()?.style.visibility).toBe("visible"));
+ expect(popup()?.classList.contains("mobile-bottom-menu")).toBe(false);
+ expect(popup()?.style.top).toBe("82px");
+ expect(popup()?.style.maxHeight).not.toBe("");
+ expect([ ...cover.classList ]).toEqual([ "show", "global-menu-cover" ]);
+ cover.remove();
+ });
+
it("is an ordinary menu beside its toggle on a desktop, with no cover", async () => {
const cover = addCover();
const { toggle } = renderDropdown({ mobileBottomSheet: true, mobileBackdrop: true });
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index e9183ef029..12a3579ca8 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -47,7 +47,8 @@ export interface DropdownProps extends Pick<HTMLProps<HTMLDivElement>, "id" | "c
mobileBackdrop?: boolean;
/**
* On a phone, shows the popup as a sheet rising from the bottom of the screen over a dimmed
- * page, the way the app's other mobile menus appear. No effect on a desktop layout.
+ * page, the way the app's other mobile menus appear. On a tablet's wider mobile layout, the
+ * popup opens beside its toggle over the dimmed page instead. No effect on a desktop layout.
*/
mobileBottomSheet?: boolean;
/**
@@ -74,6 +75,9 @@ 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;
@@ -158,9 +162,11 @@ 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.
- const bottomSheet = !!mobileBottomSheet && isMobile();
- const mobileBackdrop = (!!mobileBackdropProp || bottomSheet) && isMobile();
+ // 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;
+ const mobileBackdrop = (!!mobileBackdropProp || !!mobileBottomSheet) && isMobile();
const containerRef = useRef<HTMLDivElement | null>(null);
const triggerRef = useRef<HTMLButtonElement | null>(null);
const popupRef = useRef<HTMLDivElement | null>(null);
diff --git a/apps/client/src/widgets/react/SegmentedChoice.spec.tsx b/apps/client/src/widgets/react/SegmentedChoice.spec.tsx
index 38def609df..78bed7917c 100644
--- a/apps/client/src/widgets/react/SegmentedChoice.spec.tsx
+++ b/apps/client/src/widgets/react/SegmentedChoice.spec.tsx
@@ -160,6 +160,9 @@ describe("SegmentedChoice", () => {
it("comes up from the bottom of the screen, out of the dialog and over a backdrop", () => {
isMobileMock.mockReturnValue(true);
+ // A phone's width; happy-dom's window is 1024px wide, a tablet's.
+ const narrow = vi.spyOn(window, "matchMedia")
+ .mockReturnValue({ matches: true } as MediaQueryList);
renderOpened(<SegmentedChoice options={iconOptions} currentValue="dark" onChange={() => {}} collapseOnMobile />);
// Placed by the app's own bottom-sheet rule rather than by Popper, which mobile's forced
@@ -171,6 +174,7 @@ describe("SegmentedChoice", () => {
// context the backdrop would then cover the menu through.
expect(container.contains(menu)).toBe(false);
expect(document.getElementById("context-menu-cover")?.classList.contains("show")).toBe(true);
+ narrow.mockRestore();
});
it("shows a count beside the name rather than as a badge, the menu line having room for it", () => {