refactor logic for navbar burger to align with other scripts
authorDan Allen <dan@opendevise.com>
Mon, 21 Dec 2020 00:13:08 +0000 (17:13 -0700)
committerDan Allen <dan@opendevise.com>
Mon, 21 Dec 2020 00:13:08 +0000 (17:13 -0700)
src/js/05-mobile-navbar.js

index ba5cb09..17f0452 100644 (file)
@@ -1,12 +1,14 @@
-document.addEventListener('DOMContentLoaded', function () {
-  var navbarToggles = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0)
-  if (navbarToggles.length === 0) return
-  navbarToggles.forEach(function (el) {
-    el.addEventListener('click', function (e) {
-      e.stopPropagation()
-      el.classList.toggle('is-active')
-      document.getElementById(el.dataset.target).classList.toggle('is-active')
-      document.documentElement.classList.toggle('is-clipped--navbar')
-    })
-  })
-})
+;(function () {
+  'use strict'
+
+  var navbarBurger = document.querySelector('.navbar-burger')
+  if (!navbarBurger) return
+  navbarBurger.addEventListener('click', toggleNavbarMenu.bind(navbarBurger))
+
+  function toggleNavbarMenu (e) {
+    e.stopPropagation() // trap event
+    this.classList.toggle('is-active')
+    document.getElementById(this.dataset.target).classList.toggle('is-active')
+    document.documentElement.classList.toggle('is-clipped--navbar')
+  }
+})()