Skip to content

Commit 0f447d6

Browse files
committed
refactor logic for navbar burger to align with other scripts
1 parent 53b8b40 commit 0f447d6

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/js/05-mobile-navbar.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
document.addEventListener('DOMContentLoaded', function () {
2-
var navbarToggles = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0)
3-
if (navbarToggles.length === 0) return
4-
navbarToggles.forEach(function (el) {
5-
el.addEventListener('click', function (e) {
6-
e.stopPropagation()
7-
document.documentElement.classList.toggle('is-clipped--navbar')
8-
el.classList.toggle('is-active')
9-
var menu = document.getElementById(el.dataset.target)
10-
if (menu.classList.toggle('is-active')) {
11-
menu.style.maxHeight = ''
12-
var expectedMaxHeight = window.innerHeight - Math.round(menu.getBoundingClientRect().top)
13-
var actualMaxHeight = parseInt(window.getComputedStyle(menu).maxHeight)
14-
if (actualMaxHeight !== expectedMaxHeight) menu.style.maxHeight = expectedMaxHeight + 'px'
15-
}
16-
})
17-
})
18-
})
1+
;(function () {
2+
'use strict'
3+
4+
var navbarBurger = document.querySelector('.navbar-burger')
5+
if (!navbarBurger) return
6+
navbarBurger.addEventListener('click', toggleNavbarMenu.bind(navbarBurger))
7+
8+
function toggleNavbarMenu (e) {
9+
e.stopPropagation() // trap event
10+
document.documentElement.classList.toggle('is-clipped--navbar')
11+
this.classList.toggle('is-active')
12+
var menu = document.getElementById(this.dataset.target)
13+
if (menu.classList.toggle('is-active')) {
14+
menu.style.maxHeight = ''
15+
var expectedMaxHeight = window.innerHeight - Math.round(menu.getBoundingClientRect().top)
16+
var actualMaxHeight = parseInt(window.getComputedStyle(menu).maxHeight)
17+
if (actualMaxHeight !== expectedMaxHeight) menu.style.maxHeight = expectedMaxHeight + 'px'
18+
}
19+
}
20+
})()

0 commit comments

Comments
 (0)