Skip to content

Commit

Permalink
Merge pull request #124 from daviddarnes/fix-mobile-nav-resize
Browse files Browse the repository at this point in the history
[Fix]: Prevent nav being hidden when upscaling browser size
  • Loading branch information
daviddarnes authored Apr 6, 2019
2 parents d708b1a + 56cbd24 commit e292da3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
48 changes: 24 additions & 24 deletions _includes/nav-header.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@
</li>
{% endfor %}
</ul>
<button class="button button--nav" aria-label="Menu toggle">
{% include icon.html id="nav" title="Menu" %}
</button>
</nav>
{% else %}
{% include nav-default.html %}
{% endif %}

<template id="buttontoggle">
<button class="button button--nav" aria-label="Menu toggle">
{% include icon.html id="nav" title="Menu" %}
</button>
</template>

<script type="text/javascript">
// Get list and button
const navList = document.querySelector('.header .list--nav')
const button = document.querySelector('.header .button--nav')

// Hide nav and apply toggle
const collapseNav = () => {
if (document.body.clientWidth < 640) {
navList.style.setProperty('--listHeight', `-${navList.offsetHeight}px`)
} else {
navList.removeAttribute('style')
}

const nav = document.querySelector('.nav')
const buttonTemplate = document.querySelector('#buttontoggle')
const button = document.importNode(buttonTemplate.content, true)
nav.appendChild(button)

const applyToggle = (list, button, breakpoint) => {
const navList = document.querySelector(list)
if (document.body.clientWidth < breakpoint) {
const navHeight = navList.clientHeight
const navButton = document.querySelector(button)
navList.style.maxHeight = '0px'

navButton.onclick = () => {
if (navList.style.maxHeight == '0px') {
navList.style.maxHeight = `${navHeight}px`
button.onclick = () => {
navList.style.setProperty('transition', `margin .1s`)
if (navList.style.getPropertyValue('--listHeight')) {
navList.style.removeProperty('--listHeight')
} else {
navList.style.maxHeight = '0px'
navList.style.setProperty('--listHeight', `-${navList.offsetHeight}px`)
}
}
}
}

applyToggle('.list--nav', '.button', 640)
collapseNav()

// Check on resize if to collapse nav
window.addEventListener('resize', () => {
collapseNav()
})
</script>
4 changes: 3 additions & 1 deletion _sass/_theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ body {
display: flex;
flex-direction: column-reverse;
align-items: flex-end;
justify-content: flex-end;
overflow: hidden;
}

.feature {
Expand Down Expand Up @@ -175,7 +177,7 @@ body {
list-style: none;
&--nav {
overflow: hidden;
transition: .2s;
margin-bottom: var(--listHeight, 0);
}
.item--post,
.item--result,
Expand Down

0 comments on commit e292da3

Please sign in to comment.