Skip to content

Commit

Permalink
feature: nav 색상 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
murramge committed Dec 19, 2024
1 parent 69e9e11 commit 6ce7301
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import router from "../router/Router.js";

const Header = () => {
const LoginState = userStore.LoginState();

const html = `
<header class="bg-blue-600 text-white p-4 sticky top-0">
<h1 class="text-2xl font-bold">항해플러스</h1>
</header>
<nav class="bg-white shadow-md p-2 sticky top-14">
<ul class="flex justify-around">
<li><a href="/">홈</a></li>
<li><a href="/profile">프로필</a></li>
<li><a href="/" class="nav-link">홈</a></li>
<li><a href="/profile" class="nav-link">프로필</a></li>
${
LoginState
? `<li><a href="/login" id="logout">로그아웃</a></li>`
: `<li><a href="/login">로그인</a></li>`
? `<li><a href="/login" id="logout" class="nav-link">로그아웃</a></li>`
: `<li><a href="/login" class="nav-link">로그인</a></li>`
}
</ul>
</nav>
Expand All @@ -25,12 +26,25 @@ const Header = () => {

nav.addEventListener("click", (e) => {
e.preventDefault();

if (e.target.innerHTML === "로그아웃") {
userStore.deleteUser();
}
const path = e.target.href.replace(window.location.origin, ""); //상대 경로만 남기기!
const path = e.target.href.replace(window.location.origin, ""); //상대 경로만 남기기
router(path);

setTimeout(() => setActiveLink(path), 100); // 경로 갱신 후 활성화
});
setActiveLink(window.location.pathname);
};

const setActiveLink = (path) => {
const links = document.querySelectorAll(".nav-link");
links.forEach((link) => {
if (link.getAttribute("href") === path) {
link.classList.add("text-blue-600", "font-bold"); // 파란색과 굵은 글씨 스타일
} else {
link.classList.remove("text-blue-600", "font-bold");
}
});
};

Expand Down

0 comments on commit 6ce7301

Please sign in to comment.