Skip to content

Commit b22f3b4

Browse files
authored
Merge pull request feathericons#374 from feathericons/update-sprite
Refactor buildSpriteString and update sprite docs
2 parents 920bd45 + 94531bb commit b22f3b4

5 files changed

Lines changed: 56 additions & 45 deletions

File tree

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,40 +168,58 @@ feather.icons.x.toSvg({ class: 'foo bar', 'stroke-width': 1, color: 'red' })
168168
See the [API Reference](#api-reference) for more information about the available properties and methods of the `feather` object.
169169

170170
### SVG Sprite
171-
A SVG Sprite is also provided, which can be used as following:
171+
172+
#### 1. Install
173+
174+
> **Note:** If you intend to use Feather with a CDN, you can skip this installation step.
175+
176+
Install with [npm](https://docs.npmjs.com/getting-started/what-is-npm).
177+
178+
```
179+
npm install feather-icons --save
180+
```
181+
182+
Or just copy [`feather-sprite.svg`](https://unpkg.com/feather-icons/dist/feather-sprite.svg) into your project directory.
183+
184+
#### 2. Use
185+
186+
In your HTML, you can include an icon like so:
187+
172188
```html
173-
<svg class="feather feather-[iconName]"
189+
<svg
174190
width="24"
175-
height="24"
191+
height="24"
176192
fill="none"
177193
stroke="currentColor"
178194
stroke-width="2"
179195
stroke-linecap="round"
180196
stroke-linejoin="round"
181197
>
182-
<use xlink:href="feather-sprite.svg#[iconName]"/>
198+
<use xlink:href="path/to/feather-sprite.svg#circle"/>
183199
</svg>
184200
```
185-
Where `iconName` is the name of the icon you want to display.
186201

187-
Same result but using a CSS class:
202+
> **Note:** `circle` in the above example can be replaced with any valid icon name. See the complete list of icon names at [feathericons.com](https://feathericons.com).
203+
204+
However, this markup can be simplified using a simple CSS class to avoid repetition of SVG attributes between icons.
205+
188206
```css
189207
.feather {
190-
width: 24px;
191-
height: 24px;
192-
stroke: currentColor;
193-
stroke-width: 2;
194-
stroke-linecap: round;
195-
stroke-linejoin: round;
208+
width: 24px;
209+
height: 24px;
210+
stroke: currentColor;
211+
stroke-width: 2;
212+
stroke-linecap: round;
213+
stroke-linejoin: round;
196214
fill: none;
197215
}
198216
```
217+
199218
```html
200-
<svg class="feather feather-[iconName]">
201-
<use xlink:href="feather-sprite.svg#[iconName]"/>
219+
<svg class="feather">
220+
<use xlink:href="path/to/dist/feather-sprite.svg#circle"/>
202221
</svg>
203222
```
204-
Prefer using CSS classes to keep things organized.
205223

206224
## API Reference
207225

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`builds sprite correctly 1`] = `
4-
"<svg xmlns=\\"http://www.w3.org/2000/svg\\">
5-
<defs>
6-
<symbol id=\\"icon1\\" viewBox=\\"0 0 24 24\\">
7-
<line x1=\\"23\\" y1=\\"1\\" x2=\\"1\\" y2=\\"23\\"></line><line x1=\\"1\\" y1=\\"1\\" x2=\\"23\\" y2=\\"23\\"></line>
8-
</symbol>
9-
<symbol id=\\"icon2\\" viewBox=\\"0 0 24 24\\">
10-
<circle cx=\\"12\\" cy=\\"12\\" r=\\"11\\"></circle>
11-
</symbol>
12-
</defs>
13-
</svg>"
14-
`;
3+
exports[`builds sprite correctly 1`] = `"<svg xmlns=\\"http://www.w3.org/2000/svg\\"><defs><symbol id=\\"icon1\\" viewBox=\\"0 0 24 24\\"><line x1=\\"23\\" y1=\\"1\\" x2=\\"1\\" y2=\\"23\\"></line><line x1=\\"1\\" y1=\\"1\\" x2=\\"23\\" y2=\\"23\\"></line></symbol><symbol id=\\"icon2\\" viewBox=\\"0 0 24 24\\"><circle cx=\\"12\\" cy=\\"12\\" r=\\"11\\"></circle></symbol></defs></svg>"`;

bin/build-sprite-string.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
import defaultAttrs from '../src/default-attrs.json';
22

3-
const svgStartTag = `<svg xmlns="${defaultAttrs.xmlns}">\n<defs>\n`;
4-
const svgEndTag = '</defs>\n</svg>';
5-
63
/**
7-
* Renders the inner sprites as SVG Symbols
8-
* @param {object} icons the icons object
9-
* @returns {string} the rendered string with SVG symbols
4+
* Build an SVG sprite string containing SVG symbols.
5+
* @param {Object} icons
6+
* @returns {string}
107
*/
118
function buildSpriteString(icons) {
129
const symbols = Object.keys(icons)
1310
.map(icon => toSvgSymbol(icon, icons[icon]))
1411
.join('');
1512

16-
return svgStartTag + symbols + svgEndTag;
13+
return `<svg xmlns="${defaultAttrs.xmlns}"><defs>${symbols}</defs></svg>`;
1714
}
1815

1916
/**
20-
* Renders a SVG symbol tag
21-
* @param {string} name The name of the icon
22-
* @param {string} contents The contents of the icon
23-
* @returns {string} the rendered SVG symbol
17+
* Create an SVG symbol string.
18+
* @param {string} name - Icon name
19+
* @param {string} contents - SVG contents
20+
* @returns {string}
2421
*/
2522
function toSvgSymbol(name, contents) {
26-
return `<symbol id="${name}" viewBox="${defaultAttrs.viewBox}">
27-
${contents}\n</symbol>\n`;
23+
return `<symbol id="${name}" viewBox="${defaultAttrs.viewBox}">${
24+
contents
25+
}</symbol>`;
2826
}
2927

3028
export default buildSpriteString;

bin/build-sprite.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ import path from 'path';
33
import icons from '../dist/icons.json';
44
import buildSpriteString from './build-sprite-string';
55

6-
const sprite = buildSpriteString(icons);
7-
86
const OUT_FILE = path.resolve(__dirname, '../dist/feather-sprite.svg');
97

108
console.log(`Building ${OUT_FILE}`); // eslint-disable-line no-console
119

12-
fs.writeFile(OUT_FILE, sprite, err => {
13-
if (err) throw err;
14-
});
10+
fs.writeFileSync(OUT_FILE, buildSpriteString(icons));

bin/build.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
#!/bin/bash
22

3+
# Process SVG files
34
./node_modules/.bin/babel-node bin/process-svgs.js
45

6+
# Create dist directory
57
./node_modules/.bin/rimraf dist
68
mkdir dist
9+
10+
# Build icons.json
711
./node_modules/.bin/babel-node bin/build-icons-json.js
12+
13+
# Build SVG sprite
814
./node_modules/.bin/babel-node bin/build-sprite.js
915

16+
# Create dist/icons directory
1017
./node_modules/.bin/rimraf dist/icons
1118
mkdir dist/icons
19+
20+
# Build SVG icons
1221
./node_modules/.bin/babel-node bin/build-svgs.js
1322

23+
# Build JavaScript library
1424
./node_modules/.bin/webpack --output-filename feather.js
1525
./node_modules/.bin/webpack --output-filename feather.min.js -p

0 commit comments

Comments
 (0)