@firstandthird/svg-injector

2.2.0 • Public • Published

SVG Injector npm Build Status

Loads and caches an SVG file and injects it into the DOM.

Installation

npm install @firstandthird/svg-injector

Usage

In your project import the library:

import { loadSVG, injectSVG } from '@firstandthird/svg-injector'

The injectSVG method will be automatically called once the DOMContentLoaded event is fired using window.svgLocation as parameter.

Events

Custom events are fired on injection.

Event Where When
svgicons:loaded body SVG is injected into the DOM

API

loadSVG(url, [callback])

Loads an SVG file.

Parameters

url - {string} - A valid URL to the SVG file.

[callback] {function} - Callback function called once the SVG is loaded.

Example

loadSVG('image.svg', (svg) => {
  console.log(svg); // prints the SVG content
});

injectSVG(url)

Injects an SVG into the DOM. Calls loadSVG.

Parameters

url - {string} - A valid URL to the SVG file. Fires a svgicons:loaded event when completed.

Example

injectSVG('image.svg');

Readme

Keywords

Package Sidebar

Install

npm i @firstandthird/svg-injector

Weekly Downloads

9

Version

2.2.0

License

MIT

Unpacked Size

9.42 kB

Total Files

6

Last publish

Collaborators

  • alaguna
  • aleperez92
  • dawnerd
  • ecwillis
  • jga
  • orthagonal1