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
// or if you're passing all .svg files via react-svg-loader,
171
-
importImage2from'./image1.svg';
172
-
173
-
// and use it like any other React Component
174
-
<Image1 width={50} height={50}/>
175
-
<Image2 width={50} height={50}/>
176
-
```
177
-
178
-
### Loader output
179
-
180
-
By default the loader outputs ES2015 code (with JSX compiled to JavaScript using babel-preset-react). You can combine it with [babel-loader](https://github.com/babel/babel-loader) + [babel-preset-env](https://github.com/babel/babel-preset-env) to compile it down to your target.
181
-
182
-
```js
183
-
// In your webpack config
184
-
{
185
-
test:/\.svg$/,
186
-
use: [
187
-
{
188
-
loader:"babel-loader"
189
-
},
190
-
{
191
-
loader:"react-svg-loader",
192
-
options: {
193
-
jsx:true// true outputs JSX tags
194
-
}
195
-
}
196
-
]
197
-
}
198
-
```
199
-
200
-
### SVGO options
201
-
202
-
```js
203
-
{
204
-
test:/\.svg$/,
205
-
use: [
206
-
"babel-loader",
207
-
{
208
-
loader:"react-svg-loader",
209
-
options: {
210
-
svgo: {
211
-
plugins: [
212
-
{ removeTitle:false }
213
-
],
214
-
floatPrecision:2
215
-
}
216
-
}
217
-
}
218
-
]
219
-
}
220
-
```
221
-
222
152
## Internals
223
153
224
154
<palign="center">
@@ -233,140 +163,6 @@ SVG Optimize using <a href="https://github.com/svg/svgo">SVGO</a>
233
163
Babel Transform with <code>preset=react</code> and <ahref="src/plugin.js"><code>plugin=svgToComponent</code></a>
234
164
</p>
235
165
236
-
### Transformations
237
-
238
-
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)
239
-
240
-
#### 1. Hyphenated attributes to camelCase
241
-
242
-
```html
243
-
<svgpointer-events="none">
244
-
<pathstroke-width="5"/>
245
-
</svg>
246
-
```
247
-
248
-
is transformed to
249
-
250
-
```html
251
-
<svgpointerEvents="none">
252
-
<pathstrokeWidth="5"/>
253
-
</svg>
254
-
```
255
-
256
-
#### 2. Style attr string to object
257
-
258
-
React expects style attribute value to be an object. Also, Hyphenated style names are converted to camel case.
259
-
260
-
```html
261
-
<svgstyle="text-align: center">
262
-
<circlestyle="width: 10px"/>
263
-
</svg>
264
-
```
265
-
266
-
is transformed to
267
-
268
-
```html
269
-
<svgstyle={{textAlign:'center'}}>
270
-
<circlestyle={{width:'10px'}}/>
271
-
</svg>
272
-
```
273
-
274
-
#### 3. Propagate props to root element
275
-
276
-
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.
277
-
278
-
```html
279
-
<svgwidth="50">
280
-
...
281
-
</svg>
282
-
```
283
-
284
-
is transformed to
285
-
286
-
```html
287
-
<svgwidth="50"{...props}>
288
-
...
289
-
</svg>
290
-
```
291
-
292
-
#### 4. class to className & class values to styles prop
The 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.
345
-
346
-
```sh
347
-
`npm bin`/svg2react file1.svg file2.svg
348
-
```
349
-
350
-
and the following files will be emitted
351
-
352
-
+`file1.svg.react.js`
353
-
+`file2.svg.react.js`
354
-
355
-
in the **SAME directory** as the files
356
-
357
-
### CLI Options
358
-
359
-
+`--jsx`: Outputs JSX code instead of compiling it to JavaScript using babel-preset-react
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)
41
+
42
+
#### 1. Hyphenated attributes to camelCase
43
+
44
+
```html
45
+
<svgpointer-events="none">
46
+
<pathstroke-width="5"/>
47
+
</svg>
48
+
```
49
+
50
+
is transformed to
51
+
52
+
```html
53
+
<svgpointerEvents="none">
54
+
<pathstrokeWidth="5"/>
55
+
</svg>
56
+
```
57
+
58
+
#### 2. Style attr string to object
59
+
60
+
React expects style attribute value to be an object. Also, Hyphenated style names are converted to camel case.
61
+
62
+
```html
63
+
<svgstyle="text-align: center">
64
+
<circlestyle="width: 10px"/>
65
+
</svg>
66
+
```
67
+
68
+
is transformed to
69
+
70
+
```html
71
+
<svgstyle={{textAlign:'center'}}>
72
+
<circlestyle={{width:'10px'}}/>
73
+
</svg>
74
+
```
75
+
76
+
#### 3. Propagate props to root element
77
+
78
+
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.
79
+
80
+
```html
81
+
<svgwidth="50">
82
+
...
83
+
</svg>
84
+
```
85
+
86
+
is transformed to
87
+
88
+
```html
89
+
<svgwidth="50"{...props}>
90
+
...
91
+
</svg>
92
+
```
93
+
94
+
#### 4. class to className & class values to styles prop
0 commit comments