Skip to content

Commit c0ce5f9

Browse files
committed
Update README
1 parent cd4263f commit c0ce5f9

File tree

3 files changed

+102
-39
lines changed

3 files changed

+102
-39
lines changed

README.md

Lines changed: 100 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Old version
66

7-
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
88

99
## Install
1010

@@ -14,9 +14,19 @@ npm i react-svg-loader
1414

1515
## Usage
1616

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+
```
1826

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.
2030

2131
```js
2232
// In your webpack config
@@ -26,7 +36,7 @@ This outputs ES2015 and JSX code and should be transpiled with babel or any othe
2636
}
2737
```
2838

29-
### without babel-loader
39+
### ES5 output
3040

3141
Pass loader query `es5=true`.
3242

@@ -40,23 +50,6 @@ Note: babel transform is applied with `react` and `es2015-loose` presets.
4050
}
4151
```
4252

43-
### Props to the output component pass through to the root svg element
44-
45-
```js
46-
import Image from './arrow.svg';
47-
// width and height will be passed to svg
48-
<Image width={100} height={100} /> // <svg width=100 height=100>
49-
```
50-
51-
### Props to the output component override the root svg element's prop
52-
53-
```js
54-
// input: arrow.svg
55-
// <svg width="16">
56-
import Image from './arrow.svg';
57-
<Image width={32}/> // <svg width="32">
58-
```
59-
6053
## Internals
6154

6255
<p align="center">
@@ -71,33 +64,106 @@ SVG Optimize using <a href="https://github.com/svg/svgo">SVGO</a>
7164
Babel Transform with `preset=react` and <a href="src/plugin.js">plugin=svgToComponent</a>
7265
</p>
7366

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+
<svg pointer-events="none">
75+
<path stroke-width="5"/>
76+
</svg>
77+
```
78+
79+
is transformed to
7580

7681
```html
77-
<svg width="50" namespaced:attr="unnecessary"/>
82+
<svg pointerEvents="none">
83+
<path strokeWidth="5"/>
84+
</svg>
7885
```
7986

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+
<svg style="text-align: center">
93+
<circle style="width: 10px"/>
94+
</svg>
95+
```
96+
97+
is transformed to
8198

8299
```html
83-
<svg width="50"/>
100+
<svg style={{textAlign: 'center'}}>
101+
<circle style={{width: '10px'}}/>
102+
</svg>
84103
```
85104

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.
108+
109+
```html
110+
<svg width="50">
111+
...
112+
</svg>
113+
```
114+
115+
is transformed to
116+
117+
```html
118+
<svg width={this.props.width ? this.props.width : '50'} {...this.props}>
119+
...
120+
</svg>
121+
```
122+
123+
#### 4. export React.Component
124+
125+
The loader should now export the svg component. And this is done by wrapping it in a Component Class.
126+
127+
```html
128+
<svg>...</svg>
129+
```
130+
131+
is transformed to
87132

88133
```js
89134
import React from 'react';
90135
export default class SVG extends React.Component {
91136
render() {
92-
return <svg width={this.props.width ? this.props.width : "50"} {...this.props} />;
137+
return <svg>...</svg>;
93138
}
94139
}
95140
```
96141

142+
### Example
143+
144+
##### Input SVG
145+
146+
```html
147+
<svg style='text-align: center; width: 100px' pointer-events="stroke">
148+
<circle cx="50" cy="50" r="25" style="text-align: center;" stroke-width="5" />
149+
</svg>
150+
```
97151

98-
## Options
152+
##### Output React Component
99153

100-
The ouput svg component takes in options that are defined in the svg
154+
```js
155+
import React from "react";
156+
export default class SVG extends React.Component {
157+
render() {
158+
return <svg
159+
style={{ textAlign: "center", width: "100px" }}
160+
pointerEvents={this.props.pointerEvents ? this.props.pointerEvents : "stroke"}
161+
{...this.props} >
162+
<circle cx="50" cy="50" r="25" style={{textAlign: "center"}} strokeWidth="5" />
163+
</svg>;
164+
}
165+
}
166+
```
101167

102168
## CLI
103169

@@ -112,20 +178,21 @@ and the following files will be emitted
112178
+ `file1.svg.react.js`
113179
+ `file2.svg.react.js`
114180

115-
in the SAME directory as the files
181+
in the **SAME directory** as the files
116182

117183
### CLI Options
118184

119-
`--es5`: Transforms ES2015+JSX output to ES5 using `presets=[es2015-loose, react]`
185+
+ `--es5`: Transforms ES2015+JSX output to ES5 using `presets=[es2015-loose, react]`
186+
+ `-0`: Outputs to STDOUT
120187

121188
```
122-
`npm bin`/svg2react file1.svg --es5
189+
`npm bin`/svg2react file1.svg --es5 -0
123190
```
124191

125192
## Assumptions and Other gotchas
126193

127194
+ Root element is always `<svg>`
128-
+ SVG is optimized
195+
+ SVG is optimized using SVGO
129196

130197
## LICENSE
131198

example/dummy.svg

Lines changed: 1 addition & 3 deletions
Loading

test/loader.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ test('compression: namespace attr', function(t) {
111111

112112
const circle = `
113113
<svg style='text-align: center; width: 100px;height:100px' fill="#ddd" pointer-events="stroke">
114-
<g>
115-
<circle cx="50" cy="50" r="25" style="text-align: center; stroke: #000000;" stroke-width="5" />
116-
</g>
114+
<circle cx="50" cy="50" r="25" style="text-align: center; stroke: #000000;" stroke-width="5" />
117115
</svg>
118116
`;
119117

0 commit comments

Comments
 (0)