Skip to content

Build your personal knowledge base with Trilium Notes

fix(options): bring the collapsed choice up from the bottom on mobile

Elian Doranb88ebae077c3
diff --git a/apps/client/src/widgets/react/SegmentedChoice.spec.tsx b/apps/client/src/widgets/react/SegmentedChoice.spec.tsx
index 8640541dea..88d46067ce 100644
--- a/apps/client/src/widgets/react/SegmentedChoice.spec.tsx
+++ b/apps/client/src/widgets/react/SegmentedChoice.spec.tsx
@@ -37,15 +37,22 @@ globalThis.ResizeObserver = globalThis.ResizeObserver ?? (ResizeObserverStub as
describe("SegmentedChoice", () => {
let container: HTMLDivElement;
+ let cover: HTMLDivElement;
+
beforeEach(() => {
isMobileMock.mockReturnValue(false);
container = document.createElement("div");
document.body.appendChild(container);
+ // The backdrop the dropdown reaches for by id; part of `index.html` rather than of any widget.
+ cover = document.createElement("div");
+ cover.id = "context-menu-cover";
+ document.body.appendChild(cover);
});
afterEach(() => {
render(null, container);
container.remove();
+ cover.remove();
});
const options = (extra?: { count?: number }) => [
@@ -109,7 +116,7 @@ describe("SegmentedChoice", () => {
isMobileMock.mockReturnValue(true);
renderOpened(<SegmentedChoice options={options()} currentValue="user" onChange={() => {}} />);
- expect(container.querySelectorAll(".dropdown-item")).toHaveLength(0);
+ expect(menuItems()).toHaveLength(0);
expect(container.querySelectorAll("button")).toHaveLength(2);
});
@@ -123,7 +130,7 @@ describe("SegmentedChoice", () => {
expect(toggle?.textContent).toContain("Dark");
expect(toggle?.querySelector(".bx-moon")).not.toBeNull();
- const items = [ ...container.querySelectorAll(".dropdown-item") ];
+ const items = menuItems();
expect(items.map((item) => item.textContent?.trim())).toStrictEqual([ "Light", "Dark" ]);
// The options carry icons, so the current one is highlighted rather than having its icon
// replaced by a check mark.
@@ -143,19 +150,34 @@ describe("SegmentedChoice", () => {
onChange={() => {}}
collapseOnMobile
/>);
- expect([ ...container.querySelectorAll(".dropdown-item") ].map((item) => item.textContent?.trim()))
+ expect(menuItems().map((item) => item.textContent?.trim()))
.toStrictEqual([ "Square", "Round" ]);
renderOpened(<SegmentedChoice options={options()} currentValue="system" onChange={() => {}} collapseOnMobile />);
- const items = [ ...container.querySelectorAll(".dropdown-item") ];
+ const items = menuItems();
expect(items[1].querySelector(".bx-check")).not.toBeNull();
});
+ it("comes up from the bottom of the screen, out of the dialog and over a backdrop", () => {
+ isMobileMock.mockReturnValue(true);
+ 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
+ // `position: fixed` leaves computing an offset for a menu positioned some other way.
+ const menu = document.querySelector(".dropdown-menu.mobile-bottom-menu");
+ expect(menu).not.toBeNull();
+ // Portaled out: inside the settings dialog it would be trapped in the transformed
+ // `.modal-dialog`, which is a containing block for `position: fixed` and a stacking
+ // 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);
+ });
+
it("shows a count beside the name rather than as a badge, the menu line having room for it", () => {
isMobileMock.mockReturnValue(true);
renderOpened(<SegmentedChoice options={options({ count: 3 })} currentValue="user" onChange={() => {}} collapseOnMobile />);
- const items = [ ...container.querySelectorAll(".dropdown-item") ];
+ const items = menuItems();
expect(items.map((item) => item.querySelector("small")?.textContent)).toStrictEqual([ "3", "7" ]);
expect(container.querySelectorAll(".tn-segmented-choice-count")).toHaveLength(0);
});
@@ -169,4 +191,9 @@ describe("SegmentedChoice", () => {
if (dropdown) $(dropdown).trigger("show.bs.dropdown");
});
}
+
+ /** Queried from the document rather than the container: the menu is portaled out to `body`. */
+ function menuItems() {
+ return [ ...document.querySelectorAll(".dropdown-item") ];
+ }
});
diff --git a/apps/client/src/widgets/react/SegmentedChoice.tsx b/apps/client/src/widgets/react/SegmentedChoice.tsx
index b8c084f9bb..0a21499692 100644
--- a/apps/client/src/widgets/react/SegmentedChoice.tsx
+++ b/apps/client/src/widgets/react/SegmentedChoice.tsx
@@ -119,6 +119,18 @@ function CollapsedChoice<T extends string>({ options, currentValue, onChange, cl
disabled={disabled}
// A handful of options, so the menu never scrolls — see the prop's own note.
noDropdownListStyle
+ // The menu comes up from the bottom of the screen, as every other mobile menu does. Not
+ // only a matter of looks: left to place itself it lands as a narrow box adrift in the
+ // middle of the page, Popper having computed an offset that the app's own
+ // `body.mobile .dropdown-menu { position: fixed }` then measures from somewhere else.
+ dropdownContainerClassName="mobile-bottom-menu"
+ mobileBackdrop
+ // Out of whatever opened it, for the settings dialog it usually stands in: that dialog's
+ // `.modal-dialog` is transformed (the master-detail slide), which makes it the containing
+ // block a fixed menu is placed against *and* a stacking context the menu's own z-index
+ // cannot escape — so the backdrop, being a `<body>` child above the whole modal, would
+ // otherwise dim the menu along with the page under it.
+ portalToBody
/>
);
}