-
Notifications
You must be signed in to change notification settings - Fork 248
/
gatsby-browser.js
44 lines (40 loc) · 1.43 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
require("prismjs/themes/prism-okaidia.css");
require("prismjs/plugins/command-line/prism-command-line.css");
exports.onRouteUpdate = ({ location }) => scrollToAnchor(location);
/**
*
* @desc - a function to jump to the correct scroll position
* @param {Object} location -
* @param {Number} [mainNavHeight] - the height of any persistent nav -> document.querySelector(`nav`)
*/
function scrollToAnchor(location, mainNavHeight = 0) {
// left nav: scroll
const navItem = document.querySelector(".sidebar .active");
if (navItem) navItem.scrollIntoView({ block: "nearest" });
// right-nav: scroll
const toc = document.querySelector(".toc-sticky .active");
if (toc) toc.scrollIntoView({ block: "nearest" });
return true;
}
exports.onInitialClientRender = () => {
// h/t https://stackoverflow.com/questions/19646684/force-open-the-details-summary-tag-for-print-in-chrome/75260733#75260733
window.matchMedia("print").addEventListener("change", (evt) => {
if (evt.matches) {
let detailsElements = document.body.querySelectorAll(
"details:not([open])",
);
for (let e of detailsElements) {
e.toggleAttribute("open", true);
e.dataset.wasclosed = "";
}
} else {
let detailsElements = document.body.querySelectorAll(
"details[data-wasclosed]",
);
for (let e of detailsElements) {
e.removeAttribute("open");
delete e.dataset.wasclosed;
}
}
});
};