Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile/text): keep a toolbar menu open while the toolbar scrolls

Elian Doran6fce385ae106
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index 4796c33f4a..4dc573de42 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -25,7 +25,8 @@ import Popup, { type PopupProps } from "./Popup";
* `FormDropdownSubmenu`, `FormDropdownDivider`, `FormListHeader` and `FormListCustomItem`.
*/
export interface MenuProps<T> extends Pick<PopupProps, "anchor" | "offset" | "container" | "portalClassName"
- | "backdropClassName" | "style" | "onClick" | "elementRef" | "aria-labelledby" | "placedByStylesheet"> {
+ | "backdropClassName" | "style" | "onClick" | "elementRef" | "aria-labelledby" | "placedByStylesheet"
+ | "keepOpenSelector"> {
id?: string;
className?: string;
/** The side of its anchor it prefers: beside a point towards the right, or below an element. */
diff --git a/apps/client/src/widgets/type_widgets/text/color_menu.tsx b/apps/client/src/widgets/type_widgets/text/color_menu.tsx
index 8dfab99aab..6f03c6ea00 100644
--- a/apps/client/src/widgets/type_widgets/text/color_menu.tsx
+++ b/apps/client/src/widgets/type_widgets/text/color_menu.tsx
@@ -8,7 +8,9 @@ import ColorPicker from "../../react/ColorPicker";
import { FormListCustomItem, FormListHeader } from "../../react/FormList";
import Menu from "../../react/Menu";
import { buildFontColorConfig } from "./color_palette";
-import { isKeyboardPanelOpen, useKeyboardPanel } from "./mobile_editor_toolbar";
+import {
+ isKeyboardPanelOpen, MOBILE_TOOLBAR_ROW, useClosedByToolbarTap, useKeyboardPanel
+} from "./mobile_editor_toolbar";
/** The gap between the button and the menu, as a `Dropdown` leaves under its toggle. */
const ANCHOR_GAP = 2;
@@ -48,6 +50,7 @@ function ColorMenu({ request, onClose }: {
}) {
const bottomSheet = isMobile() && isNarrowLayout();
const { isPanel, replacesPanel } = useKeyboardPanel(bottomSheet);
+ useClosedByToolbarTap(request.anchor, () => onClose(false));
return (
<Menu
@@ -58,6 +61,7 @@ function ColorMenu({ request, onClose }: {
"tn-dropdown-menu", isPanel && "keyboard-panel", replacesPanel && "replaces-panel"
)}
bottomSheet={bottomSheet}
+ keepOpenSelector={MOBILE_TOOLBAR_ROW}
elementRef={request.setElement}
onDismiss={() => onClose(false)}
onClose={() => onClose(true)}
diff --git a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
index 388749046e..00e1205a79 100644
--- a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
+++ b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
@@ -179,6 +179,29 @@ export function useKeyboardPanel(enabled: boolean) {
return { isPanel, replacesPanel };
}
+/** The row of buttons of the phone's toolbar, which scrolls sideways under a menu of it. */
+export const MOBILE_TOOLBAR_ROW = ".classic-toolbar-island .classic-toolbar-widget";
+
+/**
+ * Closes a menu of the toolbar as a tap on another item of {@link MOBILE_TOOLBAR_ROW} clicks, before
+ * the item runs. The menu's `Popup` keeps it open on a press there, which can start a scroll; the
+ * menu's own button, at `anchor`, toggles it in the editor.
+ */
+export function useClosedByToolbarTap(anchor: HTMLElement, close: () => void) {
+ const closeRef = useRef(close);
+ closeRef.current = close;
+
+ useLayoutEffect(() => {
+ const onClick = (event: MouseEvent) => {
+ const target = event.target;
+ if (!(target instanceof Element) || anchor.contains(target)) return;
+ if (target.closest(MOBILE_TOOLBAR_ROW)) closeRef.current();
+ };
+ window.addEventListener("click", onClick, true);
+ return () => window.removeEventListener("click", onClick, true);
+ }, [ anchor ]);
+}
+
/** Whether a menu of the toolbar is in the keyboard's place, for its host to tell the editor. */
export function isKeyboardPanelOpen() {
return document.body.classList.contains("keyboard-panel-open");
diff --git a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx
index 17e719548d..74a261c785 100644
--- a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx
@@ -271,6 +271,37 @@ describe("createToolbarGroupMenuHost", () => {
expect(runs).toEqual([ "iso" ]);
});
+ it("stays open as the phone toolbar is dragged, and closes as a tap on another item clicks", () => {
+ const island = document.createElement("div");
+ island.className = "classic-toolbar-island";
+ const row = document.createElement("div");
+ row.className = "classic-toolbar-widget";
+ const bold = document.createElement("button");
+ const boldClicked = vi.fn();
+ bold.addEventListener("click", boldClicked);
+ row.append(bold);
+ island.append(row);
+ document.body.append(island);
+
+ const { menu, onClose } = show();
+ bold.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
+ expect(menu.isConnected).toBe(true);
+ expect(onClose).not.toHaveBeenCalled();
+
+ bold.click();
+ expect(onClose).toHaveBeenCalledExactlyOnceWith(false);
+ expect(menu.isConnected).toBe(false);
+ expect(boldClicked).toHaveBeenCalledTimes(1);
+
+ // The menu's own button, which toggles it, is left to the editor.
+ row.append(anchor);
+ const toggled = show();
+ anchor.click();
+ expect(toggled.onClose).not.toHaveBeenCalled();
+ document.body.append(anchor);
+ island.remove();
+ });
+
it("returns focus to the group's button on Escape, and not on a press elsewhere", () => {
const escaped = show();
escaped.menu.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }));
diff --git a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx
index 80bda2373b..0682cba7d5 100644
--- a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx
+++ b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx
@@ -9,7 +9,9 @@ import { render } from "preact";
import { escapeHtml, isMobile, isNarrowLayout } from "../../../services/utils";
import { FormDropdownDivider, FormDropdownSubmenu, FormListHeader, FormListItem } from "../../react/FormList";
import Menu, { MenuTile } from "../../react/Menu";
-import { isKeyboardPanelOpen, useKeyboardPanel } from "./mobile_editor_toolbar";
+import {
+ isKeyboardPanelOpen, MOBILE_TOOLBAR_ROW, useClosedByToolbarTap, useKeyboardPanel
+} from "./mobile_editor_toolbar";
/** The gap between the group's button and the menu, as a `Dropdown` leaves under its toggle. */
const ANCHOR_GAP = 2;
@@ -49,6 +51,7 @@ function ToolbarGroupMenu({ request, onClose }: {
}) {
const bottomSheet = isMobile() && isNarrowLayout();
const { isPanel, replacesPanel } = useKeyboardPanel(bottomSheet);
+ useClosedByToolbarTap(request.anchor, () => onClose(false));
return (
<Menu
@@ -59,6 +62,7 @@ function ToolbarGroupMenu({ request, onClose }: {
"tn-dropdown-menu", isPanel && "keyboard-panel", replacesPanel && "replaces-panel"
)}
bottomSheet={bottomSheet}
+ keepOpenSelector={MOBILE_TOOLBAR_ROW}
aria-labelledby={request.anchor.getAttribute("aria-labelledby") ?? undefined}
elementRef={request.setElement}
onDismiss={() => onClose(false)}