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): dispose tooltips even when their trigger was already detached (closes #10567)
Elian Doran065bd9159a83
diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsxindex ff111272fa..13843a0bf6 100644--- a/apps/client/src/widgets/react/hooks.spec.tsx+++ b/apps/client/src/widgets/react/hooks.spec.tsx@@ -1,8 +1,10 @@+import { Tooltip } from "bootstrap";import { render } from "preact";+import { useRef } from "preact/hooks";import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-import { type DelayedVisibilityPhase, useDelayedVisibility } from "./hooks";+import { type DelayedVisibilityPhase, useDelayedVisibility, useStaticTooltip } from "./hooks";let currentPhase: DelayedVisibilityPhase | undefined;@@ -81,3 +83,46 @@ describe("useDelayedVisibility", () => {expect(currentPhase).toBe("hidden");});});++describe("useStaticTooltip", () => {+ let container: HTMLElement;++ beforeEach(() => {+ container = document.createElement("div");+ document.body.appendChild(container);+ });++ afterEach(() => {+ render(null, container);+ container.remove();+ for (const orphan of document.querySelectorAll(".tooltip")) {+ orphan.remove();+ }+ });++ function TooltipHarness({ generation }: { generation: number }) {+ const ref = useRef<HTMLSpanElement>(null);+ // The inline config object gets a new identity on every render, so the hook's effect+ // re-runs after each commit — mirroring SyncStatus, where the cleanup for the previous+ // trigger element runs only after the keyed remount has already detached it.+ useStaticTooltip(ref, { title: "Sync status", animation: false });+ return <span key={generation} ref={ref} />;+ }++ it("removes a shown tooltip popup when the trigger element is remounted (#10567)", async () => {+ await act(async () => render(<TooltipHarness generation={1} />, container));++ const trigger = container.querySelector("span");+ expect(trigger).not.toBeNull();+ act(() => {+ if (trigger) Tooltip.getInstance(trigger)?.show();+ });+ expect(document.querySelector(".tooltip")).not.toBeNull();++ // Remount the trigger while its tooltip is shown — like a sync state change+ // arriving while the user hovers the sync button.+ await act(async () => render(<TooltipHarness generation={2} />, container));++ expect(document.querySelector(".tooltip")).toBeNull();+ });+});diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex 964372aa8a..2003a7484d 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -1052,9 +1052,11 @@ export function useTooltip(elRef: RefObject<HTMLElement>, config: Partial<Toolticonst tooltip = Tooltip.getInstance(element);return () => {- if (element.isConnected) {- tooltip?.dispose();- }+ // Dispose even when the trigger element is already detached (e.g. a keyed remount+ // replaced it before this cleanup ran) — dispose() also removes a currently-shown+ // popup from the DOM, and with the trigger gone nothing else ever would (#10567).+ // The pending-callback crash of bootstrap#37474 is handled by the dispose() patch above.+ tooltip?.dispose();};}, [ elRef, config ]);@@ -1108,9 +1110,11 @@ export function useStaticTooltip(elRef: RefObject<Element>, config?: Partial<Tooreturn () => {tooltips.delete(tooltip);- if (element.isConnected) {- tooltip.dispose();- }+ // Dispose even when the trigger element is already detached (e.g. a keyed remount+ // replaced it before this cleanup ran) — dispose() also removes a currently-shown+ // popup from the DOM, and with the trigger gone nothing else ever would (#10567).+ // The pending-callback crash of bootstrap#37474 is handled by the dispose() patch above.+ tooltip.dispose();// For delegated (`selector:`) configs, hovered children spawn per-target// Tooltip instances whose popups the parent's dispose() does not remove;