Skip to content

Commit

Permalink
fix(runtime): store global sprite object in window which allows to mo…
Browse files Browse the repository at this point in the history
…unt symbols in one place betwee

ISSUES CLOSED: #8
  • Loading branch information
kisenka committed Aug 15, 2017
1 parent d1c4b5b commit 13e3d47
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"domready": "1.0.8",
"escape-string-regexp": "1.0.5",
"loader-utils": "^1.1.0",
"svg-baker": "^1.2.10",
"svg-baker-runtime": "^1.2.98",
"svg-baker": "^1.2.12",
"svg-baker-runtime": "^1.3.0",
"url-slug": "2.0.0"
},
"devDependencies": {
Expand Down
28 changes: 13 additions & 15 deletions runtime/browser-sprite.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import BrowserSprite from 'svg-baker-runtime/src/browser-sprite';
import domready from 'domready';

const sprite = new BrowserSprite();
const globaVarName = '__SVG_SPRITE__';
const isSpriteExists = !!window[globaVarName];

const loadSprite = () => {
const svg = sprite.mount(document.body, true);
// eslint-disable-next-line import/no-mutable-exports
let sprite;

// :WORKAROUND:
// IE doesn't evaluate <style> tags in SVGs that are dynamically added to the page.
// This trick will trigger IE to read and use any existing SVG <style> tags.
//
// Reference: /~https://github.com/iconic/SVGInjector/issues/23
const ua = window.navigator.userAgent || '';
if (ua.indexOf('Trident') > 0 || ua.indexOf('Edge/') > 0) {
const styles = svg.querySelectorAll('style');
for (let i = 0, l = styles.length; i < l; i += 1) {
styles[i].textContent += '';
}
}
if (isSpriteExists) {
sprite = window[globaVarName];
} else {
sprite = new BrowserSprite();
window[globaVarName] = sprite;
}

const loadSprite = () => {
sprite.mount(document.body, true);
};

if (document.body) {
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5642,9 +5642,9 @@ supports-color@^3.1.0, supports-color@^3.1.2, supports-color@^3.2.3:
dependencies:
has-flag "^1.0.0"

svg-baker-runtime@^1.2.98:
version "1.2.98"
resolved "https://registry.yarnpkg.com/svg-baker-runtime/-/svg-baker-runtime-1.2.98.tgz#44304d92f6221310bc266ff6b17b73dec3c352a9"
svg-baker-runtime@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/svg-baker-runtime/-/svg-baker-runtime-1.3.0.tgz#ca54a2d2969e6ee590a22dfd07ed52b7d5e6ca9f"
dependencies:
deepmerge "1.3.2"
mitt "1.1.2"
Expand All @@ -5666,9 +5666,9 @@ svg-baker@^1.2.0:
query-string "^4.3.2"
traverse "^0.6.6"

svg-baker@^1.2.10:
version "1.2.10"
resolved "https://registry.yarnpkg.com/svg-baker/-/svg-baker-1.2.10.tgz#486a44434539142cedc583222c8f2788fd84a0af"
svg-baker@^1.2.12:
version "1.2.12"
resolved "https://registry.yarnpkg.com/svg-baker/-/svg-baker-1.2.12.tgz#ee219b36c44feb46a32f47278d19417bb64a8f82"
dependencies:
bluebird "^3.5.0"
clone "^2.1.1"
Expand Down

0 comments on commit 13e3d47

Please sign in to comment.