svg-inliner-util
A utility module that allows you to load SVG Stores and inject SVG elements into the DOM.
If you're unfamiliar with the concept of "SVG Stores" (or "SVG Sprites" as some call them), read this CSS-Tricks article and checkout this Grunt plugin (or this Gulp plugin.
Dependencies
svg-inliner-util
doesn't really have any dependencies but it does utilise Promises.
Depending on your browser support, you might need a Promise polyfill/ponyfill (e.g. es6-promise).
Install
npm
npm install svg-inliner-util --save
bower
bower install svg-inliner-util --save
You can also download svgInliner.js
manually.
CommonJS
var svgInliner = require('svg-inliner-util');
AMD
define(['svg-inliner-util'], function(svgInliner) { }
Good ol' script tag
<script src="svgInliner.js"></script>
Load an SVG Store
Load an SVG Store (asynchronously) and add it to the DOM.
Options:
// Returns a PromisesvgInliner});
Usage Example:
svgInliner ;});
Example of what an SVG Store may look like:
Rectangle Polygon Circle
Inject Inline SVG elements
An alternative to using the <use xlink:href="#svg-my-icon">
technique.
Inject inline SVG elements into DOM nodes using the process()
method.
The process()
method uses css selectors to find target HTML elements.
svgInliner;
The HTML elements need to have a data-use
attribute, which references an SVG element by ID.
Usage Example:
<!-- html element example -->Company Logo
// jssvgInliner;
You can also specify the WAI-ARIA role
to set on the injected SVG using a data-role
attribute (the default role is "img").
Smiley Face
Inject after SVG Store is loaded
A useful pattern for injecting SVG elements after the SVG Store has successfully loaded:
svgInliner ;});
API
svgInliner.loadStore('svg/main-svg-store.svg')
Asynchronouly load a store asset and insert it into the DOM for further use.
svgInliner.process(selector, useRequestAnimationFrame, isFocusable)
Find DOM nodes and inject SVGs into them based on their attribute configuration.
useRequestAnimationFrame
is an optional boolean value you can use to specify whether you want to use window.requestAnimationFrame
when inserting SVG elements into the DOM.
isFocusable
is an optional boolean value that allows you to specify whether you want your inline SVGs to be focusable or not. The default value for this option is false
.
e.g. svgInliner.process('[data-inline-svg]', true);
Browser Support (incomplete)
IE9+
License
MIT