Loads and caches an SVG file and injects it into the DOM.
npm install @firstandthird/svg-injector
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.
Custom events are fired on injection.
Event | Where | When |
---|---|---|
svgicons:loaded |
body |
SVG is injected into the DOM |
Loads an SVG file.
url
- {string} - A valid URL to the SVG file.
[callback]
{function} - Callback function called once the SVG is loaded.
loadSVG('image.svg', (svg) => {
console.log(svg); // prints the SVG content
});
Injects an SVG into the DOM. Calls loadSVG.
url
- {string} - A valid URL to the SVG file. Fires a svgicons:loaded
event when completed.
injectSVG('image.svg');