Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect the active navigation anchor and style it #2

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ body,
text-decoration: none;
}

.nav .nav__list .nav__item .nav__link.nav__link--active {
color: #FA3;
border-bottom: 2px solid #FA3;
}

.demo {
margin-top: 40px;
}
Expand Down
48 changes: 46 additions & 2 deletions src/js/components/homagePage/NavBar.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
import { createComponent } from '../../mini';

export function NavBar() {
const removeActiveClassFromNonSelected = selected => {
const navLinks = document.querySelectorAll('.nav__link');
navLinks.forEach(link => {
if (link !== selected) {
link.classList.remove('nav__link--active');
}
});
};

// style the selected nav link at the first render
setImmediate(() => {
// if the url doesn't contain a hash, set the first nav link as active
if (!window.location.hash) {
window.location.hash = document.querySelector('.nav__link').getAttribute('href');
}

// style the initial selected nav link
const selected = document.querySelector(`a[href="${window.location.hash}"]`);
selected.classList.add('nav__link--active');
});

let storedHash = window.location.hash;
window.setInterval(() => {
if (window.location.hash !== storedHash) {
storedHash = window.location.hash;
if (storedHash === '#demo') {
const demoLink = document.querySelectorAll('.nav__link')[0];
demoLink.classList.add('nav__link--active');
removeActiveClassFromNonSelected(demoLink);
}
if (storedHash === '#select-game-language') {
const playLink = document.querySelectorAll('.nav__link')[1];
playLink.classList.add('nav__link--active');
removeActiveClassFromNonSelected(playLink);
}
if (storedHash === '#about') {
const aboutLink = document.querySelectorAll('.nav__link')[2];
aboutLink.classList.add('nav__link--active');
removeActiveClassFromNonSelected(aboutLink);
}
}
window.clearInterval();
}, 100);

return createComponent({
render() {
return `
<nav class="nav">
<ul class="nav__list">
<li class="nav__item">
<a href="#select-game-language" class="nav__link">Play</a>
<a href="#demo" class="nav__link">Demo</a>
</li>
<li class="nav__item">
<a href="#demo" class="nav__link">Demo</a>
<a href="#select-game-language" class="nav__link">Play</a>
</li>
<li class="nav__item">
<a href="#about" class="nav__link">About</a>
Expand Down