-
-
Save calebporzio/623c536f7fff77861b8a37ec11a1d3da to your computer and use it in GitHub Desktop.
<template> | |
<div class="inline-block" v-html="require('icon-' + this.icon + '.svg')"></div> | |
</template> | |
<style module> | |
.svg { | |
fill: currentColor; | |
height: 1em; | |
margin-top: -4px; | |
vertical-align: middle; | |
width: 1em; | |
} | |
</style> | |
<script> | |
export default { | |
props: ['icon'], | |
mounted() { | |
this.$el.firstChild.classList.add(this.$style.svg) | |
this.$el.firstChild.removeAttribute('height') | |
this.$el.firstChild.removeAttribute('width') | |
} | |
} | |
</script> | |
// Here is the custom code for my webpack.mix.js file: | |
// Exclude svg icons | |
Mix.listen('configReady', function (config) { | |
const rules = config.module.rules; | |
const targetRegex = /(\.(png|jpe?g|gif)$|^((?!font).)*\.svg$)/; | |
for (let rule of rules) { | |
if (rule.test.toString() == targetRegex.toString()) { | |
rule.exclude = /\.svg$/; | |
break; | |
} | |
} | |
}); | |
// Use a custom loader for inline icons | |
mix.webpackConfig({ | |
module: { | |
rules: [{ | |
test: /\.svg$/, | |
use: [{ | |
loader: 'html-loader', | |
options: { | |
minimize: true | |
} | |
}] | |
}] | |
}, | |
resolve: { | |
modules: [ path.resolve(__dirname, 'resources/assets/svg') ] | |
} | |
}); | |
@calebporzio great article and nice solution!
A few things I've changed:
- removed
fill: currentColor;
because in my case Material Icons have svg with nested paths, where fill is set instead. In this case, I addedfill: currentColor
to relevant paths in that svg. - instead of modifying vue.config.js (I'm working on vue-cli based project where webpack files aren't exposed), I've changed my require param to
require(`!!html-loader!@/assets/icons/${icon}-icon.svg`)
, as mentioned by @beijer. This allows me to only use html-loader for icons, and leave img tags with svg sources alone. - I'm getting a Vue warning (
[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'add' of undefined"
) when using firstChild, therefore changed svg selection tothis.$el.querySelector('svg')
- I've set
<svg>
style todisplay: block
, which removes invisible margins, and svg icon fills parent element fully, and therefore there's no need to mess with alignment. - Added dynamic style prop to div wrapper:
:style="{'font-size': size + 'px'}"
Hope this helps someone.
Btw, in case anyone wants to modify vue.config.js
in order to configure webpack to use html-loader
for svg files on vue-cli based project, instead of using in require('!!html-loader!...)
:
// vue.config.js
module.exports = {
chainWebpack: (config) => {
const svgRule = config.module.rule('svg');
svgRule.uses.clear();
svgRule
.use('html-loader')
.loader('html-loader')
.options({
minimize: true,
});
},
};
👋 the regex needs to be updated for Laravel Mix 4, it should be /(\.(png|jpe?g|gif|webp)$|^((?!font).)*\.svg$)/
This is a pretty amazing idea, the main draw back is that it now adds the entire icon set that I'm using to app.js, instead of extracting the data from the specific file. So remember to either make a stripped down version of only the icons that you need.
- Install the html-loader package
npm i -D html-loader
- Add this to your webpack.mix.js (no other configuration is needed):
mix.webpackConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, 'resources/'),
},
},
});
- You can now do this:
<template>
<div v-html="require(`!!html-loader!@/svg/${icon}.svg`)" v-once></div>
</template>
<script>
export default {
props: {
name: {
default: '',
type: String,
},
},
mounted() {
this.$el.firstChild.classList.add(...this.$el.className.split(' '));
this.$el.outerHTML = this.$el.innerHTML;
},
}
</script>
Thanks for all the examples! Based on these, I came up with a mix of some proposals:
npm i -D html-loader
Also ensure you have the path set up correctly in webpack.config.js
or just use a relative import.
<template>
<div
v-html="require(`!!html-loader!@Icons/${name}.svg`)" class="svg-icon"
v-once
role="img"
></div>
</template>
<script>
export default {
props: {
name: {
default: '',
type: String,
required: true,
},
},
mounted() {
this.$el.firstChild.removeAttribute('width')
this.$el.firstChild.removeAttribute('height')
// Reassign class list and remove the div wrapper
this.$el.firstChild.classList.add(...this.$el.className.split(' '))
this.$el.outerHTML = this.$el.innerHTML
},
}
</script>
<style module>
.svg-icon {
height: 1em;
margin-top: -4px;
vertical-align: middle;
width: 1em;
}
</style>
Unfortunately, none of the examples worked for me as provided. Here's the solution I ended up with:
-
Install html-loader:
npm i -D html-loader
-
Update webpack.mix:
var path = require('path');
mix.alias({
'@icons': path.resolve(__dirname, 'resources/assets/svg/'),
});
- Create SvgIcon.vue component:
<template>
<div
v-html="require(`!!html-loader!@icons/${name}.svg`).default"
class="fill-current"
v-once
></div>
</template>
<script>
export default {
props: {
name: {
default: '',
type: String,
required: true,
},
},
mounted() {
this.$el.firstChild.removeAttribute('width')
this.$el.firstChild.removeAttribute('height')
// Reassign class list and remove the div wrapper
this.$el.firstChild.classList.add(...this.$el.className.split(' '))
this.$el.outerHTML = this.$el.innerHTML
},
}
</script>
Thanks for this gist and great comments guys.
I also wanted to remove the container div and just render a
<svg>
tag. Here's a neat little trick to achieve that:this.$el.outerHTML = this.$el.innerHTML
:)I also added a
v-once
directive, because it's a static component. Here's the code I'm using:Quick note: I'm using Zondicons that don't have the height and width attributes, that's why I don't have the lines that remove those attributes.