Vue Inline SVG
Vue component loads an SVG source dynamically and inline <svg>
so you can manipulate the style of it with CSS or JS.
It looks like basic <img>
so you markup will not be bloated with SVG content.
Loaded SVGs are cached so it will not make network request twice.
Install
NPM
npm install vue-inline-svg
Register locally in your component
import InlineSvg from 'vue-inline-svg';
export default {
components: {
InlineSvg,
}
}
Or register globally in the root Vue instance
import Vue from 'vue';
import {InlineSvgPlugin} from 'vue-inline-svg';
Vue.use(InlineSvgPlugin);
import InlineSvg from 'vue-inline-svg';
Vue.component('inline-svg', InlineSvg);
CDN
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/vue-inline-svg"></script>
<script>
Vue.use(VueInlineSvg.InlineSvgPlugin);
Vue.component('inline-svg', VueInlineSvg.InlineSvgComponent);
new Vue({
});
</script>
Usage
<inline-svg
src="image.svg"
transformSource="transformSvg"
@loaded="svgLoaded($event)"
@unloaded="svgUnloaded()"
@error="svgLoadError($event)"
width="150"
height="150"
fill="black"
aria-label="My image"
></inline-svg>
Example
props
- src
Path to SVG file
Note: if you use vue-loader assets or vue-cli, then paths like '../assets/my.svg' will not be handled by file-loader automatically like vue-cli do for <img>
tag, so you will need to use it with require
:
<inline-svg :src="require('../assets/my.svg')"/>
Learn more:
- transformSource
Function to transform SVG source
This example create circle in svg:
<inline-svg :src="image.svg" :transformSource="transform"/>
....
const transform = (svg) => {
let point = document.createElementNS("http://www.w3.org/2000/svg", 'circle');
point.setAttributeNS(null, 'cx', '20');
point.setAttributeNS(null, 'cy', '20');
point.setAttributeNS(null, 'r', '10');
point.setAttributeNS(null, 'fill', 'red');
svg.appendChild(point);
return svg;
}
// For cleaner syntax you could use https://github.com/svgdotjs/svg.js
attributes
Other attributes will be passed to inlined <svg>
. Except attributes with false
or null
value.
events
- loaded
Called when SVG image is loaded and inlined.
Inlined SVG element passed as argument into the listener’s callback function.
<inline-svg @loaded="myInlinedSvg = $event"/>
- unloaded
Called when src
prop was changed and another SVG start loading.
<inline-svg @unloaded="handleUnloaded()"/>
- error
Called when SVG failed to load.
Error object passed as argument into the listener’s callback function.
<inline-svg @error="log($event)"/>
Comparison
- This module:
- vue-simple-svg: , does not cache network requests, has wrapper around svg, attrs passed to
<svg>
are limited, converts <style>
tag into style=""
attr - svg-loader uses different approach, it inlines SVG during compilation. It has pros that SVG is prerendered and no http request needed. But also it has cons that markup size grows, especially if you have same image repeated several times. (Discussed in #11)
License
MIT License