Skip to content

Build your personal knowledge base with Trilium Notes

fix(tooltips): stop a manual tooltip being hidden under the pointer

Elian Doran4645943a1a4d
diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsx
index fcf1156a61..3dbb40a78f 100644
--- a/apps/client/src/widgets/react/hooks.spec.tsx
+++ b/apps/client/src/widgets/react/hooks.spec.tsx
@@ -4,7 +4,7 @@ import { useRef } from "preact/hooks";
import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { type DelayedVisibilityPhase, useDelayedVisibility, useImperativeSearchHighlighlighting, useStaticTooltip } from "./hooks";
+import { type DelayedVisibilityPhase, useDelayedVisibility, useImperativeSearchHighlighlighting, useStaticTooltip, useTooltip } from "./hooks";
/**
* mark.js delegating to the real implementation, so marking still works, while recording the calls
@@ -156,6 +156,59 @@ describe("useStaticTooltip", () => {
});
});
+describe("useTooltip", () => {
+ let container: HTMLElement;
+ let show: (() => void) | undefined;
+ let hide: (() => void) | undefined;
+
+ // Bootstrap defers show()'s completion until the fade-in ends, so the hover state it keeps
+ // settles a tick after the event that drove it.
+ const settle = () => act(async () => { await new Promise((resolve) => setTimeout(resolve, 40)); });
+ const isShown = () => document.querySelector(".tooltip") !== null;
+
+ const CONFIG = { title: "Show help", trigger: "manual" } satisfies Partial<Tooltip.Options>;
+
+ function TooltipHarness() {
+ const ref = useRef<HTMLSpanElement>(null);
+ ({ showTooltip: show, hideTooltip: hide } = useTooltip(ref, CONFIG));
+ return <span ref={ref} />;
+ }
+
+ beforeEach(async () => {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ await act(async () => render(<TooltipHarness />, container));
+ });
+
+ afterEach(() => {
+ render(null, container);
+ container.remove();
+ show = hide = undefined;
+ for (const orphan of document.querySelectorAll(".tooltip")) {
+ orphan.remove();
+ }
+ });
+
+ it("keeps a manually shown tooltip up after a hover too short for its fade-in", async () => {
+ act(() => show?.());
+ await settle();
+ expect(isShown(), "shown on the first hover").toBe(true);
+
+ // A quick pass over the trigger: away again before the fade-in has finished, which used to
+ // leave Bootstrap's own hover flag set with nothing shown (see clearStaleHoverState).
+ act(() => show?.());
+ act(() => hide?.());
+ await settle();
+ expect(isShown(), "hidden once the pointer has left").toBe(false);
+
+ // The hover that follows it must stand rather than being put away by the one before.
+ act(() => show?.());
+ expect(isShown(), "shown straight away").toBe(true);
+ await settle();
+ expect(isShown(), "still shown with the pointer where it is").toBe(true);
+ });
+});
+
describe("useImperativeSearchHighlighlighting", () => {
let container: HTMLElement;
let highlight: ((el: HTMLElement | null | undefined) => void) | undefined;
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 2b7cc7d265..a434e510ee 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -1016,8 +1016,12 @@ export function useTooltip(elRef: RefObject<HTMLElement>, config: Partial<Toolti
const showTooltip = useCallback(() => {
if (!elRef?.current) return;
- const $el = $(elRef.current);
- $el.tooltip("show");
+ const element = elRef.current;
+ const tooltip = Tooltip.getInstance(element);
+ if (tooltip) {
+ clearStaleHoverState(tooltip);
+ }
+ $(element).tooltip("show");
}, [ elRef, config ]);
const hideTooltip = useCallback(() => {
@@ -1032,6 +1036,27 @@ export function useTooltip(elRef: RefObject<HTMLElement>, config: Partial<Toolti
return { showTooltip, hideTooltip };
}
+/**
+ * Clears the hover state Bootstrap keeps of its own accord, so that a manual `show()` isn't undone by
+ * the hover before it.
+ *
+ * Bootstrap tracks `_isHovered` even under `trigger: "manual"`, and leans on `hide()` resetting it to
+ * `null` — the value that tells a later `show()` there is no hover to act on ("a trick to support
+ * manual triggering", as it puts it). But `show()` writes `false` into the same flag from a callback
+ * deferred until the fade-in has finished, so a hover ended before then reverses the two writes:
+ * `hide()` sets `null`, and the deferred callback then sets `false` over it. The flag is left standing
+ * with nothing shown, and the next `show()` reads it, takes the tooltip to have been left already and
+ * calls `_leave()` — which hides it a moment after it appeared, with the pointer still on the trigger.
+ *
+ * A quick pass over a trigger is enough to leave it in that state, which is why the `?` of the sidebar
+ * card headers (a {@link Dropdown} title, shown from its own mouseenter) so often vanished under the
+ * pointer. Bootstrap offers no public way to reach the flag, and no public call that clears it without
+ * a tooltip already being shown.
+ */
+function clearStaleHoverState(tooltip: Tooltip) {
+ (tooltip as unknown as { _isHovered: boolean | null })._isHovered = null;
+}
+
const tooltips = new Set<Tooltip>();
/**