@firstandthird/svg-injector
v2.2.0
Published
Loads and caches an SVG file and injects it into the DOM
Downloads
36
Readme
SVG Injector
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');