aboutsummaryrefslogtreecommitdiff
path: root/themes/hugo-pt2021/assets/script.js
blob: 2805cef95befbdc513c91227659ebf80a858890e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// enable burger menu support
// src: https://bulma.io/documentation/components/navbar/
//
// Note (2022-03-05): I initially switched from Array.prototype.slice()
// to Array.from() and added "const D = document;" to shrink the
// minified, uncompressed code from 307 bytes to 292 bytes for a ~4.9%
// (15 bytes) size reduction.
//
// Then, I dropped Array.from() entirely, since all modern browsers (see
// URL below) support NodeList.forEach().
//
// Ref: https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach
//
// This shrunk the minified, uncompressed size to 280 bytes, for a
// ~8.8% (27 bytes) cumulative size reduction.
//
// I also tried switching from forEach to for..of, but that increased
// the minified, uncompressed size by a two bytes.
//
document.addEventListener('DOMContentLoaded', () => {
  "use strict";
  const D = document;

  // iterate through burgers
  D.querySelectorAll('.navbar-burger').forEach(e => {
    e.addEventListener('click', () => {
      // get target from data-target attribute
      const t = D.getElementById(e.dataset.target);

      // toggle is-active on both burger and menu
      [e, t].forEach(e => e.classList.toggle('is-active'));
    });
  });
});