Skip to content

Commit

Permalink
fix: header active state
Browse files Browse the repository at this point in the history
  • Loading branch information
rxyhn committed Sep 8, 2024
1 parent f3df8ed commit a7dd83a
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/scripts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,24 @@ window.evaluateHeaderPosition = () => {

window.applyMenuItemClasses = () => {
const menuItems = document.querySelectorAll<HTMLAnchorElement>("nav a");
const currentPath = window.location.pathname;
let currentPath = window.location.pathname;

if (currentPath === "/") {
currentPath = "/";
} else {
currentPath = currentPath.replace(/\/$/, "");
}

const activeClass = ["text-white", "font-bold"];

menuItems.forEach((menuItem) => {
if (new URL(menuItem.href).pathname === currentPath) {
let menuItemPath = new URL(menuItem.href).pathname;

if (menuItemPath !== "/") {
menuItemPath = menuItemPath.replace(/\/$/, "");
}

if (menuItemPath === currentPath) {
menuItem.classList.add(...activeClass);
} else {
menuItem.classList.remove(...activeClass);
Expand Down

0 comments on commit a7dd83a

Please sign in to comment.