3.1.2 • Published 3 years ago

@thasmo/external-svg-polyfill v3.1.2

Weekly downloads
79
License
MIT
Repository
github
Last release
3 years ago

External SVG Polyfill npm CDN

Small and basic polyfill to support referencing external SVG files

Browser like IE9-IE11, Edge12, Safari 5.1-6, or UCWeb11 do not support referencing external files via location hash. Referencing external files can be especially useful when dealing with a technique called SVG sprites, where multiple SVG elements are stored within a single SVG file. It is possible to inline these SVG sprites into the HTML document itself, but this has several disadvantages i.e. caching issues, unneeded HTML file size growth, development inconveniences, etc. Externally referenced SVG sprites mitigate those problems but are not supported in the mentioned browsers.

external-svg-polyfill embeds referenced SVG files automatically.

Features

  • User-agent sniffing is used to determine feature support.
  • External SVG files are fetched via xhr and embedded while use elements are updated.
  • Cross-domain SVG files are fetched and embedded for all browsers to work around browser limitations.
  • To prevent naming collision issues, id attributes are updated to use a unique name.
  • Dynamically added SVG use elements are processed upon changes in the DOM.
  • Various lifecycle events get dispatched on relevant DOM elements.
  • Minified file size is below 5 KiB to keep the load, parse and execution times low.
  • Script is not self-executing, it needs to be called explicitly.
  • Project is written in TypeScript.

Setup

Installation

npm

npm install @thasmo/external-svg-polyfill

CDN

<script src="https://cdn.jsdelivr.net/npm/@thasmo/external-svg-polyfill@3/browser/bundle.min.js"></script>

Usage

Just define some svg use elements in the HTML markup and let external-svg-polyfill do the rest.

Static Website

<!doctype html>
<html>
    <head>
        <script src="https://cdn.jsdelivr.net/npm/@thasmo/external-svg-polyfill@2/browser/bundle.min.js"></script>
        <script>
            window.document.addEventListener('DOMContentLoaded', function() {
                new ExternalSvgPolyfill();
            });
        </script>
    </head>
    <body>
        <svg xmlns="http://www.w3.org/2000/svg">
            <use href="/static/sprite.svg#icon"></use>
        </svg>
    </body>
</html>

Web Application

import ExternalSvgPolyfill from '@thasmo/external-svg-polyfill';
new ExternalSvgPolyfill();

API

methoddescription
runRun the polyfill manually if the run option is set to false.
observeStart observing the DOM for changes if the observe option is set to false.
unobserveStop observing the DOM for changes.
destroyStop the polyfill, stop observing and restore the original markup.
detectRun browser detection manually if the detect option is set to false.

Options

optiondescriptiontypedefault
targetSVG use elements to process.string, SVGUseElement[], NodeListOf<SVGUseElement>svg use
contextElement within to search for SVG use elements.HTMLElementwindow.document.body ?: window.document.documentElement
rootElement to which prepend SVG embeds to.HTMLElementwindow.document.body ?: window.document.documentElement
runRun the polyfill on object instantiation.booleantrue
prefixPrefix id attribute values to ensure uniqueness. false won't change the attributes.booleantrue
detectDetect browser-support automatically. false will run the polyfill in any browser.booleantrue
observeObserve DOM changes within the context element and rerun the polyfill.booleantrue
crossdomainEmbed crossdomain SVG files for all browsers regardless.booleantrue
namespaceNamespace of the dispatched events.stringexternal-svg-polyfill
agentsArray of regular expressions matching relevant user agents to polyfill.RegExp[][/Edge\/12/, /Version\/6\.0.+Safari/, /UCBrowser\/11/]

Events

All event are prefixed with the namespace option, are bubbling and can be cancelled using event.preventDefault().

eventdescriptiondata
external-svg-polyfill.loadAn external SVG file is loaded via xhr.address
external-svg-polyfill.insertAn external SVG file is inserted.address, file
external-svg-polyfill.removeAn external SVG file is removed.address
external-svg-polyfill.applyAn SVG use element's href attribute is updated.address, identifier
external-svg-polyfill.revokeAn SVG use element's href attribute is restored.value

Compatibility

external-svg-polyfill can polyfill these browsers:

  • Internet Explorer 11 tested
  • UCWeb 11 untested

Alternatives

Some alternatives exist and, in fact, external-svg-polyfill was greatly inspired by them. Check them out!

Resources

Credits

BrowserStack
BrowserStack loves open source and provides free manual and automated testing for this project! ❤️

License

MIT license

FOSSA Status


forthebadge

3.1.2

3 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago