Convert JSX to SVG
An icon written for React is not a valid .svg file. Browsers and design tools want class instead of className, stroke-width instead of strokeWidth, and style as a CSS string. This JSX to SVG converter changes them back. The Sample component:
export const CheckIcon = (props) => (
<svg xmlns="http://www.w3.org/2000/svg" className="icon" viewBox="0 0 24 24" {...props}>
<circle cx={12} cy={12} r={9} fill="none" stroke="currentColor" strokeWidth={2} />
<path
d="M8 12.5l2.5 2.5L16 9.5"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
style={{ opacity: 0.9 }}
/>
</svg>
);becomes:
<svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" stroke-width="2" />
<path
d="M8 12.5l2.5 2.5L16 9.5"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
style="opacity: 0.9"
/>
</svg>What Is Converted
- className to class, htmlFor to for, tabIndex to tabindex.
- camelCase attributes to their SVG names: strokeWidth to stroke-width, fillRule to fill-rule, xlinkHref to xlink:href.
- SVG attributes that really are camelCase, such as viewBox, gradientUnits and stdDeviation, are kept.
- Fixed values in braces: {12} becomes "12", {"M0 0"} becomes "M0 0", {false} becomes "false".
- Style objects to CSS: {{ strokeWidth: 2, fontSize: 12 }} becomes stroke-width: 2; font-size: 12px, adding px where React would.
- {...props}, key, ref, event handlers such as onClick and JSX comments are removed.
With Add xmlns on, the SVG namespace is added when it is missing, and the xlink namespace when xlink:href is used, so the file opens on its own.
Dynamic Values Are Listed, Not Guessed
A value that comes from a variable or a calculation has no fixed value in the code, so it cannot be written into a file. The tool leaves it out and lists it. For example:
<svg viewBox="0 0 24 24">
<path d="M4 12h16" stroke={color} strokeWidth={size / 8} />
</svg>gives:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M4 12h16" />
</svg>and the What changed box shows stroke={color} and strokeWidth={size / 8} under Skipped dynamic values. Replace them with real values in the JSX, or add the attributes to the SVG by hand.
TSX, Typed Props and Conditional Parts
TypeScript does not get in the way. The tool looks for the first <svg> tag, so type annotations, props interfaces and the arrow function around it are skipped. Only the <svg> element is converted. Take this icon:
const Icon = ({ size }: Props) => (
<svg width={24} height={24} viewBox="0 0 24 24">
<g clipPath="url(#a)">
<path d="M0 0h24" fillRule="evenodd" />
</g>
<defs>
<clipPath id="a"><rect width="24" height="24" /></clipPath>
</defs>
</svg>
);The result keeps <clipPath> as a tag name and viewBox as it is, but writes the attributes as clip-path and fill-rule. Tag names and attribute names are treated differently on purpose, because SVG itself uses a camelCase tag called clipPath and a hyphenated attribute called clip-path.
Parts that depend on code, such as {cond && <path />} or text like {name}, cannot be turned into a fixed file. They are dropped and shown under Skipped dynamic values, so you see exactly what to fill in by hand. If the input holds more than one <svg>, only the first is converted.
Common Problems
- The output has no colors: the JSX used fill={color} or stroke={color}. Put a real value such as "#1a73e8" or "currentColor" in the JSX first.
- The icon looks tiny or huge: the JSX had width={size}. Set width and height, or keep only viewBox so the SVG scales to its container.
- Nothing happens and you see No <svg> element found: the pasted code has no <svg> tag. Check that you copied the component that holds the markup, not only its wrapper.
- The file works in a browser but a design tool rejects it: turn on Add xmlns, which adds the namespace attribute many tools expect.
Related SVG Tools
Go back to React with SVG to JSX or the swap button. Turn the SVG into an image with SVG to PNG, or into an Android vector drawable with SVG to XML.
Privacy
Your code is converted in your browser and never uploaded. See the Privacy Policy.