D3 Tidy tree text goes beyond chart area

I am using D3 collapsible tree and I am referring this code

The problem is that I have texts that can vary in length depending on the data. As soon as I change the length of the text, the chart starts going beyond the chart area.

I am new to D3 and this is what I have tried – codesandbox

You can see that the root node is not displaying correctly. Additionally, the intermediate nodes text is not displaying in the correct way.
Is there a way around it? or can I adjust the length of the node in such a way that even if the texts get too long, I may be able to see it with a fixed node length