-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathbrowser-sprite.js
44 lines (37 loc) · 977 Bytes
/
browser-sprite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import BrowserSprite from 'svg-baker-runtime/src/browser-sprite';
import domready from 'domready';
const spriteNodeId = '__SVG_SPRITE_NODE__';
const spriteGlobalVarName = '__SVG_SPRITE__';
const isSpriteExists = !!window[spriteGlobalVarName];
// eslint-disable-next-line import/no-mutable-exports
let sprite;
if (isSpriteExists) {
sprite = window[spriteGlobalVarName];
} else {
sprite = new BrowserSprite({
attrs: {
id: spriteNodeId,
'aria-hidden': 'true'
}
});
window[spriteGlobalVarName] = sprite;
}
const loadSprite = () => {
/**
* Check for page already contains sprite node
* If found - attach to and reuse it's content
* If not - render and mount the new sprite
*/
const existing = document.getElementById(spriteNodeId);
if (existing) {
sprite.attach(existing);
} else {
sprite.mount(document.body, true);
}
};
if (document.body) {
loadSprite();
} else {
domready(loadSprite);
}
export default sprite;