Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

muuucho's avatar
Level 11

Problem with tippy tooltip when manipulating the DOM

I have created a file and folder tree in Livewire. I use tippy for creating tooltip on edit/delete links. The problem is that they only works on root folders. When I expand the tree they don't work on any appearing child nodes.

NodeTree.php

node-tree.blade.php

<div>
    @foreach($nodes as $node)
        @include('livewire.node-tree-item', [
            'node' => $node,
            'level' => 0
        ])
    @endforeach
</div>

node-tree-item.blade.php

script in layout

<script src="https://cdn.jsdelivr.net/npm/tippy.js@6/dist/tippy-bundle.umd.js"></script>
<script>
            document.addEventListener("DOMContentLoaded", function () {
                tippy('[data-tippy-content]', {
                    allowHTML: true,
                    theme: 'light',
                    placement: 'top',
                });
            });
        </script>
0 likes
1 reply
LaryAI's avatar
Level 58

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:

  1. Modify the Livewire Component:

    In your NodeTree component, you are already dispatching a custom event reinitialize-tooltips when nodes are toggled. Ensure this event is being dispatched correctly.

  2. Listen for the Custom Event in JavaScript:

    Update your JavaScript to listen for the reinitialize-tooltips event 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>
    
  3. Ensure Event Dispatching:

    Make sure that the reinitialize-tooltips event is dispatched correctly in your Livewire component. You can do this by using the dispatchBrowserEvent method:

    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.

Please or to participate in this conversation.