Skip to content

Commit 5a1d39c

Browse files
committed
refactor: enhance Header component structure and active state handling
vbnet
1 parent eefe7f8 commit 5a1d39c

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

components/Header.tsx

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
1-
import Link from 'next/link';
2-
import ThemeToggle from './ThemeToggle';
1+
import Link from "next/link";
2+
import { usePathname } from "next/navigation";
33

44
type HeaderProps = {
5-
isDark: boolean;
5+
isDark: boolean;
66
};
77

8-
export default function Header({ isDark }: { isDark: boolean }) {
9-
return (
10-
<header className="flex justify-end items-center px-6 py-4">
11-
<div className="flex gap-6 items-center">
12-
<Link href="/" className={`${isDark ? "text-white" : "text-black"} hover:text-blue-300`}>
13-
<span className="font-bold">Main</span>
14-
</Link>
15-
<Link href="/post" className={`${isDark ? "text-white" : "text-black"} hover:text-blue-300 font-bold`}>
16-
Post
17-
</Link>
18-
<Link href="/book" className={`${isDark ? "text-white" : "text-black"} hover:text-blue-300 font-bold`}>
19-
Book-Review
20-
</Link>
21-
<Link href="/about" className={`${isDark ? "text-white" : "text-black"} hover:text-blue-300 font-bold`}>
22-
About
23-
</Link>
24-
{/*<ThemeToggle />*/}
25-
</div>
26-
</header>
27-
);
8+
export default function Header({ isDark }: HeaderProps) {
9+
const pathname = usePathname();
10+
11+
const items = [
12+
{ href: "/", label: "Main" },
13+
{ href: "/post", label: "Posts" },
14+
{ href: "/book", label: "Book-Reviews" },
15+
{ href: "/link", label: "Useful-Links" },
16+
{ href: "/about", label: "About" },
17+
];
18+
19+
const isActive = (href: string) =>
20+
href === "/" ? pathname === "/" : pathname?.startsWith(href);
21+
22+
const base = `${isDark ? "text-white" : "text-black"}`;
23+
const hover = "hover:text-blue-300";
24+
const active = "font-extrabold underline underline-offset-4";
25+
const idle = "font-bold";
26+
27+
return (
28+
<header className="flex justify-end items-center px-6 py-4">
29+
<nav className="flex gap-6 items-center" aria-label="Main Navigation">
30+
{items.map((it) => {
31+
const activeCls = isActive(it.href) ? active : idle;
32+
return (
33+
<Link
34+
key={it.href}
35+
href={it.href}
36+
className={`${base} ${hover} ${activeCls} focus:outline-none focus:ring-2 focus:ring-blue-300 rounded-md px-1`}
37+
aria-current={isActive(it.href) ? "page" : undefined}
38+
>
39+
{it.label}
40+
</Link>
41+
);
42+
})}
43+
{/* <ThemeToggle /> */}
44+
</nav>
45+
</header>
46+
);
2847
}

0 commit comments

Comments
 (0)