-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-browser.js
47 lines (41 loc) · 1.22 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
export { wrapPageElement } from './gatsby-shared';
export const onRouteUpdate = ({ location, prevLocation }) => {
const newPath = location.pathname;
const oldPath = prevLocation ? prevLocation.pathname : null;
/**
* We shouldn't handle paths that are only
* query params. eg, Staff Directory or
* Find a Specialist searches. Otherwise the
* focus would change on every key stroke.
*
* Is there a new path?
*/
if (newPath !== oldPath) {
const dataPageHeading = document.querySelector('[data-page-heading]');
const h1 = document.querySelector('h1');
const pageHeading = dataPageHeading ? dataPageHeading : h1;
if (pageHeading) {
pageHeading.setAttribute('tabindex', '-1');
pageHeading.classList.add('focus');
pageHeading.focus();
}
}
/**
* Will scroll to the position of the hash
* if it exists on an element on the page.
* Let content load first before scrolling.
*/
if (location.hash) {
const element = document.querySelector(`${location.hash}`);
if (element) {
window.scrollTo({
top: element.offsetTop,
behavior: 'smooth'
});
}
}
};
export const shouldUpdateScroll = () => {
window.scrollTo(0, 0);
return false;
};