Skip to content

Commit 94cd2cc

Browse files
committed
refactor: Simplify Navigation component with helper functions
- Extract complex conditional logic into getNavLinkClasses helper - Create navItems configuration array for DRY principle - Replace repetitive navigation links with map function - Improve maintainability and readability - Reduce code duplication in desktop and mobile navigation
1 parent da3b573 commit 94cd2cc

1 file changed

Lines changed: 35 additions & 96 deletions

File tree

src/components/Navigation.astro

Lines changed: 35 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22
const currentPath = Astro.url.pathname;
33
const isActive = (path: string) =>
44
currentPath === path || currentPath.startsWith(path + "/");
5+
6+
// Helper function to generate navigation link classes
7+
const getNavLinkClasses = (path: string) => {
8+
const baseClasses = "px-4 py-2 rounded-lg font-medium transition-colors";
9+
const activeClasses = "bg-blue-100 text-blue-700";
10+
const inactiveClasses = "text-gray-700 hover:bg-gray-100";
11+
12+
return `${baseClasses} ${isActive(path) ? activeClasses : inactiveClasses}`;
13+
};
14+
15+
// Navigation items configuration
16+
const navItems = [
17+
{ href: "/", label: "Home" },
18+
{ href: "/about", label: "About" },
19+
{ href: "/projects", label: "Projects" },
20+
{ href: "/makers", label: "Makers" },
21+
{ href: "/makerspaces", label: "MakerSpaces" },
22+
{ href: "/blog", label: "Blog" },
23+
{ href: "/store", label: "Store" },
24+
{ href: "/contact", label: "Contact" },
25+
];
526
---
627

728
<nav class="bg-white/90 backdrop-blur-sm shadow-lg sticky top-0 z-50">
@@ -25,54 +46,13 @@ const isActive = (path: string) =>
2546

2647
<!-- Desktop Navigation -->
2748
<div class="hidden md:flex items-center space-x-1">
28-
<a
29-
href="/"
30-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/") && currentPath === "/" ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
31-
>
32-
Home
33-
</a>
34-
<a
35-
href="/about"
36-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/about") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
37-
>
38-
About
39-
</a>
40-
<a
41-
href="/projects"
42-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/projects") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
43-
>
44-
Projects
45-
</a>
46-
<a
47-
href="/makers"
48-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/makers") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
49-
>
50-
Makers
51-
</a>
52-
<a
53-
href="/makerspaces"
54-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/makerspaces") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
55-
>
56-
MakerSpaces
57-
</a>
58-
<a
59-
href="/blog"
60-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/blog") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
61-
>
62-
Blog
63-
</a>
64-
<a
65-
href="/store"
66-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/store") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
67-
>
68-
Store
69-
</a>
70-
<a
71-
href="/contact"
72-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/contact") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
73-
>
74-
Contact
75-
</a>
49+
{
50+
navItems.map((item) => (
51+
<a href={item.href} class={getNavLinkClasses(item.href)}>
52+
{item.label}
53+
</a>
54+
))
55+
}
7656
<a
7757
href="/donate"
7858
class="ml-2 px-6 py-2 bg-gradient-to-r from-pink-500 to-purple-600 text-white rounded-lg font-semibold hover:shadow-lg transition-all hover:scale-105"
@@ -111,54 +91,13 @@ const isActive = (path: string) =>
11191
<!-- Mobile Navigation -->
11292
<div id="mobile-menu" class="hidden md:hidden pb-4">
11393
<div class="flex flex-col space-y-2">
114-
<a
115-
href="/"
116-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/") && currentPath === "/" ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
117-
>
118-
Home
119-
</a>
120-
<a
121-
href="/about"
122-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/about") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
123-
>
124-
About
125-
</a>
126-
<a
127-
href="/projects"
128-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/projects") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
129-
>
130-
Projects
131-
</a>
132-
<a
133-
href="/makers"
134-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/makers") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
135-
>
136-
Makers
137-
</a>
138-
<a
139-
href="/makerspaces"
140-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/makerspaces") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
141-
>
142-
MakerSpaces 🏭
143-
</a>
144-
<a
145-
href="/blog"
146-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/blog") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
147-
>
148-
Blog
149-
</a>
150-
<a
151-
href="/store"
152-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/store") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
153-
>
154-
Store 🛍️
155-
</a>
156-
<a
157-
href="/contact"
158-
class={`px-4 py-2 rounded-lg font-medium transition-colors ${isActive("/contact") ? "bg-blue-100 text-blue-700" : "text-gray-700 hover:bg-gray-100"}`}
159-
>
160-
Contact
161-
</a>
94+
{
95+
navItems.map((item) => (
96+
<a href={item.href} class={getNavLinkClasses(item.href)}>
97+
{item.label}
98+
</a>
99+
))
100+
}
162101
<a
163102
href="/donate"
164103
class="px-4 py-2 bg-gradient-to-r from-pink-500 to-purple-600 text-white rounded-lg font-semibold text-center"

0 commit comments

Comments
 (0)