Sha256: b3e0b822f355028a3d9eed744839be57d5af94b89481961faf949a1ffbe7ec86

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

function setActive(thisLink, linkText) {
  let thisAnchor = thisLink.querySelector('a');
  thisLink.classList.add('active');
  thisAnchor.insertAdjacentHTML('beforeend', ' <span class="sr-only">(current)</span>');
}

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');

  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

8 entries across 8 versions & 1 rubygems

Version Path
kcc-gem-theme-0.7.46 assets/js/theme/script/highlightCurrentNav.js
kcc-gem-theme-0.7.45 assets/js/theme/script/highlightCurrentNav.js
kcc-gem-theme-0.7.44 assets/js/theme/script/highlightCurrentNav.js
kcc-gem-theme-0.7.43 assets/js/theme/script/highlightCurrentNav.js
kcc-gem-theme-0.7.42 assets/js/theme/script/highlightCurrentNav.js
kcc-gem-theme-0.7.41 assets/js/theme/script/highlightCurrentNav.js
kcc-gem-theme-0.7.40 assets/js/theme/script/highlightCurrentNav.js
kcc-gem-theme-0.7.39 assets/js/theme/script/highlightCurrentNav.js