Created
February 23, 2023 10:24
-
-
Save danielstgt/ff336eb30982b947dd36fd6c2217d7fa to your computer and use it in GitHub Desktop.
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
<template> | |
<svg | |
v-bind="svgAttributes" | |
v-html="svgContent" | |
> | |
</svg> | |
</template> | |
<script> | |
export default { | |
props: ['icon'], | |
data() { | |
return { | |
svgMap: import.meta.glob('/resources/svg/**/*.svg', { as: 'raw', eager: true }), | |
} | |
}, | |
computed: { | |
iconPath: { | |
cache: false, | |
get() { | |
return this.icon.replace(new RegExp('.'.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1'), 'g'), '/'); | |
}, | |
}, | |
svgString() { | |
return this.svgMap['/resources/svg/' + this.iconPath + '.svg'] | |
}, | |
svgAttributes() { | |
if (! this.svgString) return {}; | |
let wrapper = document.createElement('div'); | |
wrapper.innerHTML = this.svgString; | |
let attributesList = wrapper.firstElementChild.attributes; | |
let attributes = {}; | |
Object.keys(attributesList).map(i => attributes[attributesList[i].name] = attributesList[i].value); | |
return attributes; | |
}, | |
svgContent() { | |
return this.svgString ? this.svgString.replace(/^<svg[^>]*>|<\/svg>$/g, '') : null; | |
}, | |
}, | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
app.js