How to Import SVG Files in React Diagram?
This article provides an explanation of how to import SVG files in Syncfusion® React Diagram. The example demonstrates how to add a button to facilitate importing an image from the local system. When the button is clicked, it triggers a file selection dialog that allows users to choose an image from their local storage.
Include the file input element in your HTML, configured to accept only SVG file types:
<input type="file" id="fileUpload" onChange={onFileChange} accept="image/svg+xml" />
Once an image is selected, the application processes the file upload. Then onFileChange method is invoked to handle the upload and add the SVG file as a node to the diagram. The following code, demonstrates how to import SVG files into diagram:
// function to import svg file to diagram
const onFileChange = (args) => {
const file = args.target.files[0];
if (file) {
// Create a new FileReader to read the content
const reader = new FileReader();
// Define a callback function to execute when the file reading is complete
reader.onload = (event) => {
const svgContent = event.target.result;
// Define the SVG node
const svgNode = {
id: 'svgNode',
offsetX: 250, offsetY: 250,
width: 200, height: 200,
shape: {
type: 'Native',
content: svgContent // Set SVG content
}
};
// Add the SVG node to the diagram
diagramInstance.add(svgNode);
};
reader.readAsText(file);
}
};
You can find a working example of this implementation in the provided sample on StackBlitz.
Sample - Click here for sample
Conclusion
I hope you enjoyed learning about how to import SVG files 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!