import { RefObject } from "inferno"; import { DelegateInstance as TippyDelegateInstance, Props as TippyProps, Instance as TippyInstance, delegate as tippyDelegate, } from "tippy.js"; let instance: TippyDelegateInstance | undefined; const tippySelector = "[data-tippy-content]"; const shownInstances: Set> = new Set(); const tippyDelegateOptions: Partial & { target: string } = { delay: [500, 0], // Display on "long press" touch: ["hold", 500], target: tippySelector, onShow(i: TippyInstance) { shownInstances.add(i); }, onHidden(i: TippyInstance) { shownInstances.delete(i); }, }; export function setupTippy(root: RefObject) { if (!instance && root.current) { instance = tippyDelegate(root.current, tippyDelegateOptions); } } let requested = false; export function cleanupTippy() { if (requested) { return; } requested = true; queueMicrotask(() => { requested = false; if (shownInstances.size) { // Avoid randomly closing tooltips. return; } // delegate from tippy.js creates tippy instances when needed, but only // destroys them when the delegate instance is destroyed. const current = instance?.reference ?? null; destroyTippy(); setupTippy({ current }); }); } export function destroyTippy() { instance?.destroy(); instance = undefined; }