Skip to content

Build your personal knowledge base with Trilium Notes

client: create a contextual help component

Adorian Doran2250dc05f694
diff --git a/apps/client/src/widgets/react/ContextualHelp.css b/apps/client/src/widgets/react/ContextualHelp.css
new file mode 100644
index 0000000000..2c512c785e
--- /dev/null
+++ b/apps/client/src/widgets/react/ContextualHelp.css
@@ -0,0 +1,12 @@
+/* Small, dimmed and asking to be hovered: present without competing with what it explains. */
+.contextual-help {
+ margin-inline-start: 0.25em;
+ color: var(--contextual-help-icon-color);
+ font-size: 0.75rem;
+ opacity: 0.75;
+ cursor: help;
+}
+
+.contextual-help:hover {
+ opacity: 1;
+}
diff --git a/apps/client/src/widgets/react/ContextualHelp.spec.tsx b/apps/client/src/widgets/react/ContextualHelp.spec.tsx
new file mode 100644
index 0000000000..17daf086ae
--- /dev/null
+++ b/apps/client/src/widgets/react/ContextualHelp.spec.tsx
@@ -0,0 +1,57 @@
+import { 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());
+vi.mock("./hooks", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("./hooks")>()),
+ useStaticTooltip
+}));
+
+import ContextualHelp from "./ContextualHelp";
+
+describe("ContextualHelp", () => {
+ let container: HTMLElement;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ render(null, container);
+ container.remove();
+ });
+
+ it("is an info icon explaining itself through the app's tooltip, raised above any dialog", () => {
+ act(() => render(<ContextualHelp helpMessage="What this figure covers." />, container));
+
+ const span = container.querySelector("span");
+ expect(span?.className).toBe("bx bx-info-circle contextual-help");
+ // 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(), {
+ title: "What this figure covers.",
+ placement: "bottom",
+ customClass: "tooltip-top"
+ });
+ });
+
+ 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) ?? [];
+
+ // 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);
+
+ 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." });
+ });
+});
diff --git a/apps/client/src/widgets/react/ContextualHelp.tsx b/apps/client/src/widgets/react/ContextualHelp.tsx
new file mode 100644
index 0000000000..f20c008f86
--- /dev/null
+++ b/apps/client/src/widgets/react/ContextualHelp.tsx
@@ -0,0 +1,34 @@
+import "./ContextualHelp.css";
+
+import { useMemo, useRef } from "preact/hooks";
+
+import { useStaticTooltip } from "./hooks";
+
+interface ContextualHelpProps {
+ /** What the icon explains, shown on hover. */
+ helpMessage: string;
+}
+
+/**
+ * The small info affordance that sits beside a label or a figure and explains it on hover — for a
+ * remark too slight to be spelled out in the interface itself, and which nothing is lost by missing.
+ *
+ * The explanation goes on the app's tooltip rather than the browser's: a native one would appear
+ * somewhere else, after its own delay and in its own styling, while whatever this icon sits beside
+ * already carries the app's. Where a subject warrants a paragraph, markup or a help page of its own,
+ * reach for `HelpTooltipButton` instead — this one is the passing remark.
+ */
+export default function ContextualHelp({ helpMessage }: ContextualHelpProps) {
+ const ref = useRef<HTMLSpanElement>(null);
+
+ useStaticTooltip(ref, useMemo(() => ({
+ title: helpMessage,
+ placement: "bottom" as const,
+ // Bootstrap appends tooltips to `<body>`, where the base `.tooltip` z-index sits below a
+ // modal — and these icons are often shown inside the settings dialog, so without this the
+ // hint would open behind the dialog that triggered it.
+ customClass: "tooltip-top"
+ }), [ helpMessage ]));
+
+ return <span ref={ref} className="bx bx-info-circle contextual-help" />;
+}
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.css b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.css
index cca08f168c..3fbb434cfe 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.css
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.css
@@ -77,16 +77,4 @@
font-weight: 600;
}
- /* The same affordance the option lists carry: small, dimmed, and asking to be hovered. */
- .contextual-help {
- margin-inline-start: 0.25em;
- color: var(--contextual-help-icon-color);
- font-size: 0.75rem;
- opacity: 0.75;
- cursor: help;
- }
-
- .contextual-help:hover {
- opacity: 1;
- }
}
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.tsx
index 723ab7e4d0..cf6fcddd6e 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/note_usage_donut.tsx
@@ -3,14 +3,14 @@ import "./note_usage_donut.css";
import type { SpaceUsageNoteResponse } from "@triliumnext/commons";
import clsx from "clsx";
import type { ComponentChildren } from "preact";
-import { useMemo, useRef } from "preact/hooks";
+import { useMemo } from "preact/hooks";
import type React from "react";
import { Trans } from "react-i18next";
import { t } from "../../../../../services/i18n";
import { formatSize } from "../../../../../services/utils";
import DonutChart, { type DonutRing } from "../../../../react/charts/DonutChart";
-import { useStaticTooltip } from "../../../../react/hooks";
+import ContextualHelp from "../../../../react/ContextualHelp";
import { quickEditAttachment, quickEditNote } from "./context_menu";
import {
buildCompositionSegments,
@@ -20,13 +20,6 @@ import {
type UsageTooltipKind
} from "./donut_segments";
-/**
- * The app's "raise above everything" tooltip class. Bootstrap appends tooltips to `<body>`, where
- * the base `.tooltip` z-index sits below a modal — and this section is usually shown inside the
- * settings dialog, so without it every hint here opens behind the dialog that triggered it.
- */
-const RAISED_TOOLTIP = "tooltip-top";
-
export const COMPOSITION_RING_RADIUS = 133;
/** Half the children ring's: the note's own breakdown reads as the quieter, supporting ring. */
export const COMPOSITION_RING_THICKNESS = 23;
@@ -160,26 +153,11 @@ function SizeLine({ i18nKey, help, size }: { i18nKey: string, help: string, size
Size: <span className="note-usage-donut-size-value">{formatSize(size)}</span> as React.ReactElement
}}
/>
- <ContextualHelp title={help} />
+ <ContextualHelp helpMessage={help} />
</span>
);
}
-/**
- * The info affordance the option lists already use, on the app's tooltip rather than the browser's:
- * a native one would appear somewhere else, after its own delay, and in its own styling — while the
- * line's own hint is already the app's. Showing one dismisses the other, so the two never overlap.
- */
-function ContextualHelp({ title }: { title: string }) {
- const ref = useRef<HTMLSpanElement>(null);
- useStaticTooltip(ref, useMemo(
- () => ({ title, placement: "bottom", customClass: RAISED_TOOLTIP }),
- [ title ]
- ));
-
- return <span ref={ref} className="bx bx-info-circle contextual-help" />;
-}
-
/** "Title (1.2 MiB)", prefixed with its case ("Attachment: ", "Child note: ") where one applies. */
export function segmentTooltip(kind: UsageTooltipKind, title: string, size: number) {
const key = kind === "attachment" ? "space_usage.attachment_tooltip"