Sha256: e1dac1a5777f40d71520ddb5a9dca3fe17bca8c986afc5ea3cde2c31ebf169da

Contents?: true

Size: 1.7 KB

Versions: 66

Compression:

Stored size: 1.7 KB

Contents

// Custom Vanilla JS to highlight the user's current location in the navigation bar and the sub-nav navigiation bar
function setActive(thisLink, linkText) {
  let thisAnchor = thisLink.querySelector('a');
  thisLink.classList.add('active');
  thisAnchor.insertAdjacentHTML('beforeend', ' <span class="sr-only">(current)</span>');
}

function highlightSubNav(url) {
  const subNavItems = document.querySelectorAll('.js-sub-nav-item');
  for ( let link of subNavItems) {
    const anchor = link.querySelector('a');
    const href = anchor.getAttribute('href').replace(/\.\.\//g, '');
    const linkText = anchor.textContent;
    const urlMatchesLink = url.indexOf(href) > -1;
    urlMatchesLink ?
      setActive(link, linkText)
    : null;
  }
}

function highlightNav() {
  const host = window.location.host + '/';
  const url = window.location.href.replace(/(^\w+:|^)\/\//, '');
  const urlIsIndex = url === host || url === host + '#contact' ;
  const navigationItems = document.querySelectorAll('.js-nav-item');
  document.getElementById('subNavNav') ?
    highlightSubNav(url)
  : null;

  for ( let link of navigationItems ) {
    const linkAnchor = link.querySelector('a');
    const linkHref = linkAnchor.getAttribute('href').replace(/\.\.\//g, '');
    const linkText = linkAnchor.textContent;
    const linkIsHome = linkText.toLowerCase() === 'home';
    const urlMatchesLink = url.indexOf(linkHref) > -1;

    if ( urlIsIndex ) {
      linkIsHome ? setActive(link, linkText) : null;
    } else {
      urlMatchesLink && !linkIsHome ? setActive(link, linkText) : null;
    }
  }
}
//
//  USEAGE:
//
//    document.addEventListener('DOMContentLoaded', function() {
//      highlightNav();
//    });
//
export default highlightNav;

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
kcc-gem-theme-1.30.10 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.30.9 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.29.9 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.28.9 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.28.8 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.28.7 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.27.7 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.27.6 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.26.6 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.25.6 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.25.5 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.24.5 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.23.5 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.22.5 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.21.5 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.20.5 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.20.4 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.20.3 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.20.2 assets/js/theme/src/highlightCurrentNav.js
kcc-gem-theme-1.19.2 assets/js/theme/src/highlightCurrentNav.js