Skip to content

Commit d643aea

Browse files
authored
resolves #63 expand nav eagerly to prevent it from flickering between pages (PR #64)
1 parent f473d6d commit d643aea

File tree

3 files changed

+64
-15
lines changed

3 files changed

+64
-15
lines changed

src/css/nav.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
height: var(--nav-panel-menu-height);
7676
}
7777

78+
.nav-panel-menu.is-loading {
79+
visibility: hidden;
80+
}
81+
7882
.nav-panel-menu:not(.is-active) .nav-menu {
7983
opacity: 0.75;
8084
}

src/js/01-nav.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@
1313
if (!menuPanel) return
1414
var nav = navContainer.querySelector('.nav')
1515

16-
var currentPageItem = findItemForHash() || menuPanel.querySelector('.is-current-url')
17-
if (currentPageItem) {
18-
activateCurrentPath(currentPageItem)
19-
scrollItemToMidpoint(menuPanel, currentPageItem)
16+
var currentPageItem
17+
if (menuPanel.classList.contains('is-loading')) {
18+
if ((currentPageItem = findItemForHash() || menuPanel.querySelector('.is-current-url'))) {
19+
activateCurrentPath(currentPageItem)
20+
scrollItemToMidpoint(menuPanel, currentPageItem)
21+
} else {
22+
menuPanel.scrollTop = 0
23+
}
24+
menuPanel.classList.remove('is-loading')
2025
} else {
21-
menuPanel.scrollTop = 0
26+
var match = (currentPageItem = menuPanel.querySelector('.is-current-page'))
27+
if ((!match || match.classList.contains('is-provisional')) && (match = findItemForHash(true))) {
28+
var update = !!currentPageItem
29+
activateCurrentPath((currentPageItem = match), update)
30+
scrollItemToMidpoint(menuPanel, currentPageItem)
31+
}
2232
}
2333

2434
menuPanel.querySelector('.nav-menu-toggle').addEventListener('click', function () {
@@ -29,7 +39,7 @@
2939
if (currentPageItem) {
3040
if (collapse) activateCurrentPath(currentPageItem)
3141
scrollItemToMidpoint(menuPanel, currentPageItem)
32-
} else if (collapse) {
42+
} else {
3343
menuPanel.scrollTop = 0
3444
}
3545
})
@@ -45,7 +55,8 @@
4555

4656
nav.querySelector('[data-panel=explore] .context').addEventListener('click', function () {
4757
find(nav, '[data-panel]').forEach(function (panel) {
48-
panel.classList.toggle('is-active') // NOTE logic assumes there are only two panels
58+
// NOTE logic assumes there are only two panels
59+
panel.classList.toggle('is-active')
4960
})
5061
})
5162

@@ -57,16 +68,18 @@
5768
function onHashChange () {
5869
var navItem = findItemForHash() || menuPanel.querySelector('.is-current-url')
5970
if (!navItem || navItem === currentPageItem) return
60-
find(menuPanel, '.nav-item.is-active').forEach(function (el) {
61-
el.classList.remove('is-current-path', 'is-current-page', 'is-active')
62-
})
63-
activateCurrentPath((currentPageItem = navItem))
71+
activateCurrentPath((currentPageItem = navItem), true)
6472
scrollItemToMidpoint(menuPanel, currentPageItem)
6573
}
6674

6775
if (menuPanel.querySelector('.nav-link[href^="#"]')) window.addEventListener('hashchange', onHashChange)
6876

69-
function activateCurrentPath (navItem) {
77+
function activateCurrentPath (navItem, update) {
78+
if (update) {
79+
find(menuPanel, '.nav-item.is-active').forEach(function (el) {
80+
el.classList.remove('is-current-path', 'is-current-page', 'is-active')
81+
})
82+
}
7083
var ancestor = navItem
7184
while ((ancestor = ancestor.parentNode) && ancestor !== menuPanel) {
7285
if (ancestor.classList.contains('nav-item')) ancestor.classList.add('is-current-path', 'is-active')
@@ -114,12 +127,12 @@
114127
e.stopPropagation()
115128
}
116129

117-
function findItemForHash () {
130+
function findItemForHash (articleOnly) {
118131
var hash = window.location.hash
119132
if (!hash) return
120133
if (hash.indexOf('%')) hash = decodeURIComponent(hash)
121134
if (hash.indexOf('"')) hash = hash.replace(/(?=")/g, '\\')
122-
var navLink = menuPanel.querySelector('.nav-link[href="' + hash + '"]')
135+
var navLink = !articleOnly && menuPanel.querySelector('.nav-link[href="' + hash + '"]')
123136
if (navLink) return navLink.parentNode
124137
var target = document.getElementById(hash.slice(1))
125138
if (!target) return

src/partials/nav-menu.hbs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
11
{{#with page.navigation}}
2-
<div class="nav-panel-menu is-active" data-panel="menu">
2+
<div class="nav-panel-menu is-active is-loading" data-panel="menu">
33
<nav class="nav-menu">
44
<button class="nav-menu-toggle" title="Toggle expand/collapse all"></button>
55
{{#with @root.page.componentVersion}}
66
<h3 class="title"><a href="{{{relativize ./url}}}">{{./title}}</a></h3>
77
{{/with}}
88
{{> nav-tree navigation=this}}
99
</nav>
10+
<script>
11+
{{! the logic to trace current path and scroll to item for current page copied from js/01-nav.js}}
12+
;(function () {
13+
var panel = document.querySelector('.nav-panel-menu')
14+
var page
15+
var hash = window.location.hash
16+
if (hash) {
17+
if (~hash.indexOf('%')) hash = decodeURIComponent(hash)
18+
if (~hash.indexOf('"')) hash = hash.replace(/(?=")/g, '\\')
19+
var link = panel.querySelector('.nav-link[href="' + hash + '"]')
20+
if (link) page = link.parentNode
21+
else if ((page = panel.querySelector('.is-current-url'))) page.classList.add('is-provisional')
22+
} else {
23+
page = panel.querySelector('.is-current-url')
24+
}
25+
if (page) {
26+
var ancestor = page
27+
while ((ancestor = ancestor.parentNode) && ancestor !== panel) {
28+
if (ancestor.className === 'nav-item') ancestor.classList.add('is-current-path', 'is-active')
29+
}
30+
page.classList.add('is-current-page', 'is-active')
31+
if (panel.scrollHeight > panel.clientHeight) {
32+
var panelRect = panel.getBoundingClientRect()
33+
var linkRect = page.querySelector('.nav-link').getBoundingClientRect()
34+
panel.scrollTop += Math.round(linkRect.top - panelRect.top - (panelRect.height - linkRect.height) * 0.5)
35+
}
36+
} else {
37+
panel.scrollTop = 0
38+
}
39+
panel.classList.remove('is-loading')
40+
})()
41+
</script>
1042
</div>
1143
{{/with}}

0 commit comments

Comments
 (0)