README.md 3.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Inline SVG

This module is the successor of [gulp-inline-svg](https://gitlab.sgalinski.de/toolchain/gulp-inline-svg), rewritten as a standalone library.

The purpose of this moudle is to pass it a location to a folder containing SVG icons and get back a string containing inline-svg codes that can be used as ```background-image```. The output can be customized with a mustache-template. By default it will return a string that can be saved as a Sass-partial, offering a mixin and variables for each icon. By providing your own template, you can customize the output any way you want, though.

## Compatibility

The generated SVG inline code is URL encoded, so it should be safe to use starting with IE 9.

## Important notes

The generated mixin contains width and height values for each SVG. Those values are taken from the width and height attribute inside the SVG. If it does not provide them, they will be set to 0px. You can still overwrite them in your CSS.

## Usage

### Install the inline-svg module

```bash
npm i @sgalinski/inline-svg
```

### Example for usage with Sass

```js
const InlineSvg = require('@sgalinsk/inline-svg');
const fs = require('fs');
(async () => {
    const sassTemplate = await new InlineSvg('./svgs');
    fs.writeFileSync('./_svg.scss', sassTemplate);
})();

```

### Configuration

#### Use your own template

If you don't want to use the default Sass-template, you can esaily write your own and pass it as a configuration option:

```js
const InlineSvg = require('@sgalinsk/inline-svg');
const fs = require('fs');
(async () => {
    const sassTemplate = await new InlineSvg('./svgs', {
        template: './my-template.mustache'
    });
    fs.writeFileSync('./_svg.scss', sassTemplate);
})();
```

#### Pass additional variables

If needed, you can pass additional variables to the mustache template:

```js
const InlineSvg = require('@sgalinsk/inline-svg');
const fs = require('fs');
(async () => {
    const sassTemplate = await new InlineSvg('./svgs', {
        template: './my-template.mustache',
        context: {
            message: '// GENERATED BY gulpfile.js'
        }
    });
    fs.writeFileSync('./_svg.scss', sassTemplate);
})();
```

And then use it in your template:

```mustache
{{{message}}}

{{#svgs}}
${{{name}}}: "{{{inline}}}" {{width}} {{height}};
{{/svgs}}

@mixin inline-svg($name) {
    background: transparent url(nth($name, 1)) no-repeat 50% 50%;
    background-size: 100%;
    width: nth($name, 2);
    height: nth($name, 3);
}

@function inline-svg-width($name) {
    @return nth($name, 2);
}

@function inline-svg-height($name) {
    @return nth($name, 3);
}
```

#### Customize variables

If you want to use your own naming convention for the SVG-variables, you can pass an interceptor-function:

```js
const InlineSvg = require('@sgalinsk/inline-svg');
const fs = require('fs');
(async () => {
    const sassTemplate = await new InlineSvg('./svgs', {
        interceptor: function (svgData) {
            return Object.assign(svgData, { variableName: svgData.name.toLowerCase(), prefix: 'dso-icon' });
        }
    });
    fs.writeFileSync('./_svg.scss', sassTemplate);
})();
```