Skip to content

Commit 9f8d446

Browse files
authored
resolves #82 shrink nav menu to fit available space (PR #83)
1 parent 6ad354e commit 9f8d446

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/css/footer.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ footer.footer {
66
padding: 2rem 1rem;
77
display: flex;
88
flex-direction: column;
9+
position: relative;
10+
z-index: var(--z-index-footer);
911
}
1012

1113
.footer p {

src/css/nav.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
.nav {
6060
top: var(--navbar-height);
6161
box-shadow: none;
62-
position: sticky;
62+
position: fixed;
6363
height: var(--nav-height--desktop);
64+
width: inherit;
6465
}
6566
}
6667

src/css/vars.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,6 @@
163163
--z-index-nav: 2;
164164
--z-index-toolbar: 3;
165165
--z-index-page-version-menu: 4;
166-
--z-index-navbar: 5;
166+
--z-index-footer: 5;
167+
--z-index-navbar: 6;
167168
}

src/js/01-nav.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var menuPanel = navContainer.querySelector('[data-panel=menu]')
1313
if (!menuPanel) return
1414
var nav = navContainer.querySelector('.nav')
15+
var navBounds = { encroachingElement: document.querySelector('footer.footer') }
1516

1617
var currentPageItem
1718
if (menuPanel.classList.contains('is-loading')) {
@@ -31,6 +32,10 @@
3132
}
3233
}
3334

35+
fitNavInit({})
36+
window.addEventListener('load', fitNavInit)
37+
window.addEventListener('resize', fitNavInit)
38+
3439
menuPanel.querySelector('.nav-menu-toggle').addEventListener('click', function () {
3540
var collapse = !this.classList.toggle('is-active')
3641
find(menuPanel, '.nav-item > .nav-item-toggle').forEach(function (btn) {
@@ -155,4 +160,27 @@
155160
function find (from, selector) {
156161
return [].slice.call(from.querySelectorAll(selector))
157162
}
163+
164+
function fitNavInit (e) {
165+
if (e.type) {
166+
window.removeEventListener('scroll', fitNav)
167+
nav.style.height = ''
168+
}
169+
if (window.getComputedStyle(nav).position === 'fixed') {
170+
navBounds.availableHeight = window.innerHeight
171+
navBounds.preferredHeight = nav.getBoundingClientRect().height
172+
fitNav()
173+
window.addEventListener('scroll', fitNav)
174+
}
175+
if (e.type !== 'resize' && currentPageItem) scrollItemToMidpoint(menuPanel, currentPageItem)
176+
}
177+
178+
function fitNav () {
179+
var scrollDatum = menuPanel.scrollTop + menuPanel.offsetHeight
180+
var reclaimedHeight = navBounds.availableHeight - navBounds.encroachingElement.getBoundingClientRect().top
181+
nav.style.height = reclaimedHeight > 0
182+
? Math.max(0, Math.round(navBounds.preferredHeight - reclaimedHeight)) + 'px'
183+
: ''
184+
menuPanel.scrollTop = scrollDatum - menuPanel.offsetHeight
185+
}
158186
})()

0 commit comments

Comments
 (0)