Resize <g class legend> after truncating legend text

I’m using this to truncate legend entries that are longer then 10 characters

        add_legend_hover: function(figure) {
            var legendItems = document.querySelectorAll('.legendtext');
            legendItems.forEach(function(item) {
                var name = item.textContent;
                var truncatedName = name.length > 10 ? name.substring(0, 7) + '...' : name;
                item.textContent = truncatedName;
                item.dataset.originalName = name;

            });
            return window.dash_clientside.no_update
        }

However this does not change the fact, that the legend div (g svg) is still just as wide as it was before truncating. How can I resize it, according to the legendtexts content?

Peek 2024-03-21 15-51