|
1 | 1 | import defaultAttrs from '../src/default-attrs.json'; |
2 | 2 |
|
3 | | -const svgStartTag = `<svg xmlns="${defaultAttrs.xmlns}">\n<defs>\n`; |
4 | | -const svgEndTag = '</defs>\n</svg>'; |
5 | | - |
6 | 3 | /** |
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} |
10 | 7 | */ |
11 | 8 | function buildSpriteString(icons) { |
12 | 9 | const symbols = Object.keys(icons) |
13 | 10 | .map(icon => toSvgSymbol(icon, icons[icon])) |
14 | 11 | .join(''); |
15 | 12 |
|
16 | | - return svgStartTag + symbols + svgEndTag; |
| 13 | + return `<svg xmlns="${defaultAttrs.xmlns}"><defs>${symbols}</defs></svg>`; |
17 | 14 | } |
18 | 15 |
|
19 | 16 | /** |
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} |
24 | 21 | */ |
25 | 22 | 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>`; |
28 | 26 | } |
29 | 27 |
|
30 | 28 | export default buildSpriteString; |
0 commit comments