Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(client/tooltips): put away a delegated tooltip whose trigger left the DOM
matt wilkiea604f8610162
diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsxindex 10468eff39..bac0b281db 100644--- a/apps/client/src/widgets/react/hooks.spec.tsx+++ b/apps/client/src/widgets/react/hooks.spec.tsx@@ -170,6 +170,60 @@ describe("useStaticTooltip", () => {await act(async () => { trigger?.dispatchEvent(new MouseEvent("click", { bubbles: true })); });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.+ const DELEGATED_CONFIG = {+ selector: "span",+ animation: false,+ title() {+ return this.getAttribute("title") || "";+ }+ } satisfies Partial<Tooltip.Options>;++ function DelegatedTooltipHarness({ generation }: { generation: number }) {+ const ref = useRef<HTMLDivElement>(null);+ useStaticTooltip(ref, DELEGATED_CONFIG);+ return (+ <div ref={ref}>+ <span key={generation} title="smile" />+ </div>+ );+ }++ it("removes an orphaned popup when a delegated tooltip's hovered child is removed without a mouseleave (#10680)", async () => {+ await act(async () => render(<DelegatedTooltipHarness generation={1} />, container));++ const span = container.querySelector("span");+ expect(span).not.toBeNull();++ // A synthetic mouseenter does not reliably reach Bootstrap's own delegated listener under+ // happy-dom, so the per-span instance is created directly instead — the fix only depends on+ // showing it firing `inserted.bs.tooltip` on the span, which then bubbles to the container+ // exactly as it would from Bootstrap's own delegated hover handling.+ act(() => {+ if (span) {+ Tooltip.getOrCreateInstance(span, {+ animation: false,+ title() {+ return this.getAttribute("title") || "";+ }+ }).show();+ }+ });+ expect(document.querySelector(".tooltip"), "shown before the remount").not.toBeNull();++ // Replace the hovered span with a fresh one — a keyed remount, like the icon picker's grid+ // re-keying its cells on every keystroke in its search box — without ever firing mouseleave.+ await act(async () => render(<DelegatedTooltipHarness generation={2} />, container));++ // The MutationObserver callback runs as a microtask; give it a turn to fire.+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });++ expect(document.querySelector(".tooltip"), "gone once the observer sees the removal").toBeNull();+ });});describe("useTooltip", () => {diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex 7118175387..7fc239d7f9 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -1177,7 +1177,61 @@ export function useStaticTooltip(elRef: RefObject<Element>, config?: Partial<Too};element.addEventListener("click", dismissOnPress);+ // For delegated (`selector:`) configs, a hovered child gets its own per-target Tooltip+ // instance (see the sweep at the end of the cleanup below) that only ever hides on its+ // 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.+ 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;+ const instance = Tooltip.getInstance(target);+ if (instance) {+ // Reuses the bootstrap#37474 guard the patched dispose() above installs.+ instance.dispose();+ } else {+ // Belt-and-braces: the instance may already be gone on its own.+ const popupId = target.getAttribute("aria-describedby");+ if (popupId) {+ document.getElementById(popupId)?.remove();+ }+ }+ shownDelegates.delete(target);+ }+ });+ delegateObserver.observe(element, { childList: true, subtree: true });++ disposeDelegateTracking = () => {+ delegateObserver.disconnect();+ element.removeEventListener("inserted.bs.tooltip", onDelegateShown);+ element.removeEventListener("hidden.bs.tooltip", onDelegateHidden);+ };+ }+return () => {+ disposeDelegateTracking();element.removeEventListener("click", dismissOnPress);tooltips.delete(tooltip);// Dispose even when the trigger element is already detached (e.g. a keyed remount