Repository navigation
Expand file tree
/
Copy pathdocs.js
More file actions
71 lines (64 loc) · 2.68 KB
/
Copy pathdocs.js
File metadata and controls
71 lines (64 loc) · 2.68 KB
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
// Docs page behaviour: mobile sidebar accordion + legacy anchor rescue
// + Pagefind search.
// The GitHub wiki generated anchors with a leading hyphen (#-swap-ops)
// because those headings began with an inline icon image. Links of that
// shape are still out in the wild — in older toolkit builds and in other
// people's posts — so accept them and land on the real heading.
(function () {
function rescue() {
var hash = decodeURIComponent(location.hash || '');
if (!hash || hash.length < 3 || hash.charAt(1) !== '-') return;
if (document.getElementById(hash.slice(1))) return;
var el = document.getElementById(hash.slice(2));
if (el) el.scrollIntoView();
}
rescue();
window.addEventListener('hashchange', rescue);
})();
// Pagefind ships with the generated bundle; if the search index has not
// been built yet the import fails and the page is simply search-less.
(function () {
var mount = document.getElementById('search');
if (!mount || !window.PagefindUI) return;
new window.PagefindUI({
element: '#search',
baseUrl: '/',
showSubResults: true,
showImages: false,
resetStyles: false,
translations: { placeholder: 'Search the docs' },
});
})();
// The sidebar groups ship as <details open>: with no JavaScript a reader
// gets the full list, which is what desktop wants and is no worse than
// what mobile had. Here we close them below the layout breakpoint, where
// 53 expanded packages pushed the first line of every page off the third
// screen -- tapping a tool landed you back on the menu.
//
// ALL of them close, the current one included: the crumb and the <h1> two
// rows below already say where you are, and leaving that group open put
// its category (5 to 11 packages) back between the reader and the article.
// Re-opened on the way back to desktop, because a collapsed group there
// has no affordance to open it (the summary is styled as a plain heading
// and the chevron is mobile-only).
(function () {
var side = document.getElementById('docs-side');
if (!side) return;
var groups = [].slice.call(side.querySelectorAll('.side-group'));
if (!groups.length) return;
var mq = window.matchMedia('(max-width: 900px)');
var touched = false; // once a reader opens or closes one, stop steering
function apply(narrow) {
if (touched) return;
groups.forEach(function (g) {
g.open = !narrow;
});
}
side.addEventListener('toggle', function (e) {
if (e.target.classList.contains('side-group')) touched = true;
}, true);
apply(mq.matches);
var onChange = function (e) { apply(e.matches); };
if (mq.addEventListener) mq.addEventListener('change', onChange);
else if (mq.addListener) mq.addListener(onChange);
})();