Sha256: 7f6f7c2045e09dfe3607e216db10fd88d59298afbbf15b2df9e7a8fe48ce1262

Contents?: true

Size: 1.34 KB

Versions: 53

Compression:

Stored size: 1.34 KB

Contents

// Custom JS to toggle the navigation menu if a dropdown item is open
// and the screen is resized to mobile sized nav. Otherwise, the dropdown
// will stay open while the menu is collapsed causing it to look broken.
const globalNavBottom = document.getElementById('navGlobalBottom');  // built into the site's HTML
const menuCollapseElement = document.getElementById('headerGlobalNavbarContent');

function hideOpenMenu(menuEl, bsCollapse) {
  if ( menuEl.classList.contains('show') ) { // 'show' is a Bootstrap 4 class that makes `.collapse` items visible. // Checking to see if the Menu is open
    bsCollapse.hide(); // BS5 Collapse.hide() method
  }
}

function openMenu(bsCollapse) {
  bsCollapse.show(); // BS5 Collapse.show() method
}

/**
 * 
 * @param {function} Collapse Bootstrap 5 Collapse plugin
 * @see https://getbootstrap.com/docs/5.3/components/collapse/#via-javascript
 */
function toggleMenuOnWindowResize(Collapse) {
  const bsCollapse = new Collapse(menuCollapseElement, { toggle: false });

  window.addEventListener('resize', _e => {
    // 992px and up is bootstrap breakpoint for "lg"
    if ( window.innerWidth >= 992 ) {
      hideOpenMenu(menuCollapseElement, bsCollapse);
    } else {
      if (globalNavBottom.querySelector('.dropdown-toggle.show')) {
        openMenu(bsCollapse);
      }
    }
  });
}

export default toggleMenuOnWindowResize;

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
kcc-gem-theme-3.11.4 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.11.3 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.11.1 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.11.0 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.10.7 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.10.6 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.10.5 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.10.4 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.10.3 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.10.2 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.10.1 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.10.0 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.9.4 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.9.3 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.9.2 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.9.1 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.9.0 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.8.0 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.7.10 assets/js/nav/megaNav/toggleMenuOnWindowResize.js
kcc-gem-theme-3.7.9 assets/js/nav/megaNav/toggleMenuOnWindowResize.js