Skip to content

tgroutars/react-svg

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-svg

npm version npm downloads

A React component that uses SVGInjector to dynamically add SVG to the DOM.

table of contents

installation

$ npm install react-svg --save

There are also UMD builds available in the dist directory. If you use these, make sure you have already included React as a dependency.

example

import React from 'react';
import ReactDOM from 'react-dom';

import ReactSVG from '../src/index.js';

ReactDOM.render(
  <ReactSVG
    path={'atomic.svg'}
    className={'example'}
    callback={(svg) => console.log(svg)}
  />,
  document.querySelector('.Root')
);

To run the above example:

$ npm start

Then open a browser at localhost:8080.

api

Props

  • path - Path to the SVG.
  • className - Optional Class name to be added to the SVG.
  • evalScripts - Optional Run any script blocks found in the SVG (always, once, or never). Defaults to never.
  • fallbackPath - Optional Path to the fallback PNG.
  • callback - Optional Function to call after the SVG is injected. Receives the newly injected SVG DOM element as a parameter. Defaults to null.

Example

<ReactSVG
  path={'atomic.svg'}
  className={'example'}
  evalScript={'always'}
  fallbackPath={'atomic.png'}
  callback={(svg) => console.log(svg)}
/>

Refer to the SVGInjector configuration docs for more information.

tests

$ npm test

releasing

The release script for this module uses npm-version under the hood, so you should pass a semver string or release type as an argument.

For example, to publish a patch release:

$ npm run release -- patch

roadmap

react-svg does not currently support being rendered in Node. This is because SVGInjector uses XMLHttpRequest, something that Node does not have locally. It would be nice to rewrite SVGInjector to use something environment agnostic, like superagent or anything along those lines. This would allow react-svg to be rendered on both client and server.

license

MIT

About

A react.js svg component for loading svg files into the dom.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.4%
  • HTML 2.6%