import { Tooltip } from 'bootstrap'; import { isNullOrEmpty } from './is-null-or-empty'; export interface TooltipSyncArgs { /** Passed to the Bootstrap constructor on first creation only; later syncs ignore it. */ constructorOptions?: Partial; /** Set when the caller renders its own aria-label — blocks the stale-aria re-sync below. */ callerOwnsAriaLabel?: boolean; } /** What Bootstrap keeps on a tooltip but does not type: the tip element, while there is one. */ interface TooltipInternals { tip: HTMLElement | null; } /** * Shared Bootstrap tooltip lifecycle (QA_GO-736). Bootstrap consumes `title` once, at * construction: it moves the text into data-bs-original-title, clears `title`, and reads only * that copy from then on. A render that keeps emitting the current `title` therefore shows a * frozen Bootstrap tip plus a competing native one. Call sync() from mounted() and a * @Watch('', { flush: 'post' }) — flush 'post' so the element already carries the * freshly patched title — and dispose() from beforeUnmount(): Bootstrap appends the tip to * and binds a hide handler to the closest .modal (an ancestor that outlives the * component), so an undisposed instance leaks both. A control whose click does something * (opens an overlay, removes its own row) calls dismiss() from its click handler. */ export class TooltipLifecycle { static sync( el: HTMLElement, title: string, args?: TooltipSyncArgs, ): void { if (el == null) { return; } const instance = Tooltip.getInstance(el); if (isNullOrEmpty(title)) { if (instance != null) { TooltipLifecycle.dispose(el); // dispose() writes the now-stale data-bs-original-title back into `title`. el.removeAttribute('title'); } return; } if (instance == null) { el.setAttribute('title', title); // eslint-disable-next-line no-new new Tooltip(el, args?.constructorOptions); return; } // No-op when nothing changed: setContent() on an open tip tears it down and re-animates // (dispose + show), so unconditional re-syncs (e.g. from an updated() hook) would flicker // a hovered tip on every unrelated re-render. if (el.getAttribute('data-bs-original-title') === title && !el.hasAttribute('title')) { return; } el.setAttribute('data-bs-original-title', title); // Render just re-added `title`; left in place it shows a second, native tooltip. el.removeAttribute('title'); // Bootstrap derives aria-label from the title on text-less triggers, and that copy goes // stale too. Only ours to update — a caller-supplied aria-label is owned by render. if (args?.callerOwnsAriaLabel != true && el.hasAttribute('aria-label')) { el.setAttribute('aria-label', title); } instance.setContent({ '.tooltip-inner': title }); } /** * A click answers the tooltip: the tip that is up goes at once, and one that is still on its way never comes. * * The second half is the touch case. A tap raises the browser's emulated `mouseenter` / `focusin` right before its click, * and Bootstrap's `_enter()` only SCHEDULES `show()` (a timer, `delay.show` — 0 for every Powerduck tooltip). So the tip * was created after the click's own work, above the overlay the click opened (a dialog, a sheet), and no `mouseleave` * ever took it away. A disabled tooltip shows nothing: it is switched off for the one task the scheduled show runs in, and * back on after it, so a hover that comes later shows its tip as ever. */ static dismiss(el: Element): void { document.querySelector('body > .tooltip')?.remove(); const instance = el != null ? Tooltip.getInstance(el) : null; if (instance == null) { return; } instance.disable(); setTimeout(() => instance.enable(), 0); } /** * Bootstrap ends a show or a hide through a callback that reads the instance, and `dispose()` empties it: a trigger that * goes (its row re-rendered or removed) while its tip is still fading used to have that callback throw * `_isWithActiveTrigger` of null. Bootstrap's own fallback timer ends a transition by dispatching `transitionend` on the * tip — done here first, while the instance is whole. */ static dispose(el: Element): void { const instance = el != null ? Tooltip.getInstance(el) : null; if (instance == null) { return; } (instance as unknown as TooltipInternals).tip?.dispatchEvent(new Event('transitionend')); instance.dispose(); } }