├── .gitignore
├── LICENSE
├── README.md
├── art
├── logo.png
└── logo.svg
├── index.js
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | *.log
3 | package-lock.json
4 | yarn.lock
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Daniel Hartmann
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | # Laravel Mix SVG Vue
12 |
13 | **Extension to inline SVG files with Vue.js and optimize them automatically with SVGO.**
14 |
15 | ## Installation
16 |
17 | ```sh
18 | # npm
19 | npm install laravel-mix-svg-vue
20 |
21 | # yarn
22 | yarn add laravel-mix-svg-vue
23 | ```
24 |
25 | Next require the extension inside your Laravel Mix config and call `svgVue()` in your pipeline:
26 |
27 | ```js
28 | // webpack.mix.js
29 | const mix = require('laravel-mix');
30 | require('laravel-mix-svg-vue');
31 |
32 | mix.js('resources/js/app.js', 'public/js')
33 | // .vue() // only necessary if you are using mix v6
34 | .svgVue();
35 | ```
36 |
37 | The last step is to import and register the Vue component, either for Vue 2 or 3. Notice the different imports for `SvgVue`:
38 |
39 | #### Vue 2
40 |
41 | ```js
42 | // e.g. app.js
43 | import Vue from 'vue';
44 | import SvgVue from 'svg-vue';
45 |
46 | Vue.use(SvgVue);
47 |
48 | const app = new Vue({
49 | el: '#app'
50 | });
51 | ```
52 |
53 | #### Vue 3
54 |
55 | ```js
56 | // e.g. app.js
57 | import { createApp } from 'vue';
58 | import SvgVue from 'svg-vue3';
59 |
60 | const app = createApp({});
61 |
62 | app.use(SvgVue);
63 |
64 | app.mount('#app');
65 | ```
66 |
67 | ## Usage
68 |
69 | To display your SVG files, all you need to do is pass the filename (and path if placed inside a subdirectory) to the Vue component:
70 |
71 | ```html
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | ```
81 |
82 | ## Options
83 |
84 | #### Default options
85 |
86 | If nothing is passed to the extension inside your Laravel Mix config, the following options will be used:
87 |
88 | ```js
89 | {
90 | svgPath: 'resources/svg',
91 | extract: false,
92 | svgoSettings: [
93 | { removeTitle: true },
94 | { removeViewBox: false },
95 | { removeDimensions: true }
96 | ]
97 | }
98 | ```
99 |
100 | #### Option details
101 |
102 | #### `svgPath`
103 |
104 | The path to your SVG files relative to the Laravel Mix config.
105 |
106 | #### `extract`
107 |
108 | If you wish to extract the SVG's to a separate file instead of including them in your main `app.js`, you can set this option to `true`. This will create a `svg.js` file which then needs to be loaded in your HTML. Make sure to load `app.js` before `svg.js`:
109 |
110 | ```html
111 |
112 |
113 | ```
114 |
115 | #### `svgoSettings`
116 |
117 | Plugin configuration passed to SVGO. [See here](https://github.com/svg/svgo#built-in-plugins) for a list of available settings.
118 |
119 | #### Options overview
120 |
121 | Option | Type | Default | Description
122 | ---|---|---|---
123 | `svgPath` | String | `resources/svg` | Path to your SVG files
124 | `extract` | Boolean | `false` | Separate the SVG's from your main bundle
125 | `svgoSettings` | Array | [{ removeTitle: true }, { removeViewBox: false }, { removeDimensions: true }] | SVGO settings
126 |
127 | ## Toggling or rendering icons inside lists
128 |
129 | Not really related to this extension, but when more than one `` icon is rendered inside a conditional state with `v-if` or `v-for`, a `key` attribute should be used to tell Vue that an element needs to change when any condition changes.
130 |
131 | While in most cases the cost for toggling elements with `v-show` should be preferred (also no need for a `key` attribute then), a simple example when toggling an icon with `v-if` inside a button could look like this:
132 |
133 | ```html
134 |
138 |
139 |
143 | ```
144 |
145 | Rendering lists could be handled like this:
146 |
147 | ```html
148 |