npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg-to-jsx

v1.0.4

Published

SVG to JSX converter

Downloads

97,764

Readme

svg-to-jsx

Build Status

Simple module that consumes SVG and spits out JSX. As simple as that.

Hey! If you're using gulp you might find gulp-svg-to-jsx interesting. And if you're using webpack you might like svg-jsx-loader that wraps this module for use as a webpack loader.

Installation

svg-to-jsx is a node module. To install you have to have Node.js and NPM installed on your machine.

npm install svg-to-jsx

Usage

You can either use the module in your Node.js project or via command line.

Use as a node module

var svgtojsx = require('svg-to-jsx');
var svg = '<svg version="1.1"><path id="myPath" style="font-family: Verdana; margin-bottom: 10px; -webkit-transition: all; ms-transition: all;"/></svg>';

// You can use svgtojsx with old school callbacks
svgtojsx(svg, function(error, jsx) {
    // ...
});

// The returned object is a promise though, you might prefer that
svgtojsx(svg).then(function(jsx) {
    // ...
});

Options

root String In case you only want to output single SVG element you can set this to its ID.

passProps Boolean Set this to true in case you want to pass props to the root element.

renderChildren Boolean|String Set this to true in case you want to render this.props.children in the root element. If set to string value, this value is interpreted as an element ID and children are rendered into this element. If element already has some text content children are appended to the end.

refs Object In case you want to be able to access specific elements from your SVG file, you can add refs to them. This object's keys are IDs of elements that will be assigned refs, the values are the ref names, for example:

{
    mySvgElement: 'refToMySvgElement'
}

will result in element with ID mySvgElement to be accessible via this.refs.refToMySvgElement.

Use from command line

# To output JSX to stdout
$ svg-to-jsx <path to an SVG file>

# To display usage info
$ svg-to-jsx --help
$ svg-to-jsx -h

# To output to file
$ svg-to-jsx -o <path to JSX file> <path to an SVG file>

Notes

<use/> tags are not allowed in JSX. The element referenced by a <use/> tag's xlink:href attribute is looked up, its id is discarded, and it replaces the original <use/> tag.

Suppose you have an SVG file with following structure:

<polygon id="mask-path" points="497,129 537.1,135.3 494.4,215.8"/>
<clipPath id="mask">
    <use xlink:href="#mask-path" overflow="visible"/>
</clipPath>
<g id="group" clip-path="url(#mask)">
	<!-- Group contents -->
</g>

Then of course React won't support <use/> tags and you would end up unmasked #group. So the <use/> tags are replaced and you end up with following structure which is supported by React:

<polygon id="mask-path" points="497,129 537.1,135.3 494.4,215.8"/>
<clipPath id="mask">
	<polygon points="497,129 537.1,135.3 494.4,215.8"/>
</clipPath>
<g id="group" clip-path="url(#mask)">
	<!-- Group contents -->
</g>

Testing

To run unit test just execute

npm test