Confirmed Tooltip remains visible after AJAX replaces its trigger

Painbaker

Well-known member
Licensed customer
Affected version
2.3.13
Show a tooltip on a control inside a row, then replace that row via AJAX. The tooltip remains visible after its trigger is removed because XF renders it under
document.body. The tooltip should close when its trigger is detached.

Suggested fix:

Diff:
diff --git a/js/xf/core_handlers.js b/js/xf/core_handlers.js
index 4a68808..11b8042 100644
--- a/js/xf/core_handlers.js
+++ b/js/xf/core_handlers.js
@@ -1396,6 +1396,7 @@
 
         target: null,
         tooltip: null,
+        targetObserver: null,
 
         delayTimeout: null,
         delayTimeoutType: null,
@@ -1593,6 +1594,11 @@
 
             setTimeout(() =>
             {
+                if (!this.clickTriggered || !this.target.isConnected)
+                {
+                    return;
+                }
+
                 const covers = this.addCovers()
 
                 if (XF.isEventTouchTriggered(e))
@@ -1824,6 +1830,11 @@
 
         show ()
         {
+            if (!this.target.isConnected)
+            {
+                return;
+            }
+
             const id = XF.uniqueId(this.target)
             XF.off(window, 'focus.tooltip-' + id)
             XF.on(window, 'focus.tooltip-' + id, e =>
@@ -1831,6 +1842,19 @@
                 this.stopFocusBefore = Date.now() + 250
             })
 
+            if (!this.targetObserver)
+            {
+                this.targetObserver = new MutationObserver(() =>
+                {
+                    if (!this.target.isConnected)
+                    {
+                        XF.off(window, 'focus.tooltip-' + id);
+                        this.hide();
+                    }
+                })
+                this.targetObserver.observe(document, { childList: true, subtree: true });
+            }
+
             XF.setRelativeZIndex(this.tooltip.getTooltip(), this.target)
 
             if (this.options.onShow)
@@ -1856,6 +1880,12 @@
 
         hide ()
         {
+            if (this.targetObserver)
+            {
+                this.targetObserver.disconnect();
+                this.targetObserver = null;
+            }
+
             this.tooltip.hide()
             this.resetDelayTimer()
             this.clickTriggered = false
 
Back
Top Bottom