Skip to content

Instantly share code, notes, and snippets.

@randy-allen
Forked from JeffreyWay/InlineSvg.js
Last active August 1, 2019 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randy-allen/1e32b154b85075fe51708994911c75d6 to your computer and use it in GitHub Desktop.
Save randy-allen/1e32b154b85075fe51708994911c75d6 to your computer and use it in GitHub Desktop.
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)
}
});
}
};
@randy-allen
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment