lorenzoiovino.com/src/components/Navbar.astro

230 lines
8.1 KiB
Text

---
const currentPath = Astro.url.pathname;
---
<nav
class="fixed top-0 left-0 right-0 z-50 bg-secondary dark:bg-gray-800 shadow-sm transition-colors duration-200"
>
<div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center h-16">
<!-- Logo/Brand -->
<a
href="/"
class="text-xl font-bold text-gray-800 dark:text-gray-100 hover:text-gray-600 dark:hover:text-gray-300 transition-colors duration-200"
>
Lorenzo Iovino
</a>
<!-- Desktop Navigation -->
<div class="hidden md:flex items-center space-x-8">
<!-- Dark Mode Toggle -->
<button
id="theme-toggle"
class="p-2 rounded-lg text-gray-800 dark:text-gray-200 hover:bg-white/20 dark:hover:bg-white/10 transition-colors duration-200"
aria-label="Toggle dark mode"
>
<svg
id="theme-toggle-dark-icon"
class="theme-toggle-dark-icon w-5 h-5 hidden"
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z"></path>
</svg>
<svg
id="theme-toggle-light-icon"
class="theme-toggle-light-icon w-5 h-5 hidden"
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z"
fill-rule="evenodd"
clip-rule="evenodd"></path>
</svg>
</button>
<a
href="/"
class={`text-sm font-medium transition-colors duration-200 ${
currentPath === "/"
? "text-gray-800 dark:text-gray-100 font-semibold"
: "text-gray-800/80 dark:text-gray-300 hover:text-gray-800 dark:hover:text-gray-100"
}`}
>
Home
</a>
<a
href="/bio"
class={`text-sm font-medium transition-colors duration-200 ${
currentPath === "/bio"
? "text-gray-800 dark:text-gray-100 font-semibold"
: "text-gray-800/80 dark:text-gray-300 hover:text-gray-800 dark:hover:text-gray-100"
}`}
>
Bio
</a>
<a
href="/blog"
class={`text-sm font-medium transition-colors duration-200 ${
currentPath === "/blog"
? "text-gray-800 dark:text-gray-100 font-semibold"
: "text-gray-800/80 dark:text-gray-300 hover:text-gray-800 dark:hover:text-gray-100"
}`}
>
Blog
</a>
</div>
<!-- Mobile controls (theme toggle + menu button) -->
<div class="md:hidden flex items-center space-x-2">
<!-- Dark Mode Toggle (Mobile) -->
<button
id="theme-toggle-mobile"
class="p-2 rounded-lg text-gray-800 dark:text-gray-200 hover:bg-white/20 dark:hover:bg-white/10 transition-colors duration-200"
aria-label="Toggle dark mode"
>
<svg
class="theme-toggle-dark-icon w-5 h-5 hidden"
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z"></path>
</svg>
<svg
class="theme-toggle-light-icon w-5 h-5 hidden"
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z"
fill-rule="evenodd"
clip-rule="evenodd"></path>
</svg>
</button>
<!-- Mobile menu button -->
<button
id="mobile-menu-button"
class="p-2 rounded-lg text-gray-800 dark:text-gray-200 hover:bg-white/10 dark:hover:bg-white/5 focus:outline-none focus:ring-2 focus:ring-white/50"
aria-label="Toggle menu"
>
<svg
class="h-6 w-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
</div>
</div>
</div>
<!-- Mobile Navigation -->
<div
id="mobile-menu"
class="hidden md:hidden bg-secondary dark:bg-gray-800 transition-colors duration-200"
style="max-height: 0; overflow: hidden; transition: max-height 0.3s ease-in-out;"
>
<div class="px-4 py-3 space-y-3">
<a
href="/"
class={`block px-3 py-2 rounded-lg text-base font-medium transition-colors duration-200 ${
currentPath === "/"
? "bg-white/20 dark:bg-white/10 text-gray-800 dark:text-gray-100 font-semibold"
: "text-gray-800/80 dark:text-gray-300 hover:bg-white/10 dark:hover:bg-white/5 hover:text-gray-800 dark:hover:text-gray-100"
}`}
>
Home
</a>
<a
href="/bio"
class={`block px-3 py-2 rounded-lg text-base font-medium transition-colors duration-200 ${
currentPath === "/bio"
? "bg-white/20 dark:bg-white/10 text-gray-800 dark:text-gray-100 font-semibold"
: "text-gray-800/80 dark:text-gray-300 hover:bg-white/10 dark:hover:bg-white/5 hover:text-gray-800 dark:hover:text-gray-100"
}`}
>
Bio
</a>
<a
href="/blog"
class={`block px-3 py-2 rounded-lg text-base font-medium transition-colors duration-200 ${
currentPath === "/blog"
? "bg-white/20 dark:bg-white/10 text-gray-800 dark:text-gray-100 font-semibold"
: "text-gray-800/80 dark:text-gray-300 hover:bg-white/10 dark:hover:bg-white/5 hover:text-gray-800 dark:hover:text-gray-100"
}`}
>
Blog
</a>
</div>
</div>
</nav>
<script>
// Mobile menu toggle
const mobileMenuButton = document.getElementById("mobile-menu-button");
const mobileMenu = document.getElementById("mobile-menu");
mobileMenuButton?.addEventListener("click", () => {
if (mobileMenu?.classList.contains("hidden")) {
mobileMenu.classList.remove("hidden");
mobileMenu.style.maxHeight = "500px";
} else if (mobileMenu) {
mobileMenu.style.maxHeight = "0";
setTimeout(() => {
mobileMenu?.classList.add("hidden");
}, 300);
}
});
// Theme toggle functionality
const themeToggleBtns = [
document.getElementById("theme-toggle"),
document.getElementById("theme-toggle-mobile"),
];
const themeToggleDarkIcons = document.querySelectorAll(".theme-toggle-dark-icon");
const themeToggleLightIcons = document.querySelectorAll(".theme-toggle-light-icon");
// Check for saved theme preference or default to 'light' mode
const currentTheme = localStorage.getItem("theme") || "light";
// Set initial theme and icons
if (currentTheme === "dark") {
document.documentElement.classList.add("dark");
themeToggleLightIcons.forEach((icon) => icon.classList.remove("hidden"));
themeToggleDarkIcons.forEach((icon) => icon.classList.add("hidden"));
} else {
document.documentElement.classList.remove("dark");
themeToggleDarkIcons.forEach((icon) => icon.classList.remove("hidden"));
themeToggleLightIcons.forEach((icon) => icon.classList.add("hidden"));
}
// Toggle theme on button clicks
themeToggleBtns.forEach((btn) => {
btn?.addEventListener("click", () => {
document.documentElement.classList.toggle("dark");
// Swap icons
themeToggleDarkIcons.forEach((icon) => icon.classList.toggle("hidden"));
themeToggleLightIcons.forEach((icon) => icon.classList.toggle("hidden"));
// Save theme preference
if (document.documentElement.classList.contains("dark")) {
localStorage.setItem("theme", "dark");
} else {
localStorage.setItem("theme", "light");
}
});
});
</script>