UNPKG

1.54 kBMarkdownView Raw
1# react-svg-loader
2
3[![Build Status](https://travis-ci.org/boopathi/react-svg-loader.svg)](https://travis-ci.org/boopathi/react-svg-loader)
4
5## Install
6
7```sh
8npm i react-svg-loader
9```
10
11## Usage
12
13This outputs ES6+JSX code and to be used with `babel-loader`
14
15```js
16module.exports = {
17 loaders: [
18 {
19 test: /\.svg$/,
20 loader: 'babel!react-svg'
21 }
22 ]
23}
24```
25
26### Output
27
28of react-svg-loader
29
30```js
31import React from 'react';
32export default class extends React.Component {
33 render() {
34 return <svg>
35 ...
36 </svg>;
37 }
38}
39```
40
41and this should be passed through babel-loader
42
43### Options
44
45The ouput svg component takes in options that are defined in the svg
46
47### CLI
48
49The react-svg-loader comes with a cli (`svg2react`) that you can use to convert svg files to react components. Use this tool when you'd want to customize your svg component by hand. Otherwise the loader just works.
50
51```sh
52`npm bin`/svg2react file1.svg file2.svg
53```
54
55and the following files will be emitted
56
57+ `file1.react.svg`
58+ `file2.react.svg`
59
60in the SAME directory as the files
61
62## Assumptions and Other gotchas
63
64+ Root element is always `<svg>`
65+ All ids are preserved as it is
66+ `style` tags are parsed and outputted as objects
67+ `root`'s attributes are parsed and overridden by props
68+ namespace-d attributes (`myns:something`) are stripped
69+ Only tags allowed by react are retrieved. Others are simply ignored
70+ Order of the tags are maintained as it is
71+ Width and Height are added to svg component by default and set to 300 when not defined