npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg-inliner-util

v2.2.0

Published

Inline SVG elements in the DOM and load SVG Stores.

Downloads

276

Readme

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 Promise
svgInliner.loadStore(assetURL)
});

Usage Example:

svgInliner.loadStore('svg/main-svg-store.svg')
    .then(() => {
        console.log('svg store loaded!');
    })
    .catch(() => {
        console.log('svg store failed to load.');
    });
});

Example of what an SVG Store may look like:

<svg viewBox="0 0 100 100">
    <symbol viewBox="0 0 40 30" id="svg-rectangle">
        <title>Rectangle</title>
        <rect width="40" height="30"/>
    </symbol>

    <symbol viewBox="0 0 25.693 25.693" id="svg-poly-1">
        <title>Polygon</title>
        <polygon points="7.526,25.693 0.001,18.168 0.001,7.525 7.526,0 18.167,0 25.694,7.525 25.694,18.168 18.167,25.693"/>
    </symbol>

    <symbol viewBox="0 0 39.719 39.718" id="svg-circle">
        <title>Circle</title>
        <circle cx="19.859" cy="19.858" r="19.859"/>
    </symbol>
</svg>

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.process('.css-selector', true);

The HTML elements need to have a data-use attribute, which references an SVG element by ID.

Usage Example:

<!-- html element example -->
<div class="inline-svg" data-use="#svg-company-logo-01" data-inline-svg>Company Logo</div>
// js
svgInliner.process('.js-inline-svg');

You can also specify the WAI-ARIA role to set on the injected SVG using a data-role attribute (the default role is "img").

<div class="inline-svg" data-use="#svg-smiley-01" data-role="presentation" data-inline-svg>Smiley Face</div>

Inject after SVG Store is loaded

A useful pattern for injecting SVG elements after the SVG Store has successfully loaded:

svgInliner.loadStore('svg/main-svg-store.svg')
    .then(() => {
        svgInliner.process('[data-inline-svg]', true);
    })
    .catch(() => {
        console.error('error loading SVG Store');
    });
});

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