-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-browser.js
50 lines (42 loc) · 1.08 KB
/
gatsby-browser.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
45
46
47
48
49
50
const getOptions = require("./opts");
const injectBodyMovinScript = ({ renderer, lottieVersion }) => {
const s = document.createElement("script");
s.type = "text/javascript";
s.src = `https://cdnjs.cloudflare.com/ajax/libs/bodymovin/${lottieVersion}/${getScriptType(
renderer
)}`;
document.getElementsByTagName("head")[0].appendChild(s);
};
const getScriptType = renderer => {
switch (renderer) {
case "svg":
case "html":
case "canvas":
return `lottie_${renderer}.min.js`;
default:
return "lottie.min.js";
}
};
let injectedScript = false;
exports.onRouteUpdate = (p, pluginOptions) => {
if (window.lottie) {
window.lottie.destroy();
}
const elements = document.querySelectorAll(".lottie");
if (elements.length) {
elements.forEach(el => {
// clear the current SVG placeholder so lottie can do its thing
while (el.firstChild) {
el.removeChild(el.firstChild);
}
});
if (!injectedScript) {
injectBodyMovinScript(getOptions(pluginOptions));
injectedScript = true;
} else {
if (window.lottie) {
window.lottie.searchAnimations();
}
}
}
};