Skip to content

Build your personal knowledge base with Trilium Notes

fix(client/tooltips): watch a delegated container only while a popup is up

Elian Doran7e22479aa8cb
diff --git a/apps/client/src/widgets/react/IconPicker.tsx b/apps/client/src/widgets/react/IconPicker.tsx
index 2928c78652..ea0823cd8c 100644
--- a/apps/client/src/widgets/react/IconPicker.tsx
+++ b/apps/client/src/widgets/react/IconPicker.tsx
@@ -1,7 +1,7 @@
import "./IconPicker.css";
import { IconRegistry } from "@triliumnext/commons";
-import { Dropdown as BootstrapDropdown } from "bootstrap";
+import { Dropdown as BootstrapDropdown, Tooltip } from "bootstrap";
import clsx from "clsx";
import { CSSProperties } from "preact";
import { createPortal } from "preact/compat";
@@ -22,6 +22,15 @@ import Modal from "./Modal";
/** The room one icon takes in the grid, which also decides how many fit across a given width. */
export const ICON_SIZE = isMobile() ? 56 : 48;
+// One tooltip on the grid, delegated to the icon tiles. A module constant so the grid re-rendering
+// on every keystroke in the search does not tear the tooltip down and build it again each time.
+const ICON_TOOLTIP_CONFIG: Partial<Tooltip.Options> = {
+ selector: "span",
+ customClass: "pre-wrap-text",
+ animation: false,
+ title() { return this.getAttribute("title") || ""; },
+};
+
interface IconPickerProps {
/** Receives the class of the icon picked, e.g. `bx bx-star`. */
onSelect(iconClass: string): void;
@@ -47,12 +56,7 @@ export default function IconPicker({ onSelect, onReset, resetText, columnCount }
const iconListRef = useRef<HTMLDivElement>(null);
const [ search, setSearch ] = useState<string>();
const [ filterByPrefix, setFilterByPrefix ] = useState<string | null>(null);
- useStaticTooltip(iconListRef, {
- selector: "span",
- customClass: "pre-wrap-text",
- animation: false,
- title() { return this.getAttribute("title") || ""; },
- });
+ useStaticTooltip(iconListRef, ICON_TOOLTIP_CONFIG);
const allIcons = useAllIcons();
const filteredIcons = useFilteredIcons(allIcons, search, filterByPrefix);
diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsx
index bf7eefa145..aa9d4a6bda 100644
--- a/apps/client/src/widgets/react/hooks.spec.tsx
+++ b/apps/client/src/widgets/react/hooks.spec.tsx
@@ -171,10 +171,11 @@ describe("useStaticTooltip", () => {
expect(document.querySelector(".tooltip"), "gone with the press").toBeNull();
});
- // Module-level (not recreated per render) on purpose: the icon picker's own container effect
- // never re-runs when its virtualized grid re-keys a cell, so the MutationObserver #10680's fix
- // installs has to survive across that re-render on its own — a config recreated inline, like
- // TooltipHarness above, would re-run the effect and mask exactly what this test checks.
+ // Module-level (not recreated per render) on purpose, as the icon picker's `ICON_TOOLTIP_CONFIG`
+ // is: the container effect never re-runs when the virtualized grid re-keys a cell, so the
+ // MutationObserver #10680's fix installs has to survive across that re-render on its own — a
+ // config recreated inline, like TooltipHarness above, would re-run the effect and mask exactly
+ // what this test checks.
const DELEGATED_CONFIG = {
selector: "span",
animation: false,
@@ -226,10 +227,14 @@ describe("useStaticTooltip", () => {
});
it("stops tracking a delegated tooltip that was put away normally, and leaves its instance alone", async () => {
+ const observe = vi.spyOn(MutationObserver.prototype, "observe");
+ const disconnect = vi.spyOn(MutationObserver.prototype, "disconnect");
await act(async () => render(<DelegatedTooltipHarness generation={1} />, container));
const span = container.querySelector("span");
expect(span).not.toBeNull();
+ // The container is watched only while a popup is up, not from the moment it mounts.
+ expect(observe, "not observing before anything is shown").not.toHaveBeenCalled();
let instance: Tooltip | undefined;
act(() => {
@@ -244,11 +249,16 @@ describe("useStaticTooltip", () => {
}
});
expect(document.querySelector(".tooltip"), "shown").not.toBeNull();
+ expect(observe, "observing once a popup is shown").toHaveBeenCalledTimes(1);
// An ordinary mouseleave-driven hide fires `hidden.bs.tooltip`, which must untrack the
// span — otherwise the observer below would dispose an instance Bootstrap still owns.
+ disconnect.mockClear();
act(() => instance?.hide());
expect(document.querySelector(".tooltip"), "hidden the normal way").toBeNull();
+ expect(disconnect, "observer let go once nothing is shown").toHaveBeenCalled();
+ observe.mockRestore();
+ disconnect.mockRestore();
await act(async () => render(<DelegatedTooltipHarness generation={2} />, container));
await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 7fc239d7f9..c4eac0f804 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -1182,28 +1182,14 @@ export function useStaticTooltip(elRef: RefObject<Element>, config?: Partial<Too
// own mouseleave. If that child leaves the DOM while its popup is still shown — the note
// icon picker's virtualized grid re-keys its cells on every keystroke in the search box,
// and does the same on scroll — no mouseleave ever fires, so the instance never hides and
- // its popup is orphaned in `document.body` until reload (#10680). Track which delegate
- // elements currently have a popup shown, via the container-level events Bootstrap's own
- // component events bubble to.
+ // its popup is orphaned in `document.body` until reload (#10680). While a delegate has its
+ // popup up, watch the container for removals and put the popup away the moment its trigger
+ // is gone — the hook's own cleanup cannot be relied on, since a grid re-render driven by the
+ // grid's own state never re-runs this effect. The observer is connected only for as long
+ // as a popup is shown, so it costs nothing while the grid is merely typed into or scrolled.
let disposeDelegateTracking = () => {};
if (config?.selector) {
const shownDelegates = new Set<Element>();
- const onDelegateShown = (event: Event) => {
- if (event.target instanceof Element && event.target !== element) {
- shownDelegates.add(event.target);
- }
- };
- const onDelegateHidden = (event: Event) => {
- if (event.target instanceof Element) {
- shownDelegates.delete(event.target);
- }
- };
- element.addEventListener("inserted.bs.tooltip", onDelegateShown);
- element.addEventListener("hidden.bs.tooltip", onDelegateHidden);
-
- // A MutationObserver catches the removal the moment it happens, rather than
- // depending on this hook's own cleanup running again — which a grid re-render
- // triggered by its own state, not by `elRef`/`config` changing, never causes.
const delegateObserver = new MutationObserver(() => {
for (const target of shownDelegates) {
if (target.isConnected) continue;
@@ -1220,8 +1206,23 @@ export function useStaticTooltip(elRef: RefObject<Element>, config?: Partial<Too
}
shownDelegates.delete(target);
}
+ if (!shownDelegates.size) delegateObserver.disconnect();
});
- delegateObserver.observe(element, { childList: true, subtree: true });
+ // Bootstrap's component events bubble from the delegate up to the container.
+ const onDelegateShown = (event: Event) => {
+ if (!(event.target instanceof Element) || event.target === element) return;
+ if (!shownDelegates.size) {
+ delegateObserver.observe(element, { childList: true, subtree: true });
+ }
+ shownDelegates.add(event.target);
+ };
+ const onDelegateHidden = (event: Event) => {
+ if (!(event.target instanceof Element)) return;
+ shownDelegates.delete(event.target);
+ if (!shownDelegates.size) delegateObserver.disconnect();
+ };
+ element.addEventListener("inserted.bs.tooltip", onDelegateShown);
+ element.addEventListener("hidden.bs.tooltip", onDelegateHidden);
disposeDelegateTracking = () => {
delegateObserver.disconnect();