2.1.9 • Published 7 years ago

svg4everybody v2.1.9

Weekly downloads
71,106
License
CC0-1.0
Repository
github
Last release
7 years ago

SVG for Everybody

NPM Version Build Status

SVG for Everybody adds SVG External Content support to all browsers.

To use it now, include the script in your document.

<script src="/path/to/svg4everybody.js"></script>
<script>svg4everybody(); // run it now or whenever you are ready</script>

To support Internet Explorer 6-8, include the legacy script instead.

<script src="/path/to/svg4everybody.legacy.js"></script>
<script>svg4everybody(); // run it now or whenever you are ready</script>

As of v2.0.0, you must manually call svg4everybody(). If you are using an AMD/CommonJS dependency loader then you may call it within the callback closure.

IE 6-8 require you to put the script in the <head> in order to shiv <svg> and <use> elements. For best results in IE, set X-UA-Compatible to ie=edge. This can be done with a response header from the server or the following HTML in the <head>.

<meta http-equiv="x-ua-compatible" content="ie=edge">

Usage

Create an SVG image.

map.svg:

<svg xmlns="http://www.w3.org/2000/svg">
	<symbol id="codepen" viewBox="0 0 64 64">
		<title>CodePen</title>
		<path etc.../>
	</symbol>
	<symbol id="youtube" viewBox="0 0 64 64">
		<title>YouTube</title>
		<path etc.../>
	</symbol>
	<symbol id="twitter" viewBox="0 0 64 64">
		<title>Twitter</title>
		<path etc.../>
	</symbol>
</svg>

This spritemap displays fine in Chrome, Safari 7.1+, Firefox, and Opera. SVG for Everybody polyfills the experience in Safari 6, IE 6+, and Edge 12.

<svg role="img" title="CodePen">
	<use xlink:href="map.svg#codepen"/>
</svg>
<svg role="img" title="YouTube">
	<use xlink:href="map.svg#youtube"/>
</svg>
<svg role="img" title="Twitter">
	<use xlink:href="map.svg#twitter"/>
</svg>

IE 6-8 requires a trailing slash / when using a self-closing <use/> element.

3 SVG logos

Browsers not supporting SVG fallback to images.

<svg role="img" title="CodePen">
	<img src="map.svg.codepen.png">
</svg>
<svg role="img" title="YouTube">
	<img src="map.svg.youtube.png">
</svg>
<svg role="img" title="Twitter">
	<img src="map.svg.twitter.png">
</svg>

By default, fallback images point to a PNG file in the same location as the SVG, only with the # hash replaced by a . dot and then appended with a .png extension. If you want to change this behavior, you can define your own fallback.

svg4everybody({
	fallback: function (src, svg, use) {
		// src: current xlink:href String 
		// svg: current SVG Element 
		// use: current USE Element 

		return 'fallback.png'; // ok, always return fallback.png
	}
});

All <use> elements that are descendants of an <svg> are checked for external content. If you want to change this behavior, you can define your own validator.

svg4everybody({
	validate: function (src, svg, use) {
		// src: current xlink:href String 
		// svg: current SVG Element 
		// use: current USE Element 

		return true; // ok, everything is valid
	}
});

You can override whether the script polyfills External Content at all (polyfill), or whether SVG should even be used over fallback images (nosvg).

svg4everybody({
	nosvg: true, // shiv <svg> and <use> elements and use image fallbacks
	polyfill: true // polyfill <use> elements for External Content
});

Use of the nosvg option will requires you to use the legacy version of SVG for Everybody.

Implementation status

Modern browsers support external content in SVGs, except Edge. No frets; we can shim it.

OSBrowserSVGExternal ContentShimmed
*Chrome 21+
*Chrome 14-20
*Firefox 4+
*Opera 23+
*Opera Mini 8+
AndAnd. Browser 40+
AndAnd. Browser 4.1+
iOSiOS 8.1+
iOSiOS 6-7
OSXSaf 7.1+
OSXSaf 6
WinEdge 13
WinEdge 12
WinIE 9 - 11
WinIE 6 - 8

As you can see, all major browsers support external content.

We had been waiting on Edge, previously, but David Storey, Edge’s project manager assured us that native support for external content in SVGs was high on their checklist. We would track progress and vote for attention to this issue. Then, just as I predicted...

I have complete faith in the Microsoft Edge team and absolutely expect support to arrive within the next few months.

— Jon Neal (August, 2015)

All of our dreams came true.

Readability and accessibility

SVGs are compelling to use for many reasons, and one of them is their ease of accessibility.

Within your spritemap, have each sprite use a <title> element to identify itself.

<symbol id="codepen">
	<title>CodePen</title>
	<path etc.../>
</symbol>

When this sprite is used, its title will be read aloud in JAWS and NVDA. Then, within your document, each sprite may use a title attribute to identify itself.

<svg title="CodePen">
	<use xlink:href="map.svg#codepen"/>
</svg>

That title will be read aloud in VoiceOver and NVDA. At present, the title attribute is the only way to properly read aloud an SVG in VoiceOver. I’ll let you know if this changes.

All together, use the title attribute in your document and the title element in your SVG.

ARIA roles may also be used to provide even more information to assistive technology.

When a sprite is merely decoration, use role="presentation".

<a href="//twitter.com/jon_neal"><svg role="presentation">
	<use xlink:href="map.svg#twitter"/>
</svg> Find me on Twitter</a>

Otherwise, use role="img" and remember to add a description.

<a href="//twitter.com/jon_neal"><svg title="Find me on Twitter" role="img">
	<use xlink:href="map.svg#twitter"/>
</svg></a>

Futher reading

Optimized SVGs

SVG files, especially exported from vector tools, often contain tons of unnecessary data such as editor metadata, comments, hidden elements, and other stuff that can be safely removed without affecting SVG rendering result.

Use a tool like SVGO to optimize SVG spritemaps.

$ [sudo] npm install -g svgo
$ svgo spritemap.svg
@lidl/cake-web-css_framework@htmlfactory.cz/frontend-startergrunt-frontend-starter@siiron/sds-icons@cake-hub/test-frameworkvexx@space-designsystem/starter-kit-tailwind2runroom-library@infinitebrahmanuniverse/nolb-svg4unity-vue@everything-registry/sub-chunk-2858common-uxeurostar-base-stylesesds-libraryesds-docfe-angular-globaleasy-svgfeuxworksgp-boilerplate-environmentfsastylesrebirth-ui@economist/component-subscribe-messagelx_fork_fix_svg-spritemap-webpack-pluginlucije-global-component-librarykickoff-frameworkmillsmediaelementrmc-svg-loadersc-react-ionssalt-toolssalt-iconsir-trevorsvg-spritemap-webpack-plugintdm-angularnuxt-svg4everybody-moduleninelines-rbc-templateninelines-template-styleofficial-spacereact-conventionsreact-lightning-design-componentsreact-ionsreact-lightning-design-systemreact-lightning-design-system-with-remoter-app1tutti-ch-styleguide@pxwlab/katana-jl@plesk/ui-library@skilld/kaizen-svg-sprite@vaibhavp1725/react-ions@decidim/core@delement/ui@cake-hub/lidl-web-bootstrap_theme@cake-hub/schwarz-web-bootstrap_theme@cake-hub/web-css_framework@covisint/cui-idm-b2x@jdrfhq/spirit@kununu/keds@laterpay/assets@lighting-beetle/lighter-styleguide@liferay/portal-7.2@netapp/luci@wingsuit-designsystem/wingsuit@acronis/ui-kit@axa-ch/pod-rsv@axa-ch/style-guide@axa-ch/pod-myaxa-nextrel@axa-ch/alt-pod-myaxa@axa-ch/axa-ch-core@orangesk/orange-design-system@wide/polyfilleras-c-vue-neo-bas-c-ngas-c-vue-neo@gameinpl/beerpolyalexandria-adslotambassador-app@automattic/calypso-polyfills@chanzuckerberg/eds@northernbeat/prototypecdap-ui@muxy/svg-spritemap-webpack-plugin@logicline/react-lightning-design-systemboiler-room@petsmart-ui/sparky
2.1.9

7 years ago

2.1.8

7 years ago

2.1.7

8 years ago

2.1.6

8 years ago

2.1.5

8 years ago

2.1.4

8 years ago

2.1.3

8 years ago

2.1.2

8 years ago

2.1.0

8 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.0

9 years ago

0.0.2

10 years ago

0.0.1

10 years ago

0.0.0

10 years ago