GithubHelp home page GithubHelp logo

react-svg-icons's Introduction

React SVG Icons

Use SVG icons as react components.

Usage

var Icon = require('react-svg-icons');

<Icon name='icon-name'
  width='256'
  height='256'
  color='#4cd695'/>

Configration

You need to configure an asset directory from which all SVG icons will be read. These will be inlined when you browserify your bundle. In your package.json add:

  "react-svg-icons": {
    "assetPath": "path/to/dir/containing/svgs"
  }

Options

name

Specifies which icon to use. Icon names are file paths relative to your assetPath, minus the .svg extension. If you have directory like:

my-icon.svg
nested/some other icon.svg

Your icons will be available under these names:

  • name="my-icon"
  • name="nested/some other icon"

width and height

Sets the width and height. By default the aspect ration will be preserved. You can use preserveAspectRatio to override this behavior. These properties have no effect when there is no viewBox set on the SVG element.

viewBox

Usually you don't have to set this property, unless the original SVG doesn't define a viewBox. In that case width and height won't work. You will have to set a viewBox like "0 0 originalWidth originalHeight"

preserveAspectRatio

Set this property to "none" if you want to squash the icon. You can set it to any other valid SVG attribute. Default is "xMidYMid meet".

color

Overrides fill and stroke styles with the provided color unless it is set to none in the original SVG. If you need a more fine grained control, you can pass a function for this property, which returns a color:

function chooseColor(type, original) {
  var colorString = '#000';

  // type is 'fill' or 'stroke'
  if (type === 'fill') {
    ...
  }

  // the original color in the svg
  if (original === '#fff') {
    ...
  }

  return colorString;
}

<Icon name='icon-name' color={chooseColor} />

other options

Of course you can pass any other standard property, such as style and event handlers.

react-svg-icons's People

Contributors

felixhageloh avatar vgrbr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-svg-icons's Issues

Doesn't support svg with style elements

I am using react-svg-icons and started encountering the error below when I added SVG files that contain style elements:

E.g.

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 12.6 7.8" style="enable-background:new 0 0 12.6 7.8;" xml:space="preserve">
<style type="text/css">
    .st0{fill:none;stroke:#41d6c3;stroke-width:2;stroke-miterlimit:10;}
</style>
<polyline id="XMLID_70_" class="st0" points="11.9,7.1 6.2,1.4 0.7,6.9 "/>
</svg>

However, if I change the SVG to put the style in-line, the error goes away:

E.g.

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 12.6 7.8" enable-background="new 0 0 12.6 7.8" xml:space="preserve">
<g>
  <polyline fill="none" stroke="#41d6c3" stroke-width="2" stroke-miterlimit="10" id="XMLID_70_" class="st0" points="11.9,7.1 6.2,1.4 0.7,6.9 "/>
</g>
</svg>

Error:

/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/lib/transformation/file/index.js:671
      throw err;
      ^

SyntaxError: unknown: Unexpected token (1:60)
> 1 | <svg {...params}viewBox="-12 11.3 6.1 10.7"><style>.st0{fill:#777677;}</style><path class="st0" d="M-11.3 22l-.7-.7 4.6-4.6L-12 12l.7-.7 5.4 5.4z"/></svg>
    |                                                             ^
    at Parser.pp.raise (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/parser/location.js:24:13)
    at Parser.pp.unexpected (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/parser/util.js:82:8)
    at Parser.pp.expect (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/parser/util.js:76:33)
    at Parser.pp.jsxParseExpressionContainer (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/plugins/jsx/index.js:301:8)
    at Parser.pp.jsxParseElementAt (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/plugins/jsx/index.js:370:30)
    at Parser.pp.jsxParseElementAt (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/plugins/jsx/index.js:362:30)
    at Parser.pp.jsxParseElement (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/plugins/jsx/index.js:398:15)
    at Parser.parseExprAtom (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/plugins/jsx/index.js:410:21)
    at Parser.pp.parseExprSubscripts (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:236:19)
    at Parser.pp.parseMaybeUnary (/Users/lmlewis/git/platform-ui/platform-ui/node_modules/react-svg-icons/node_modules/svg-to-react/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:217:19)

These SVG files are coming from our design team and I assume are valid SVG, but I don't know enough about SVG to be certain. Is it possible to have react-svg-icons support SVG files like these that contain style elements?

TypeError: Path must be a string.

I'm getting an error that seems like it might be related to the asset path.

TypeError: Path must be a string. Received undefined while parsing file: [app]/node_modules/react-svg-icons/index.js

I have the asset path defined in package.json like this:

    "react-svg-icons": {
      "version": "^0.2.0",
      "assetPath": "img/global/svg-icons"
    },

(I've also tried variations on that path with no luck)

and everything else defined in a component file as specified by the README.

Have you encountered this or have an idea what might be the problem?

This is with React v0.14.8, node v6.1.0, and npm 3.8.6.

Turn off manipulation of original svg file?

Hey buddy! I started using this, and love it al ready :)

I noticed that react svg icons (or possibly svg-to-react) changes the svg; it removed classes and it merged certain paths. Is it possible to cirmumvent this? I'm trying to do some CSS-magic on svg-files and this isn't helping :P Any ideas how I should do this?

Usage with Webpack

Hi,
I tried to import your module but got errors:

WARNING in ./~/recursive-readdir/~/minimatch/minimatch.js
Critical dependencies:
6:5-12 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/recursive-readdir/~/minimatch/minimatch.js 6:5-12

WARNING in ./~/babel-core/lib/transformation/file/index.js
Critical dependencies:
537:33-45 the request of a dependency is an expression
 @ ./~/babel-core/lib/transformation/file/index.js 537:33-45

WARNING in ./~/recursive-readdir/~/minimatch/LICENSE
Module parse failed: c:\WORKSPACE\JS\hbg\node_modules\recursive-readdir\node_modules\minimatch\LICENSE Line 1: Unexpected number
You may need an appropriate loader to handle this file type.
| Copyright 2009, 2010, 2011 Isaac Z. Schlueter.
| All rights reserved.
|
 @ ./~/recursive-readdir/~/minimatch ^\.\/.*$

WARNING in ./~/recursive-readdir/~/minimatch/README.md
Module parse failed: c:\WORKSPACE\JS\hbg\node_modules\recursive-readdir\node_modules\minimatch\README.md Line 1: Unexpected token ILLEGAL
You may need an appropriate loader to handle this file type.
| # minimatch
|
| A minimal matching utility.
 @ ./~/recursive-readdir/~/minimatch ^\.\/.*$

WARNING in ./~/babel-core/lib/transformation/file/plugin-manager.js
Critical dependencies:
141:19-31 the request of a dependency is an expression
 @ ./~/babel-core/lib/transformation/file/plugin-manager.js 141:19-31

WARNING in ./~/try-resolve/index.js
Critical dependencies:
5:24-31 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/try-resolve/index.js 5:24-31

WARNING in ./~/babel-core/lib/transformation/file/README.md
Module parse failed: c:\WORKSPACE\JS\hbg\node_modules\babel-core\lib\transformation\file\README.md Line 1: Unexpected token ILLEGAL
You may need an appropriate loader to handle this file type.
| ## File Transformation
|
| This is the File Transformation directory.
 @ ./~/babel-core/lib/transformation/file ^\.\/.*$

WARNING in ./~/babel-core/lib/transformation/file/options/README.md
Module parse failed: c:\WORKSPACE\JS\hbg\node_modules\babel-core\lib\transformation\file\options\README.md Line 1: Unexpected token ILLEGAL
You may need an appropriate loader to handle this file type.
| ## File Options
|
| This is the File Options directory.
 @ ./~/babel-core/lib/transformation/file ^\.\/.*$

WARNING in ./~/try-resolve/README.md
Module parse failed: c:\WORKSPACE\JS\hbg\node_modules\try-resolve\README.md Line 1: Unexpected token ILLEGAL
You may need an appropriate loader to handle this file type.
| # try-resolve
|
| ## Installation
 @ ./~/try-resolve ^\.\/.*$

ERROR in ./~/recursive-readdir/~/minimatch/test/caching.js
Module not found: Error: Cannot resolve module 'tap' in c:\WORKSPACE\JS\hbg\node_modules\recursive-readdir\node_modules\minimatch\test
 @ ./~/recursive-readdir/~/minimatch/test/caching.js 2:10-24

ERROR in ./~/recursive-readdir/~/minimatch/test/brace-expand.js
Module not found: Error: Cannot resolve module 'tap' in c:\WORKSPACE\JS\hbg\node_modules\recursive-readdir\node_modules\minimatch\test
 @ ./~/recursive-readdir/~/minimatch/test/brace-expand.js 1:10-24

ERROR in ./~/recursive-readdir/~/minimatch/test/extglob-ending-with-state-char.js
Module not found: Error: Cannot resolve module 'tap' in c:\WORKSPACE\JS\hbg\node_modules\recursive-readdir\node_modules\minimatch\test
 @ ./~/recursive-readdir/~/minimatch/test/extglob-ending-with-state-char.js 1:11-25

ERROR in ./~/recursive-readdir/~/minimatch/test/basic.js
Module not found: Error: Cannot resolve module 'tap' in c:\WORKSPACE\JS\hbg\node_modules\recursive-readdir\node_modules\minimatch\test
 @ ./~/recursive-readdir/~/minimatch/test/basic.js 6:10-24

ERROR in ./~/recursive-readdir/~/minimatch/test/defaults.js
Module not found: Error: Cannot resolve module 'tap' in c:\WORKSPACE\JS\hbg\node_modules\recursive-readdir\node_modules\minimatch\test
 @ ./~/recursive-readdir/~/minimatch/test/defaults.js 6:10-24

I'm using Webpack. Do you have an idea what's wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.