项目作者: oliverfindl

项目描述 :
Vue plugin for inline replacement of SVG images with actual content of SVG files.
高级语言: JavaScript
项目地址: git://github.com/oliverfindl/vue-svg-inline-plugin.git
创建时间: 2019-11-11T19:27:09Z
项目社区:https://github.com/oliverfindl/vue-svg-inline-plugin

开源协议:MIT License

下载


vue-svg-inline-plugin

version
downloads
license
paypal

Vue plugin for inline replacement of SVG images with actual content of SVG files.

⚠ Reactive Vue bindings won’t be transfered to SVG replacement.

SVG files should be optimized beforehand (e.g.: using SVGO or SVGOMG).

Placeholder images should be optimized beforehand (e.g.: using pngquant or TinyPNG / TinyJPG).

Compatible with Vue@2 and Vue@3.


Table of contents:


Installation

Package managers

  • npm [package]:

    1. $ npm install vue-svg-inline-plugin --save
  • yarn [package]:

    1. $ yarn add vue-svg-inline-plugin

Legacy browsers

  • unpkg [package]:

    1. <script src="//unpkg.com/vue-svg-inline-plugin"></script>
  • jsDelivr [package]:

    1. <script src="//cdn.jsdelivr.net/npm/vue-svg-inline-plugin"></script>

Modern browsers

This version is not transpiled and does not include any polyfills.

  • unpkg [package]:

    1. <script src="//unpkg.com/vue-svg-inline-plugin/dist/vue-svg-inline-plugin-modern.min.js"></script>
  • jsDelivr [package]:

    1. <script src="//cdn.jsdelivr.net/npm/vue-svg-inline-plugin/dist/vue-svg-inline-plugin-modern.min.js"></script>

Usage

Webpack based Vue projects (e.g.: Webpack or Vue CLI) and Vite projects

  1. // Vue@2
  2. // import basic Vue app
  3. import Vue from "vue";
  4. import App from "./App.vue";
  5. // import Vue plugin
  6. import VueSvgInlinePlugin from "vue-svg-inline-plugin";
  7. // import polyfills for IE if you want to support it
  8. import "vue-svg-inline-plugin/src/polyfills";
  9. // use Vue plugin without options
  10. Vue.use(VueSvgInlinePlugin);
  11. // use Vue plugin with options
  12. VueSvgInlinePlugin.install(Vue, {
  13. attributes: {
  14. data: [ "src" ],
  15. remove: [ "alt" ]
  16. }
  17. });
  18. // initialize and mount Vue app
  19. new Vue({
  20. render: h => h(App),
  21. }).$mount("#app");
  1. // Vue@3
  2. // import basic Vue app
  3. import { createApp } from "vue";
  4. import App from "./App.vue";
  5. // import Vue plugin
  6. import VueSvgInlinePlugin from "vue-svg-inline-plugin";
  7. // import polyfills for IE if you want to support it
  8. import "vue-svg-inline-plugin/src/polyfills";
  9. // initialize Vue app
  10. const app = createApp(App);
  11. // use Vue plugin without options
  12. app.use(VueSvgInlinePlugin);
  13. // use Vue plugin with options
  14. app.use(VueSvgInlinePlugin, {
  15. attributes: {
  16. data: [ "src" ],
  17. remove: [ "alt" ]
  18. }
  19. });
  20. // mount Vue app
  21. app.mount("#app");

Browsers

  1. // Vue@2
  2. // use Vue plugin without options
  3. Vue.use(VueSvgInlinePlugin);
  4. // use Vue plugin with options
  5. VueSvgInlinePlugin.install(Vue, {
  6. attributes: {
  7. data: [ "src" ],
  8. remove: [ "alt" ]
  9. }
  10. });
  11. // initialize and mount Vue app
  12. new Vue({ /* options */ }).$mount("#app");
  1. // Vue@3
  2. // initialize Vue app
  3. const app = Vue.createApp({ /* options */ });
  4. // use Vue plugin without options
  5. app.use(VueSvgInlinePlugin);
  6. // use Vue plugin with options
  7. app.use(VueSvgInlinePlugin, {
  8. attributes: {
  9. data: [ "src" ],
  10. remove: [ "alt" ]
  11. }
  12. });
  13. // mount Vue app
  14. app.mount("#app");

Directive

Directive name can be changed via options.

v-svg-inline directive:

  1. <img v-svg-inline class="icon" src="./images/example.svg" alt="example svg image" />

Replaces into:

  1. <svg xmlns="http://www.w3.org/2000/svg" viewBox="..." class="icon" focusable="false" role="presentation" tabindex="-1">
  2. <path d="..."></path>
  3. <!-- ... -->
  4. </svg>

v-svg-inline directive with sprite modifier:

⚠ Note, that for now, the viewBox property is not being applied on the <svg> link node.
This can cause issues when having icons differently sized in your UI.
For the most icon-systems, you can add a viewBox="0 0 24 24" by yourself onto the <img> node or use attributes.add option.

Fixed in version 2.1.0, use attributes.clone option.

  1. <img v-svg-inline.sprite class="icon" src="./images/example.svg" alt="example svg image" />

Replaces into:

  1. <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="..." class="icon" focusable="false" role="presentation" tabindex="-1">
  2. <use xlink:href="#svg-inline-plugin-sprite-<NUMBER>" href="#svg-inline-plugin-sprite-<NUMBER>"></use>
  3. </svg>
  4. <!-- ... -->
  5. <!-- injected before body closing tag -->
  6. <svg xmlns="http://www.w3.org/2000/svg" style="display: none !important;">
  7. <symbol id="svg-inline-plugin-sprite-<NUMBER>" xmlns="http://www.w3.org/2000/svg" viewBox="...">
  8. <path d="..."></path>
  9. <!-- ... -->
  10. </symbol>
  11. </svg>

Lazy loading

This plugin supports lazy (down)loading of SVG files. To enable it, rename src attribute to data-src. Please also provide placeholder image, which should be located in src attribute to avoid broken image icons in browsers.

Configuration

Default options

  1. {
  2. directive: {
  3. name: "v-svg-inline",
  4. spriteModifierName: "sprite"
  5. },
  6. attributes: {
  7. clone: [ "viewbox" ],
  8. merge: [ "class", "style" ],
  9. add: [ {
  10. name: "focusable",
  11. value: false
  12. }, {
  13. name: "role",
  14. value: "presentation"
  15. }, {
  16. name: "tabindex",
  17. value: -1
  18. } ],
  19. data: [],
  20. remove: [ "alt", "src", "data-src" ]
  21. },
  22. cache: {
  23. version: "<PACKAGE_VERSION>",
  24. persistent: true,
  25. removeRevisions: true
  26. },
  27. intersectionObserverOptions: {},
  28. axios: null,
  29. xhtml: false
  30. }

Explanation

  • directive.name:
    Defines directive name (lowercase string), which marks images you want to replace with inline SVGs.

  • directive.spriteModifierName:
    Defines directive modifier name (lowercase string), which together with directive.name marks images you want to replace with inline SVGs using inline SVG sprites.

  • attributes.clone:
    Array of attributes (lowercase strings) which should be cloned into SVG link node if using inline SVG sprites.

  • attributes.merge:
    Array of attributes (lowercase strings) which should be merged.

  • attributes.add:
    Array of attributes (objects with name (lowercase string) and value (string) properties), which should be added. If attribute already exists, it will be merged or skipped depending on attributes.merge option.

  • attributes.data:
    Array of attributes (lowercase strings) which should be transformed into data-attributes. If data-attribute already exists, it will be merged or skipped depending on attributes.merge option.

  • attributes.remove:
    Array of attributes (lowercase strings) which should be removed.

  • cache.version:
    Defines cache version (lowercase string or number).

  • cache.persistent:
    Boolean. Cache downloaded SVG files into local storage.

  • cache.removeRevisions:
    Boolean. Remove previous cache revisions from local storage.

  • intersectionObserverOptions:
    Intersection observer options object for processing image nodes. This option is not validated. Official documentation.

  • axios:
    Axios instance with pre-configured options. If omitted, new axios instance (if axios available) will be created. Official documentation.

  • xhtml:
    Boolean. In XHTML mode attribute minimization is forbidden. Empty attributes are filled with their names to be XHTML-compliant (e.g.: disabled=”disabled”).

Notices

  • User-defined options are deep-merged with default options. Arrays are not concatenated.

  • Attributes options are executed in this order: clone > merge > add > data > remove.

Polyfills

Required polyfills for IE:

Examples


License

MIT