Skip to content

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

License

Notifications You must be signed in to change notification settings

firstandthird/svg-injector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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');