-
-
Save randy-allen/1e32b154b85075fe51708994911c75d6 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
class Svg { | |
constructor(name) { | |
let div = document.createElement('div'); | |
div.innerHTML = require('../../../../public/img/svg/' + name + '.svg'); // change to wherever your svg files are | |
let fragment = document.createDocumentFragment(); | |
fragment.appendChild(div); | |
this.svg = fragment.querySelector('svg'); | |
} | |
classes(classes) { | |
if (classes) { | |
this.svg.classList = ''; | |
classes.split(' ').forEach(className => { | |
this.svg.classList.add(className); | |
}); | |
} | |
return this; | |
} | |
width(width) { | |
if (width) { | |
this.svg.setAttribute('width', width); | |
} | |
return this; | |
} | |
height(height) { | |
if (height) { | |
this.svg.setAttribute('height', height); | |
} | |
return this; | |
} | |
toString() { | |
return this.svg.outerHTML; | |
} | |
} | |
export default { | |
props: ['name', 'classes', 'width', 'height'], | |
render(h) { | |
return h('div', { | |
domProps: { | |
classList: 'flex items-center', | |
innerHTML: new Svg(this.name) | |
.classes(this.classes) | |
.width(this.width) | |
.height(this.height) | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A guide to using this can be found here: http://randy.works/how-to-insert-svg-files-directly-into-your-vue-components