Last active
April 18, 2021 08:56
-
-
Save achhunna/cae74645f3ef1e7787c07bcdee163d18 to your computer and use it in GitHub Desktop.
Import svg inline and modify element to create icon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
function recursivelyRemoveFill(el) { | |
if (!el) { | |
return; | |
} | |
el.removeAttribute('fill'); | |
[].forEach.call(el.children, child => { | |
recursivelyRemoveFill(child); | |
}); | |
} | |
export default { | |
name: 'svg-icon', | |
props: { | |
icon: { | |
type: String, | |
default: null | |
}, | |
hasFill: { | |
type: Boolean, | |
default: false | |
}, | |
growByHeight: { | |
type: Boolean, | |
default: true | |
}, | |
}, | |
mounted() { | |
if (this.$el.firstElementChild.nodeName === 'svg') { | |
const svgElement = this.$el.firstElementChild; | |
// use `viewBox` attribute to get the svg's inherent width and height | |
const viewBox = svgElement.getAttribute('viewBox').split(' ').map(n => Number(n)); | |
const widthToHeight = (viewBox[2] / viewBox[3]).toFixed(2); | |
if (this.hasFill) { | |
// recursively remove all fill attribute of element and its nested children | |
recursivelyRemoveFill(svgElement); | |
} | |
// set width and height relative to font size | |
// if growByHeight is true, height set to 1em else width set to 1em and remaining is calculated based on widthToHeight ratio | |
if (this.growByHeight) { | |
svgElement.setAttribute('height', '1em'); | |
svgElement.setAttribute('width', `${widthToHeight}em`); | |
} else { | |
svgElement.setAttribute('width', '1em'); | |
svgElement.setAttribute('height', `${1 / widthToHeight}em`); | |
} | |
svgElement.classList.add('svg-class'); | |
} | |
} | |
} | |
</script> | |
<template> | |
<div v-html="require(`!html-loader!../assets/svg/${icon}.svg`)" class="svg-container"></div> | |
</template> | |
<style lang="scss" scoped> | |
.svg-container { | |
display: inline-flex; | |
} | |
</style> |
I use the icon prop to change toggle states, and changes in the category indicator icon of items that can be sorted in categories by the user.
The other props may change depending on the icon change. However, the one time I might have had to use it previously I just ended up fixing the inconsistent icon instead. So yeah, you could have just the watcher on the icon prop.
That's a cool use case. For the scope of the article, I will skip adding it since I just want to highlight basic functionality. Thanks for your feedback and input though! 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use the icon prop to change toggle states, and changes in the category indicator icon of items that can be sorted in categories by the user.
The other props may change depending on the icon change. However, the one time I might have had to use it previously I just ended up fixing the inconsistent icon instead. So yeah, you could have just the watcher on the icon prop.