Skip to content

Commit

Permalink
add highlight to current selected menu element (closes #156)
Browse files Browse the repository at this point in the history
  • Loading branch information
tipsy committed Nov 9, 2023
1 parent ef7e647 commit 3aa613c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<a id="logo" href="/" aria-label="Javalin">
{% if page.splash %}<img src="/img/logo_inv.svg" alt="Javalin">{% else %}<img src="/img/logo.svg" alt="Javalin">{% endif %}
</a>
<ul>
<ul class="top-nav">
<li><a href="/documentation">Docs</a></li>
<li><a href="/tutorials">Tutorials</a></li>
<li><a href="/plugins">Plugins</a></li>
Expand Down
5 changes: 5 additions & 0 deletions _sass/_frame.scss
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ nav {
li {
margin: 0;

&.active {
color: #000;
text-decoration: underline;
}

+ li {
border-left: 1px solid $light-border;
}
Expand Down
10 changes: 10 additions & 0 deletions js/_partials/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ for (let i = 0; i < document.links.length; i++) {
submenuLi.querySelector("ul").style.display = "none";
});
})();

(() => {
let navLinks = document.querySelectorAll(".top-nav li a");
navLinks.forEach(link => {
const href = link.getAttribute("href")
if (window.location.pathname.startsWith(href)) {
link.parentElement.classList.add("active");
}
});
})();
34 changes: 19 additions & 15 deletions js/_partials/scrolling.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
// Function to run after scrolling
}
});
gumshoe.init({ // https://github.com/cferdinandi/gumshoe (scrollspy)
selector: '#spy-nav ul a', // Default link selector
selectorHeader: fixedHeaderSelector, // Fixed header selector
container: window, // The element to spy on scrolling in (must be a valid DOM Node)
offset: 128, // Distance in pixels to offset calculations
activeClass: 'active', // Class to apply to active navigation link and its parent list item
scrollDelay: false, // Wait until scrolling has stopped before updating the navigation
callback: function (nav) {
try {
document.querySelector(".right-menu .active").scrollIntoView({block: "start"});
window.history.replaceState({}, "", location.pathname + "#" + nav.target.id);
document.dispatchEvent(new CustomEvent("menu-scroll-enter", {detail: nav}));
} catch (e) { /* Doesn't matter */
try {
gumshoe.init({ // https://github.com/cferdinandi/gumshoe (scrollspy)
selector: '#spy-nav ul a', // Default link selector
selectorHeader: fixedHeaderSelector, // Fixed header selector
container: window, // The element to spy on scrolling in (must be a valid DOM Node)
offset: 128, // Distance in pixels to offset calculations
activeClass: 'active', // Class to apply to active navigation link and its parent list item
scrollDelay: false, // Wait until scrolling has stopped before updating the navigation
callback: function (nav) {
try {
document.querySelector(".right-menu .active").scrollIntoView({block: "start"});
window.history.replaceState({}, "", location.pathname + "#" + nav.target.id);
document.dispatchEvent(new CustomEvent("menu-scroll-enter", {detail: nav}));
} catch (e) { /* Doesn't matter */
}
}
}
});
});
} catch (e) {
console.error("Failed to initialize gumshoe");
}
})();

0 comments on commit 3aa613c

Please sign in to comment.