-
Notifications
You must be signed in to change notification settings - Fork 20
/
gatsby-browser.js
48 lines (41 loc) · 1.28 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
require("prismjs/themes/prism-okaidia.css");
require("./src/theme/assets/scss/theme.scss");
require('./src/theme/assets/fontawesome/css/all.css');
const React = require('react');
const Layout = require('./src/components/Layout').default;
const NodeProvider = require('./src/components/NodeProvider').default;
/**
* Ensures the chrome doesn't rerender every page load, which makes the sidebar reset its scroll.
*/
exports.wrapPageElement = ({ element, props }) => {
return (
<NodeProvider {...props}>
<Layout {...props}>
{element}
</Layout>
</NodeProvider>
);
};
exports.onRouteUpdate = ({location}) => {
if (window.ga && process.env.NODE_ENV === 'production') {
window.ga('send', 'pageview');
}
anchorScroll(location);
return true;
};
exports.shouldUpdateScroll = ({
routerProps: { location },
}) => {
anchorScroll(location);
return true;
}
const anchorScroll = location => {
// Check for location so build does not fail
if (location && location.hash) {
setTimeout(() => {
const item = document.querySelector(`${location.hash}`).offsetTop;
const mainNavHeight = document.querySelector(`header`).offsetHeight;
window.scrollTo({top: item - mainNavHeight, left: 0, behavior: 'instant'});
}, 0);
}
}