Skip to content

Commit

Permalink
add active classname based on the id and current pathname
Browse files Browse the repository at this point in the history
Signed-off-by: Huong Nguyen <huongg1409@gmail>
  • Loading branch information
Huong Nguyen committed Jun 26, 2024
1 parent a68f927 commit 1b113d2
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/kedro_sphinx_theme/theme/kedro-sphinx-theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
</div>

<div class="wy-main-nav">
<a class="wy-main-nav-link" href="/" class="active">Kedro</a>
<a class="wy-main-nav-link" href="https://docs.kedro.org/projects/kedro-viz/en/stable/">Kedro-Viz</a>
<a class="wy-main-nav-link" href="https://docs.kedro.org/projects/kedro-datasets">Kedro-Datasets</a>
<a id='kedro' class="wy-main-nav-link" href="">Kedro</a>
<a id='kedro-viz' class="wy-main-nav-link" href="/">Kedro-Viz</a>
<a id='kedro-datasets' class="wy-main-nav-link" href="">Kedro-Datasets</a>
</div>

{%- include "searchbox.html" %}
Expand Down Expand Up @@ -64,10 +64,24 @@
document.querySelector('.wy-nav-top').prepend(closeBtn);

// Prepend open button to mobile header
var openBtn = document.createElement("span");
openBtn.dataset.toggle = 'wy-nav-top';
openBtn.classList.add('wy-nav-top-open');
document.querySelector('.wy-nav-top').prepend(openBtn);
});
var openBtn = document.createElement("span");
openBtn.dataset.toggle = 'wy-nav-top';
openBtn.classList.add('wy-nav-top-open');
document.querySelector('.wy-nav-top').prepend(openBtn);

// Select all navigation links within the .wy-main-nav container
var navLinks = document.querySelectorAll('.wy-main-nav .wy-main-nav-link');
var currentPath = window.location.pathname;

// Iterate over each link to check if the current path includes the link's unique ID
navLinks.forEach(function(link) {
var kedroSpecificPath = 'https://docs.kedro.org/en';
var isKedroLink = link.id === 'kedro';

if ((isKedroLink && currentPath.includes(kedroSpecificPath)) || (!isKedroLink && currentPath.includes(link.id))) {
link.classList.add('active');
}
});
});
</script>
{% endblock %}

0 comments on commit 1b113d2

Please sign in to comment.