You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now `master` represents the new rewrite of the react-svg loader. Though this gives the exact same output as of the previous one, the entire parsing is changed. So if you'd like to continue using the old one, it's in [`v0.1` branch](https://github.com/boopathi/react-svg-loader/tree/v0.1) and `~0.1.0` on npm
7
+
Now `master` represents the new rewrite of the react-svg loader. Though this gives the exact same output as the previous one, the entire parsing is changed. So if you'd like to continue using the old one, it's in [`v0.1` branch](https://github.com/boopathi/react-svg-loader/tree/v0.1) and `^0.1.0` on npm
8
8
9
9
## Install
10
10
@@ -14,9 +14,19 @@ npm i react-svg-loader
14
14
15
15
## Usage
16
16
17
-
### with babel-loader
17
+
```js
18
+
var Image1 =require('react-svg?es5=1!./image1.svg');
19
+
// or
20
+
var Image2 =require('babel!react-svg!./image2.svg');
21
+
22
+
// and use it as
23
+
<Image1 width={50} height={50}/>
24
+
<Image2 width={50} height={50}/>
25
+
```
18
26
19
-
This outputs ES2015 and JSX code and should be transpiled with babel or any other transpiler that supports ES2015 and JSX.
27
+
### ES2015 + JSX output
28
+
29
+
By default the loader outputs ES2015 and JSX code and should be transpiled with babel or any other transpiler that supports ES2015 and JSX.
20
30
21
31
```js
22
32
// In your webpack config
@@ -26,7 +36,7 @@ This outputs ES2015 and JSX code and should be transpiled with babel or any othe
26
36
}
27
37
```
28
38
29
-
### without babel-loader
39
+
### ES5 output
30
40
31
41
Pass loader query `es5=true`.
32
42
@@ -40,23 +50,6 @@ Note: babel transform is applied with `react` and `es2015-loose` presets.
40
50
}
41
51
```
42
52
43
-
### Props to the output component pass through to the root svg element
### Props to the output component override the root svg element's prop
52
-
53
-
```js
54
-
// input: arrow.svg
55
-
// <svg width="16">
56
-
importImagefrom'./arrow.svg';
57
-
<Image width={32}/>// <svg width="32">
58
-
```
59
-
60
53
## Internals
61
54
62
55
<palign="center">
@@ -71,33 +64,106 @@ SVG Optimize using <a href="https://github.com/svg/svgo">SVGO</a>
71
64
Babel Transform with `preset=react` and <ahref="src/plugin.js">plugin=svgToComponent</a>
72
65
</p>
73
66
74
-
#### Input svg
67
+
### Transformations
68
+
69
+
Going from bottom up, the following transformations are applied and the same can be checked in the partly annotated source - [babel-plugin](src/plugin.js)
70
+
71
+
#### 1. Hyphenated attributes to camelCase
72
+
73
+
```html
74
+
<svgpointer-events="none">
75
+
<pathstroke-width="5"/>
76
+
</svg>
77
+
```
78
+
79
+
is transformed to
75
80
76
81
```html
77
-
<svgwidth="50"namespaced:attr="unnecessary"/>
82
+
<svgpointerEvents="none">
83
+
<pathstrokeWidth="5"/>
84
+
</svg>
78
85
```
79
86
80
-
#### SVG Optimize
87
+
#### 2. Style attr string to object
88
+
89
+
React expects style attribute value to be an object. Also, Hyphenated style names are converted to camel case.
90
+
91
+
```html
92
+
<svgstyle="text-align: center">
93
+
<circlestyle="width: 10px"/>
94
+
</svg>
95
+
```
96
+
97
+
is transformed to
81
98
82
99
```html
83
-
<svgwidth="50"/>
100
+
<svgstyle={{textAlign:'center'}}>
101
+
<circlestyle={{width:'10px'}}/>
102
+
</svg>
84
103
```
85
104
86
-
#### Babel transform
105
+
#### 3. Propagate props to root element
106
+
107
+
The props passed to the output component is passed on to the root SVG node and the props already defined are overridden by the props passed.
0 commit comments