How SVGR Converts SVG Files into React Components
SVGR is a specialized tool that automates the transformation of raw Scalable Vector Graphics (SVG) files into production-ready React components. Rather than manually copying SVG paths into JSX and adjusting attribute names, SVGR leverages an internal pipeline combining SVGO and Babel to parse, optimize, transform, and wrap SVG code into clean JavaScript or TypeScript components.
1. SVG Optimization via SVGO
The process begins by cleaning and optimizing the raw SVG source code using SVGO (SVG Optimizer). Raw SVG files exported from design software like Figma or Adobe Illustrator often contain redundant metadata, editor-specific tags, comments, hidden elements, and unoptimized path data. SVGO strips out this unnecessary markup, reduces coordinate precision to minimize file size, and unifies shapes to ensure the SVG is as lightweight and clean as possible before code transformation begins.
2. Parsing into an Abstract Syntax Tree (AST)
Once the SVG is optimized, SVGR parses the XML-based string into an Abstract Syntax Tree (AST). It converts the SVG markup into a Hypertext Abstract Syntax Tree (HAST). This tree structure represents every node, element, and attribute in the SVG as a JavaScript object, allowing SVGR to programmatically inspect, modify, and manipulate the document structure.
3. Transforming HTML/XML to JSX with Babel
React’s JSX uses camelCase conventions for attributes and properties,
whereas standard SVG uses kebab-case (e.g., stroke-width
vs. strokeWidth, fill-rule
vs. fillRule) along with attributes like class
instead of className. SVGR converts the HAST into a Babel
AST and applies transformation rules to map these XML attributes into
valid React JSX props. During this stage, SVGR can also replace fixed
values (such as width, height, or
fill) with dynamic React props like
currentColor or component-level configuration values.
4. Template Wrapping and Code Generation
After the JSX AST is constructed, SVGR wraps the vector markup inside a React functional component definition. Using customizable templates, SVGR can generate:
- Standard functional components.
- Components wrapped in
React.forwardRefto pass DOM references to the root<svg>tag. - Components augmented with custom props (e.g.,
titletags for accessibility). - Strongly typed TypeScript components with
SVGProps<SVGSVGElement>definitions.
Finally, @babel/generator compiles the AST back into
standard JavaScript or TypeScript code, formatting the output with tools
like Prettier if configured.
5. Integration Across Build Tools
SVGR delivers this transformation process through multiple
environments: * CLI: For batch-generating static
.jsx or .tsx files in a source directory. *
Webpack Loader (@svgr/webpack): For
importing .svg files directly as React components inside
application bundles on the fly. * Vite and Rollup
Plugins: For processing SVGs during modern ES module builds. *
Node API: For custom build scripts requiring
programmatic transformation.