The issue you're experiencing with Tippy.js tooltips not working on dynamically added child nodes is likely due to the fact that Tippy.js initializes tooltips only once when the page loads. When you expand the tree and new nodes are added to the DOM, those elements are not automatically initialized with tooltips.
To solve this, you need to reinitialize Tippy.js tooltips whenever the DOM is updated. You can achieve this by listening for a custom event in your Livewire component and reinitializing the tooltips.
Here's how you can modify your code to fix the issue:
-
Modify the Livewire Component:
In your
NodeTreecomponent, you are already dispatching a custom eventreinitialize-tooltipswhen nodes are toggled. Ensure this event is being dispatched correctly. -
Listen for the Custom Event in JavaScript:
Update your JavaScript to listen for the
reinitialize-tooltipsevent and reinitialize Tippy.js tooltips.<script src="https://cdn.jsdelivr.net/npm/tippy.js@6/dist/tippy-bundle.umd.js"></script> <script> document.addEventListener("DOMContentLoaded", function () { function initializeTooltips() { tippy('[data-tippy-content]', { allowHTML: true, theme: 'light', placement: 'top', }); } // Initialize tooltips on page load initializeTooltips(); // Listen for the custom event to reinitialize tooltips window.addEventListener('reinitialize-tooltips', function () { initializeTooltips(); }); }); </script> -
Ensure Event Dispatching:
Make sure that the
reinitialize-tooltipsevent is dispatched correctly in your Livewire component. You can do this by using thedispatchBrowserEventmethod:public function toggleNode($nodeId): void { if (in_array($nodeId, $this->expandedNodes)) { $this->expandedNodes = array_diff($this->expandedNodes, [$nodeId]); } else { $this->expandedNodes[] = $nodeId; } // Dispatch the event to reinitialize tooltips $this->dispatchBrowserEvent('reinitialize-tooltips'); }
By following these steps, you ensure that Tippy.js tooltips are reinitialized every time the DOM is updated with new nodes, allowing tooltips to work on dynamically added elements.