How to Increase height when child nodes are added in React Diagram?
In the Syncfusion® React Diagram Component, it’s possible to dynamically adjust the diagram’s height as new child nodes are added. This ensures that all nodes remain visible without the need for scrollbars. This is particularly useful in scenarios where nodes are added to the diagram dynamically, and you want the diagram to resize automatically.
Below is a code example demonstrating how to achieve this:
//function called when we add a new node dynamically using buttons.
function addchild() {
let parent =
diagramInstance.selectedItems.nodes.length > 0
? diagramInstance.selectedItems.nodes[0]
: diagramInstance.nodes[0];
// Define a new node with a unique ID and specified dimensions.
let newNode = { id: randomId(), width: 100, height: 50 };
diagramInstance.add(newNode);
// Create a new connector between the parent node and the new node.
let newCon = { id: randomId(), sourceID: parent.id, targetID: newNode.id };
diagramInstance.add(newCon);
// Execute the layout to arrange the nodes and connectors.
diagramInstance.doLayout();
// Calculate the current diagram height.
let diagramHeight = Number(diagramInstance.height);
let newlyAddedNode = diagramInstance.getObject(newNode.id);
// Calculate the bottom position of the newly added node.
let bottom = newlyAddedNode.offsetY + newlyAddedNode.height / 2;
// Adjust the diagram height if the new node extends beyond its current height.
if (newlyAddedNode && bottom > diagramHeight) {
diagramInstance.height = bottom + 20;
diagramInstance.dataBind();
}
}
Sample:Click Here
Conclusion
I hope you enjoyed learning on how to increase height when child nodes are added in React Diagram.
You can refer to our React Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
You can also explore our React Diagram example to understand how to create and manipulate data. For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!