Skip to content

Build your personal knowledge base with Trilium Notes

fix(options): open the settings dropdowns as bottom sheets on mobile

Elian Doran3125abad5ffd
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
index a0090f6171..224ebcb721 100644
--- a/apps/client/src/widgets/react/Dropdown.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -29,6 +29,13 @@ vi.mock("./hooks", async (importOriginal) => ({
useTooltip: () => tooltipStub
}));
+// `mobileBottomSheet` only takes effect on a mobile layout, which has to be answerable per test.
+const isMobileMock = vi.hoisted(() => vi.fn(() => false));
+vi.mock("../../services/utils", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../../services/utils")>()),
+ isMobile: () => isMobileMock()
+}));
+
import Dropdown from "./Dropdown";
// happy-dom has no ResizeObserver; the component only needs observe/disconnect to exist.
@@ -63,6 +70,7 @@ function fire(target: EventTarget, eventName: string) {
describe("Dropdown", () => {
beforeEach(() => {
vi.clearAllMocks();
+ isMobileMock.mockReturnValue(false);
instance._menu = null;
});
@@ -139,6 +147,39 @@ describe("Dropdown", () => {
expect(document.body.querySelector(":scope > .my-scope")).toBeNull();
});
+ it("leaves a mobileBottomSheet menu alone on a desktop layout", () => {
+ const el = renderInto(<Dropdown mobileBottomSheet forceShown>item</Dropdown>);
+
+ // Nested next to the toggle, unclassed and undimmed: the sheet is a mobile arrangement only.
+ expect(el.querySelector(".dropdown > .dropdown-menu")).toBeTruthy();
+ expect(el.querySelector(".mobile-bottom-menu")).toBeNull();
+ });
+
+ it("makes a mobileBottomSheet menu a bottom sheet on mobile: classed, portaled out and over the backdrop", () => {
+ isMobileMock.mockReturnValue(true);
+ const cover = document.createElement("div");
+ cover.id = "context-menu-cover";
+ document.body.appendChild(cover);
+
+ const el = renderInto(<Dropdown mobileBottomSheet>item</Dropdown>);
+ const dropdownEl = el.querySelector(".dropdown");
+ void act(() => {
+ $(dropdownEl as HTMLElement).trigger("show.bs.dropdown");
+ });
+
+ // The one prop stands in for all three pieces the arrangement needs.
+ expect(instance._menu?.classList.contains("mobile-bottom-menu")).toBe(true);
+ expect(el.contains(instance._menu)).toBe(false);
+ expect(instance._menu?.closest("body")).toBeTruthy();
+ expect(cover.classList.contains("show")).toBe(true);
+
+ void act(() => {
+ $(dropdownEl as HTMLElement).trigger("hide.bs.dropdown");
+ });
+ expect(cover.classList.contains("show")).toBe(false);
+ cover.remove();
+ });
+
it("mounts and shows a forceShown portaled dropdown immediately", () => {
// No className: the portaled wrapper falls back to an empty class.
renderInto(<Dropdown portalToBody forceShown>item</Dropdown>);
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index 0e0946b5e3..e80e5c8cb8 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -56,9 +56,27 @@ export interface DropdownProps extends Pick<HTMLProps<HTMLDivElement>, "id" | "c
* class keeps applying even though the menu no longer lives inside the toggle's wrapper.
*/
portalToBody?: boolean;
+ /**
+ * On a phone, show the menu 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.
+ *
+ * Prefer this to setting the pieces by hand. A menu left to place itself on mobile lands as a
+ * narrow box adrift in the middle of the page — Popper computes an offset that the app's own
+ * `body.mobile .dropdown-menu { position: fixed }` then measures from somewhere else — and one
+ * opened inside a dialog needs {@link portalToBody} besides, since a transformed `.modal-dialog`
+ * is both the box a fixed menu is placed against and a stacking context the backdrop, painting
+ * above the whole modal, would otherwise dim the menu through.
+ */
+ mobileBottomSheet?: boolean;
}
-export default function Dropdown({ id, className, buttonClassName, isStatic, children, title, text, dropdownContainerStyle, dropdownContainerClassName, dropdownContainerRef: externalContainerRef, hideToggleArrow, iconAction, disabled, noSelectButtonStyle, noDropdownListStyle, forceShown, onShown: externalOnShown, onHidden: externalOnHidden, dropdownOptions, buttonProps, dropdownRef, titlePosition, titleOptions, mobileBackdrop, portalToBody }: DropdownProps) {
+export default function Dropdown({ id, className, buttonClassName, isStatic, children, title, text, dropdownContainerStyle, dropdownContainerClassName, dropdownContainerRef: externalContainerRef, hideToggleArrow, iconAction, disabled, noSelectButtonStyle, noDropdownListStyle, forceShown, onShown: externalOnShown, onHidden: externalOnHidden, dropdownOptions, buttonProps, dropdownRef, titlePosition, titleOptions, mobileBackdrop: mobileBackdropProp, portalToBody: portalToBodyProp, mobileBottomSheet }: DropdownProps) {
+ // The sheet is three things at once — placed by the app's own rule, dimming what is behind it,
+ // and lifted out of whatever opened it — so it is asked for as one thing and unpacked here.
+ const bottomSheet = !!mobileBottomSheet && isMobile();
+ const mobileBackdrop = mobileBackdropProp || bottomSheet;
+ const portalToBody = portalToBodyProp || bottomSheet;
+
const containerRef = useRef<HTMLDivElement | null>(null);
const triggerRef = useRef<HTMLButtonElement | null>(null);
const dropdownContainerRef = useRef<HTMLUListElement | null>(null);
@@ -193,7 +211,7 @@ export default function Dropdown({ id, className, buttonClassName, isStatic, chi
const menu = (
<ul
- class={`dropdown-menu tn-dropdown-menu ${isStatic ? "static" : ""} ${dropdownContainerClassName ?? ""} ${!noDropdownListStyle ? "tn-dropdown-list" : ""}`}
+ class={`dropdown-menu tn-dropdown-menu ${isStatic ? "static" : ""} ${dropdownContainerClassName ?? ""} ${bottomSheet ? "mobile-bottom-menu" : ""} ${!noDropdownListStyle ? "tn-dropdown-list" : ""}`}
style={dropdownContainerStyle}
aria-labelledby={ariaId}
ref={dropdownContainerRef}
diff --git a/apps/client/src/widgets/react/SegmentedChoice.tsx b/apps/client/src/widgets/react/SegmentedChoice.tsx
index 0a21499692..be333fca97 100644
--- a/apps/client/src/widgets/react/SegmentedChoice.tsx
+++ b/apps/client/src/widgets/react/SegmentedChoice.tsx
@@ -119,18 +119,9 @@ 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
+ // This only ever renders on a phone, where a menu is a sheet from the bottom of the
+ // screen rather than a box beside the control it belongs to.
+ mobileBottomSheet
/>
);
}
diff --git a/apps/client/src/widgets/type_widgets/options/appearance.tsx b/apps/client/src/widgets/type_widgets/options/appearance.tsx
index 34e2af3a1a..02e5a98af9 100644
--- a/apps/client/src/widgets/type_widgets/options/appearance.tsx
+++ b/apps/client/src/widgets/type_widgets/options/appearance.tsx
@@ -183,6 +183,7 @@ function UserInterface() {
<span className={currentFamilyIcon} style={{ marginRight: "8px" }} />
{currentFamilyLabel}
</>}
+ mobileBottomSheet
>
{THEME_FAMILIES.map(family => (
<FormListItem
diff --git a/apps/client/src/widgets/type_widgets/options/components/LocaleSelector.tsx b/apps/client/src/widgets/type_widgets/options/components/LocaleSelector.tsx
index 3c5517b481..79270457c3 100644
--- a/apps/client/src/widgets/type_widgets/options/components/LocaleSelector.tsx
+++ b/apps/client/src/widgets/type_widgets/options/components/LocaleSelector.tsx
@@ -16,7 +16,7 @@ export function LocaleSelector({ id, locales, currentValue, onChange, defaultLoc
const currentValueWithDefault = currentValue ?? defaultLocale?.id ?? "";
const { activeLocale, processedLocales } = useProcessedLocales(locales, defaultLocale, currentValueWithDefault);
return (
- <Dropdown id={id} text={activeLocale?.name}>
+ <Dropdown id={id} text={activeLocale?.name} mobileBottomSheet>
{processedLocales.map((locale, index) => (
(typeof locale === "object") ? (
<FormListItem
diff --git a/apps/client/src/widgets/type_widgets/options/text_notes.tsx b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
index 370ec0b13e..03f5014580 100644
--- a/apps/client/src/widgets/type_widgets/options/text_notes.tsx
+++ b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
@@ -421,7 +421,7 @@ function HeadingStyleSelector({ currentValue, onChange }: { currentValue: string
const currentStyle = HEADING_STYLES.find(s => s.value === currentValue) ?? HEADING_STYLES[0];
return (
- <Dropdown text={t(currentStyle.labelKey)}>
+ <Dropdown text={t(currentStyle.labelKey)} mobileBottomSheet>
{HEADING_STYLES.map(({ value, labelKey }) => (
<FormListItem
key={value}