-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-browser.js
72 lines (63 loc) · 2.07 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React from 'react';
import App from './src/components/App';
export const wrapRootElement = ({ element }) => <App>{element}</App>;
/* ***********************************************
** Customized code from gatsby-plugin-twitter ***
*********************************************** */
const injectTwitterScript = () => {
function addJS(jsCode) {
const s = document.createElement('script');
s.type = 'text/javascript';
s.innerText = jsCode;
document.getElementsByTagName('head')[0].appendChild(s);
}
addJS(`
window.twttr = (function(d, s, id) {
var js,
fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function(f) {
t._e.push(f);
};
return t;
})(document, "script", "twitter-wjs");
`);
};
let injectedTwitterScript = false;
const embedClasses = [
'.twitter-tweet',
'.twitter-timeline',
'.twitter-follow-button',
'.twitter-share-button',
].join(',');
export const onRouteUpdate = () => {
// If there's an embedded element, lazy-load the twitter script (if it hasn't
// already been loaded), and then run the twitter load function.
if (document.querySelector(embedClasses) !== null) {
if (!injectedTwitterScript) {
injectTwitterScript();
injectedTwitterScript = true;
}
window.twttr.ready((twttr) => {
twttr.events.bind('rendered', (event) => {
const container = event.target.closest('.twitterEmbed__container');
const { height } = event.target.getBoundingClientRect();
container.style.minHeight = `${height}px`;
container.classList.add('twitterEmbed--rendered');
});
});
if (
typeof twttr !== 'undefined'
&& window.twttr.widgets
&& typeof window.twttr.widgets.load === 'function'
) {
window.twttr.widgets.load(document.querySelectorAll('.twitterEmbed__container'));
}
}
};