Skip to content

Build your personal knowledge base with Trilium Notes

client/context help: fix an issue (P1)

Adorian Dorana2a86b17943d
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index d3aa0088fb..dab918a9d3 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3118,6 +3118,9 @@
"scripting_disabled_button": "Open security settings",
"dismiss": "Dismiss"
},
+ "contextual_help": {
+ "label": "Help: {{- message}}"
+ },
"settings": {
"related_settings": "Related settings",
"related_actions": "Related actions"
diff --git a/apps/client/src/widgets/react/ContextualHelp.css b/apps/client/src/widgets/react/ContextualHelp.css
index ff7c7a53ce..ac8f3df6cc 100644
--- a/apps/client/src/widgets/react/ContextualHelp.css
+++ b/apps/client/src/widgets/react/ContextualHelp.css
@@ -7,10 +7,18 @@
cursor: help;
}
-.contextual-help:hover {
+.contextual-help:hover,
+.contextual-help:focus-visible {
opacity: 1;
}
+/* Reached by keyboard it is the only thing being pointed at, so it says where the focus is. */
+.contextual-help:focus-visible {
+ outline: 2px solid var(--input-focus-outline-color);
+ outline-offset: 2px;
+ border-radius: 2px;
+}
+
/*
* 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
diff --git a/apps/client/src/widgets/react/ContextualHelp.spec.tsx b/apps/client/src/widgets/react/ContextualHelp.spec.tsx
index c10ba6b69b..c8048ca4fc 100644
--- a/apps/client/src/widgets/react/ContextualHelp.spec.tsx
+++ b/apps/client/src/widgets/react/ContextualHelp.spec.tsx
@@ -28,8 +28,24 @@ vi.mock("./Modal", () => ({
)
}));
+// Nothing initializes i18next for the tests, so the wording is stood in for by the key and the
+// values interpolated into it — which is what the assertions here are actually about.
+vi.mock("../../services/i18n", () => ({
+ t: (key: string, opts?: Record<string, string>) => `${key}(${opts?.message ?? ""})`
+}));
+
+import { Tooltip } from "bootstrap";
+
import ContextualHelp from "./ContextualHelp";
+/** Presses a key on the icon the way a keyboard user would, and answers whether it was handled. */
+function press(element: Element | null | undefined, key: string) {
+ const event = new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true });
+
+ act(() => { element?.dispatchEvent(event); });
+ return event.defaultPrevented;
+}
+
describe("ContextualHelp", () => {
let container: HTMLElement;
@@ -59,6 +75,30 @@ describe("ContextualHelp", () => {
});
});
+ it("is a control in its own right: reachable, named by its explanation, and worked by key", () => {
+ const tooltip = { toggle: vi.fn(), hide: vi.fn() };
+ vi.spyOn(Tooltip, "getInstance").mockReturnValue(tooltip as unknown as Tooltip);
+
+ act(() => render(<ContextualHelp helpMessage="What this figure covers." />, container));
+ const span = container.querySelector("span");
+
+ expect(span?.getAttribute("role")).toBe("button");
+ expect(span?.getAttribute("tabindex")).toBe("0");
+ // The explanation is the accessible name, so it is there to be read whether or not a
+ // tooltip happens to be on screen at the time.
+ expect(span?.getAttribute("aria-label")).toBe("contextual_help.label(What this figure covers.)");
+
+ // Both activation keys reveal it, and both are swallowed — Space would otherwise scroll the
+ // page out from under what is being explained.
+ expect(press(span, "Enter")).toBe(true);
+ expect(press(span, " ")).toBe(true);
+ expect(tooltip.toggle).toHaveBeenCalledTimes(2);
+
+ // Dismissed from the keyboard too, without having to tab away.
+ expect(press(span, "Escape")).toBe(false);
+ expect(tooltip.hide).toHaveBeenCalledTimes(1);
+ });
+
it("hands the tooltip the same configuration until the message itself changes", () => {
act(() => render(<ContextualHelp helpMessage="First." />, container));
const [ , first ] = mocks.useStaticTooltip.mock.calls.at(-1) ?? [];
@@ -90,6 +130,18 @@ describe("ContextualHelp", () => {
expect(sheet?.textContent).toBe("What this figure covers.");
expect(container.contains(sheet)).toBe(false);
});
+
+ it("opens that sheet from the keyboard as well, and says that it opens one", async () => {
+ const OnMobile = await loadOnMobile();
+ act(() => render(<OnMobile helpMessage="What this figure covers." />, container));
+
+ const span = container.querySelector("span");
+ expect(span?.getAttribute("role")).toBe("button");
+ expect(span?.getAttribute("aria-haspopup")).toBe("dialog");
+
+ expect(press(span, "Enter")).toBe(true);
+ expect(document.body.querySelector(".sheet-stub")?.textContent).toBe("What this figure covers.");
+ });
});
/** The component as a phone loads it — the layout is read as its module is evaluated, once. */
diff --git a/apps/client/src/widgets/react/ContextualHelp.tsx b/apps/client/src/widgets/react/ContextualHelp.tsx
index 6a8b1e7503..81e2e399a1 100644
--- a/apps/client/src/widgets/react/ContextualHelp.tsx
+++ b/apps/client/src/widgets/react/ContextualHelp.tsx
@@ -1,8 +1,11 @@
import "./ContextualHelp.css";
+import { Tooltip } from "bootstrap";
+import type { RefObject } from "preact";
import { createPortal } from "preact/compat";
import { useMemo, useRef, useState } from "preact/hooks";
+import { t } from "../../services/i18n";
import { isMobile } from "../../services/utils";
import { useStaticTooltip } from "./hooks";
import Modal from "./Modal";
@@ -30,7 +33,7 @@ export default function ContextualHelp({ helpMessage }: ContextualHelpProps) {
/** 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. */
+/** The pointer's: the explanation is a hover — or a focus, or a key press — away. */
function HoveredHelp({ helpMessage }: ContextualHelpProps) {
const ref = useRef<HTMLSpanElement>(null);
@@ -43,7 +46,17 @@ function HoveredHelp({ helpMessage }: ContextualHelpProps) {
customClass: "tooltip-top"
}), [ helpMessage ]));
- return <span ref={ref} className="bx bx-info-circle contextual-help" />;
+ return (
+ <HelpIcon
+ iconRef={ref}
+ helpMessage={helpMessage}
+ // Reaching the icon reveals the explanation by itself — Bootstrap's default trigger is
+ // "hover focus" — so what the key press adds is the other half: dismissing it again
+ // without tabbing away, and answering a screen reader's activation of the control.
+ onActivate={() => withTooltip(ref, (tooltip) => tooltip.toggle())}
+ onDismiss={() => withTooltip(ref, (tooltip) => tooltip.hide())}
+ />
+ );
}
/**
@@ -61,9 +74,10 @@ function TappedHelp({ helpMessage }: ContextualHelpProps) {
return (
<>
- <span
- className="bx bx-info-circle contextual-help"
- onClick={() => setShown(true)}
+ <HelpIcon
+ helpMessage={helpMessage}
+ popup="dialog"
+ onActivate={() => setShown(true)}
/>
{createPortal((
@@ -80,3 +94,59 @@ function TappedHelp({ helpMessage }: ContextualHelpProps) {
</>
);
}
+
+interface HelpIconProps {
+ iconRef?: RefObject<HTMLSpanElement>;
+ helpMessage: string;
+ /** What activating the icon opens, for anything that is not simply revealed in place. */
+ popup?: "dialog";
+ onActivate: () => void;
+ onDismiss?: () => void;
+}
+
+/**
+ * The icon both affordances are drawn as: a control rather than a decoration, so it is reachable by
+ * keyboard, announced as something that can be activated, and answers `Enter`/`Space` with whatever
+ * a pointer would have done.
+ *
+ * Its accessible name carries the explanation itself. Bootstrap points `aria-describedby` at the
+ * tooltip while one is shown and takes the attribute away again on hide, so leaning on that would
+ * leave the message reachable only during the moments it happens to be on screen — and on a phone
+ * there is no tooltip at all.
+ */
+function HelpIcon({ iconRef, helpMessage, popup, onActivate, onDismiss }: HelpIconProps) {
+ return (
+ <span
+ ref={iconRef}
+ className="bx bx-info-circle contextual-help"
+ role="button"
+ tabIndex={0}
+ aria-label={t("contextual_help.label", { message: helpMessage })}
+ aria-haspopup={popup}
+ onClick={onActivate}
+ onKeyDown={(e) => {
+ if (e.key === "Enter" || e.key === " ") {
+ // Space scrolls the page under the icon otherwise, which is exactly the thing
+ // the explanation was meant to be read against.
+ e.preventDefault();
+ onActivate();
+ } else if (e.key === "Escape") {
+ onDismiss?.();
+ }
+ }}
+ />
+ );
+}
+
+/** Runs something on the icon's tooltip, if one has been built for it yet. */
+function withTooltip(iconRef: RefObject<HTMLSpanElement>, action: (tooltip: Tooltip) => void) {
+ const element = iconRef.current;
+
+ if (element) {
+ const tooltip = Tooltip.getInstance(element);
+
+ if (tooltip) {
+ action(tooltip);
+ }
+ }
+}