Skip to content

Build your personal knowledge base with Trilium Notes

client: add initial mobile support for the contextual help component

Adorian Doranf377e50d3e9e
diff --git a/apps/client/src/widgets/react/ContextualHelp.css b/apps/client/src/widgets/react/ContextualHelp.css
index 2c512c785e..ff7c7a53ce 100644
--- a/apps/client/src/widgets/react/ContextualHelp.css
+++ b/apps/client/src/widgets/react/ContextualHelp.css
@@ -10,3 +10,21 @@
.contextual-help:hover {
opacity: 1;
}
+
+/*
+ * The sheet the explanation rises in on a phone. Every modal is already a bottom sheet there
+ * (see `body.mobile .modal-dialog` in style.css), so all this does is take away the chrome a single
+ * remark has no use for: the header is left holding nothing but the button that dismisses it.
+ */
+.modal.contextual-help-sheet {
+ .modal-header {
+ border-bottom: none;
+ padding-bottom: 0;
+ }
+
+ .modal-body {
+ padding-top: 0.5em;
+ /* The sheet sits on the bottom edge, where a gesture bar would otherwise cross the text. */
+ padding-bottom: max(env(safe-area-inset-bottom), 1rem);
+ }
+}
diff --git a/apps/client/src/widgets/react/ContextualHelp.spec.tsx b/apps/client/src/widgets/react/ContextualHelp.spec.tsx
index 17daf086ae..c10ba6b69b 100644
--- a/apps/client/src/widgets/react/ContextualHelp.spec.tsx
+++ b/apps/client/src/widgets/react/ContextualHelp.spec.tsx
@@ -1,13 +1,31 @@
-import { render } from "preact";
+import { type ComponentChildren, render } from "preact";
import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
// The tooltip itself is Bootstrap's; what this component decides is the configuration it hands over,
// so the hook is stubbed and the configuration asserted on.
-const useStaticTooltip = vi.hoisted(() => vi.fn());
+const mocks = vi.hoisted(() => ({
+ useStaticTooltip: vi.fn(),
+ /** Which affordance the component is built for; read once, when its module is first evaluated. */
+ isMobile: vi.fn(() => false)
+}));
+
vi.mock("./hooks", async (importOriginal) => ({
...(await importOriginal<typeof import("./hooks")>()),
- useStaticTooltip
+ useStaticTooltip: mocks.useStaticTooltip
+}));
+
+vi.mock("../../services/utils", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../../services/utils")>()),
+ isMobile: mocks.isMobile
+}));
+
+// The sheet is the app's own modal, which on a phone every dialog already is; what matters here is
+// that the message is handed to one, and only once the icon has been tapped.
+vi.mock("./Modal", () => ({
+ default: ({ children, show }: { children: ComponentChildren, show: boolean }) => (
+ show ? <div className="sheet-stub">{children}</div> : null
+ )
}));
import ContextualHelp from "./ContextualHelp";
@@ -34,7 +52,7 @@ describe("ContextualHelp", () => {
// No title of its own: a native tooltip would open somewhere else, in its own styling.
expect(span?.getAttribute("title")).toBeNull();
- expect(useStaticTooltip).toHaveBeenLastCalledWith(expect.anything(), {
+ expect(mocks.useStaticTooltip).toHaveBeenLastCalledWith(expect.anything(), {
title: "What this figure covers.",
placement: "bottom",
customClass: "tooltip-top"
@@ -43,15 +61,41 @@ describe("ContextualHelp", () => {
it("hands the tooltip the same configuration until the message itself changes", () => {
act(() => render(<ContextualHelp helpMessage="First." />, container));
- const [ , first ] = useStaticTooltip.mock.calls.at(-1) ?? [];
+ const [ , first ] = mocks.useStaticTooltip.mock.calls.at(-1) ?? [];
// A re-render with the same message must not tear the tooltip down and build it again:
// the hook rebuilds on any new configuration object.
act(() => render(<ContextualHelp helpMessage="First." />, container));
- expect(useStaticTooltip.mock.calls.at(-1)?.[1]).toBe(first);
+ expect(mocks.useStaticTooltip.mock.calls.at(-1)?.[1]).toBe(first);
act(() => render(<ContextualHelp helpMessage="Second." />, container));
- expect(useStaticTooltip.mock.calls.at(-1)?.[1]).not.toBe(first);
- expect(useStaticTooltip.mock.calls.at(-1)?.[1]).toMatchObject({ title: "Second." });
+ expect(mocks.useStaticTooltip.mock.calls.at(-1)?.[1]).not.toBe(first);
+ expect(mocks.useStaticTooltip.mock.calls.at(-1)?.[1]).toMatchObject({ title: "Second." });
+ });
+
+ it("opens the message in a sheet on tap instead, where nothing hovers", async () => {
+ const OnMobile = await loadOnMobile();
+ act(() => render(<OnMobile helpMessage="What this figure covers." />, container));
+
+ // The same affordance, and no tooltip behind it: a phone would never show one.
+ const span = container.querySelector("span");
+ expect(span?.className).toBe("bx bx-info-circle contextual-help");
+ expect(mocks.useStaticTooltip).not.toHaveBeenCalled();
+ expect(document.body.querySelector(".sheet-stub")).toBeNull();
+
+ act(() => { span?.dispatchEvent(new MouseEvent("click", { bubbles: true })); });
+ // Out at the body rather than beside the icon: where the icon sits — a chart, a cell, a
+ // scroll container — is exactly what must not lay the sheet out or clip it.
+ const sheet = document.body.querySelector(".sheet-stub");
+ expect(sheet?.textContent).toBe("What this figure covers.");
+ expect(container.contains(sheet)).toBe(false);
});
});
+
+/** The component as a phone loads it — the layout is read as its module is evaluated, once. */
+async function loadOnMobile() {
+ mocks.isMobile.mockReturnValue(true);
+ vi.resetModules();
+
+ return (await import("./ContextualHelp")).default;
+}
diff --git a/apps/client/src/widgets/react/ContextualHelp.tsx b/apps/client/src/widgets/react/ContextualHelp.tsx
index f20c008f86..6a8b1e7503 100644
--- a/apps/client/src/widgets/react/ContextualHelp.tsx
+++ b/apps/client/src/widgets/react/ContextualHelp.tsx
@@ -1,11 +1,14 @@
import "./ContextualHelp.css";
-import { useMemo, useRef } from "preact/hooks";
+import { createPortal } from "preact/compat";
+import { useMemo, useRef, useState } from "preact/hooks";
+import { isMobile } from "../../services/utils";
import { useStaticTooltip } from "./hooks";
+import Modal from "./Modal";
interface ContextualHelpProps {
- /** What the icon explains, shown on hover. */
+ /** What the icon explains, shown on hover or — on a phone — on tap. */
helpMessage: string;
}
@@ -19,6 +22,16 @@ interface ContextualHelpProps {
* reach for `HelpTooltipButton` instead — this one is the passing remark.
*/
export default function ContextualHelp({ helpMessage }: ContextualHelpProps) {
+ return IS_MOBILE
+ ? <TappedHelp helpMessage={helpMessage} />
+ : <HoveredHelp helpMessage={helpMessage} />;
+}
+
+/** Which affordance the icon carries, read once: the layout it answers to holds for the session. */
+const IS_MOBILE = isMobile();
+
+/** The pointer's: the explanation is a hover away, and the icon itself does nothing. */
+function HoveredHelp({ helpMessage }: ContextualHelpProps) {
const ref = useRef<HTMLSpanElement>(null);
useStaticTooltip(ref, useMemo(() => ({
@@ -32,3 +45,38 @@ export default function ContextualHelp({ helpMessage }: ContextualHelpProps) {
return <span ref={ref} className="bx bx-info-circle contextual-help" />;
}
+
+/**
+ * The thumb's: nothing hovers on a phone, so the explanation is a tap away — in the sheet that
+ * rises from the bottom of the screen, which is where this app already puts what a thumb opens.
+ *
+ * Portalled to the body, since the icon sits wherever it explains something — inside a chart, a
+ * table cell, a scroll container — and a sheet left there is laid out and clipped by that, rather
+ * than by the screen it is supposed to rise from. Stackable for the same reason of place: the icon
+ * is usually inside a dialog of its own (the settings pages, above all), and opening the sheet must
+ * not take that dialog down with it.
+ */
+function TappedHelp({ helpMessage }: ContextualHelpProps) {
+ const [ shown, setShown ] = useState(false);
+
+ return (
+ <>
+ <span
+ className="bx bx-info-circle contextual-help"
+ onClick={() => setShown(true)}
+ />
+
+ {createPortal((
+ <Modal
+ className="contextual-help-sheet"
+ size="md"
+ show={shown}
+ onHidden={() => setShown(false)}
+ stackable
+ >
+ {helpMessage}
+ </Modal>
+ ), document.body)}
+ </>
+ );
+}