Sha256: d0def05338ca7910a9b8bdfdb951dd1f4bd45ffa0666ca73cc2958146771e926

Contents?: true

Size: 1.27 KB

Versions: 15

Compression:

Stored size: 1.27 KB

Contents

// Custom Vanilla JS to highlight the user's current location in the navigation bar and the sub-nav navigation bar
function setActive(link) {
  const li = link.parentNode;

  li.classList.add('active');
  link.insertAdjacentHTML('beforeend', ' <span class="visually-hidden">(current)</span>');
}

function checkNavLinks(navList) {
  const pathname = window.location.pathname;
  const locationIsContactHash = window.location.hash === '#contact';
  const locationIsHome = window.location.pathname === '/';

  [...navList].forEach(item => {
    const link = item.querySelector('a');
    const href = link.getAttribute('href').replace(/^\/?\.\.\/(\.\.\/)?(\.\.\/)?/g, '/');
    const linkIsHome = link.textContent.toLowerCase() === 'home';
    const linkIsMatch = (pathname.indexOf(href) !== -1);

    if (locationIsHome || locationIsContactHash) {
      if (linkIsHome) {
        setActive(link);
      }
    } else {
      if (linkIsMatch && !linkIsHome) {
        setActive(link);
      }
    }
  });
}

function highlightNav() {
  const navList = document.querySelectorAll('.js-nav-item');

  if (document.getElementById('subNavNav')) {
    const subNavList = document.querySelectorAll('.js-sub-nav-item');

    checkNavLinks(subNavList);
  }

  checkNavLinks(navList);
}

export default highlightNav;

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
kcc-gem-theme-original-1.2.4 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.2.3 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.2.2 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.2.1 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.2.0 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.1.0 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.0.8 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.0.7 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.0.6 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.0.5 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.0.4 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.0.3 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.0.2 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.0.1 assets/js/nav/highlightCurrentNav.js
kcc-gem-theme-original-1.0.0 assets/js/nav/highlightCurrentNav.js