Home

Awesome

vue-simple-svg (V2)

A simple Vue.js plugin that allows you to use a component that dynamically loads a .svg file as an inline SVG so you can easily control its style programmatically. No jQuery required.

I recommend using vue-svg-loader for many cases when you just need to load a SVG file as a component. This plugin is built to cover some other cases the library doesn't fit, which are:

Installation:

$ npm install vue-simple-svg

Usage:

  1. initialize in your main file,
// as a plugin
import VueSimpleSVG from 'vue-simple-svg'
Vue.use(VueSimpleSVG)

// or as a component
import {SimpleSVG} from 'vue-simple-svg'
Vue.component('simple-svg', SimpleSVG)
  1. specify which elements in the SVG will be manipulated their fill and stroke colors by setting dedicated class names to them
<svg xmlns="http://www.w3.org/2000/svg">
  <g>
    <path class="fill-to-change" d="XXXXX XXXXX XXXXX" />
    <path class="stroke-to-change" d="XXXXX XXXXX XXXXX" />
  </g>
</svg>
  1. and use it in your component,
<simple-svg
  :src="svgSrc"
  fill-class-name="fill-to-change"
  :fill="svgFillColor"
  stroke-class-name="stroke-to-change"
  :stroke="svgFillColor"
  width="100%"
  height="100%"
  custom-id="my-id"
  custom-class-name="my-class"
  @load="svgLoaded()"
/>

Available props and events:

propstypedescriptiondefault
srcstringpath to your SVG file*required
fillClassNamestringclass name set to the elements in your SVG file whose fill color you want to change''
fillstringCSS-valid fill color value''
strokeClassNamestringclass name set to the elements in your SVG file whose stroke color you want to change''
strokestringCSS-valid stroke color value''
widthstringroot SVG element's style width'auto'
heightstringroot SVG element's style height'auto'
customIdstringroot SVG element's id''
customClassNamestringroot SVG element's class''
eventsdescription
@loadcalled when the inline SVG is generated

Notes:

Demo:

result

To run demo in your local environment,

$ npm run dev-demo

You can see the example of how to use simple-svg component at demo/components/SvgButton.vue

Reference: