Skip to content

Commit e292da3

Browse files
authored
Merge pull request #124 from daviddarnes/fix-mobile-nav-resize
[Fix]: Prevent nav being hidden when upscaling browser size
2 parents d708b1a + 56cbd24 commit e292da3

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

_includes/nav-header.html

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,41 @@
1414
</li>
1515
{% endfor %}
1616
</ul>
17+
<button class="button button--nav" aria-label="Menu toggle">
18+
{% include icon.html id="nav" title="Menu" %}
19+
</button>
1720
</nav>
1821
{% else %}
1922
{% include nav-default.html %}
2023
{% endif %}
2124

22-
<template id="buttontoggle">
23-
<button class="button button--nav" aria-label="Menu toggle">
24-
{% include icon.html id="nav" title="Menu" %}
25-
</button>
26-
</template>
27-
2825
<script type="text/javascript">
26+
// Get list and button
27+
const navList = document.querySelector('.header .list--nav')
28+
const button = document.querySelector('.header .button--nav')
29+
30+
// Hide nav and apply toggle
31+
const collapseNav = () => {
32+
if (document.body.clientWidth < 640) {
33+
navList.style.setProperty('--listHeight', `-${navList.offsetHeight}px`)
34+
} else {
35+
navList.removeAttribute('style')
36+
}
2937

30-
const nav = document.querySelector('.nav')
31-
const buttonTemplate = document.querySelector('#buttontoggle')
32-
const button = document.importNode(buttonTemplate.content, true)
33-
nav.appendChild(button)
34-
35-
const applyToggle = (list, button, breakpoint) => {
36-
const navList = document.querySelector(list)
37-
if (document.body.clientWidth < breakpoint) {
38-
const navHeight = navList.clientHeight
39-
const navButton = document.querySelector(button)
40-
navList.style.maxHeight = '0px'
41-
42-
navButton.onclick = () => {
43-
if (navList.style.maxHeight == '0px') {
44-
navList.style.maxHeight = `${navHeight}px`
38+
button.onclick = () => {
39+
navList.style.setProperty('transition', `margin .1s`)
40+
if (navList.style.getPropertyValue('--listHeight')) {
41+
navList.style.removeProperty('--listHeight')
4542
} else {
46-
navList.style.maxHeight = '0px'
43+
navList.style.setProperty('--listHeight', `-${navList.offsetHeight}px`)
4744
}
4845
}
4946
}
50-
}
5147

52-
applyToggle('.list--nav', '.button', 640)
48+
collapseNav()
5349

50+
// Check on resize if to collapse nav
51+
window.addEventListener('resize', () => {
52+
collapseNav()
53+
})
5454
</script>

_sass/_theme.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ body {
3939
display: flex;
4040
flex-direction: column-reverse;
4141
align-items: flex-end;
42+
justify-content: flex-end;
43+
overflow: hidden;
4244
}
4345

4446
.feature {
@@ -175,7 +177,7 @@ body {
175177
list-style: none;
176178
&--nav {
177179
overflow: hidden;
178-
transition: .2s;
180+
margin-bottom: var(--listHeight, 0);
179181
}
180182
.item--post,
181183
.item--result,

0 commit comments

Comments
 (0)