JSPM

react-svg

1.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 234567
  • Score
    100M100P100Q159257F
  • License MIT

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

Package Exports

  • react-svg

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-svg) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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