feat: implement dark mode with theme toggle

- Add dark mode support across all components and pages
- Implement theme toggle button in navbar (desktop and mobile)
- Fix desktop theme toggle icon visibility issue
- Add dark mode color variants to all text, backgrounds and components
- Update Tailwind config to enable class-based dark mode
- Add dark mode support to prose styles for blog content
- Persist theme preference in localStorage
- Update all pages (index, bio, blog) with dark mode colors
- Optimize images (me.png and me-baby.jpg)
This commit is contained in:
Lorenzo Iovino 2026-01-09 16:20:24 +01:00
parent a6cd4bcee5
commit 5f95673d2f
16 changed files with 392 additions and 203 deletions

View file

@ -2,23 +2,55 @@
const currentPath = Astro.url.pathname;
---
<nav class="fixed top-0 left-0 right-0 z-50 bg-secondary shadow-sm">
<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 hover:text-gray-200 transition-colors duration-200"
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 font-semibold" : "text-gray-800/80 hover:text-gray-800"
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
@ -26,7 +58,9 @@ const currentPath = Astro.url.pathname;
<a
href="/bio"
class={`text-sm font-medium transition-colors duration-200 ${
currentPath === "/bio" ? "text-gray-800 font-semibold" : "text-gray-800/80 hover:text-gray-800"
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
@ -34,45 +68,80 @@ const currentPath = Astro.url.pathname;
<a
href="/blog"
class={`text-sm font-medium transition-colors duration-200 ${
currentPath === "/blog" ? "text-gray-800 font-semibold" : "text-gray-800/80 hover:text-gray-800"
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 menu button -->
<button
id="mobile-menu-button"
class="md:hidden p-2 rounded-lg text-gray-800 hover:bg-white/10 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"
<!-- 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"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
<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">
<div
id="mobile-menu"
class="hidden md:hidden bg-secondary dark:bg-gray-800 transition-colors duration-200"
>
<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 text-gray-800 font-semibold"
: "text-gray-800/80 hover:bg-white/10 hover:text-gray-800"
? "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
@ -81,8 +150,8 @@ const currentPath = Astro.url.pathname;
href="/bio"
class={`block px-3 py-2 rounded-lg text-base font-medium transition-colors duration-200 ${
currentPath === "/bio"
? "bg-white/20 text-gray-800 font-semibold"
: "text-gray-800/80 hover:bg-white/10 hover:text-gray-800"
? "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
@ -91,8 +160,8 @@ const currentPath = Astro.url.pathname;
href="/blog"
class={`block px-3 py-2 rounded-lg text-base font-medium transition-colors duration-200 ${
currentPath === "/blog"
? "bg-white/20 text-gray-800 font-semibold"
: "text-gray-800/80 hover:bg-white/10 hover:text-gray-800"
? "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
@ -102,10 +171,51 @@ const currentPath = Astro.url.pathname;
</nav>
<script>
// Mobile menu toggle
const mobileMenuButton = document.getElementById("mobile-menu-button");
const mobileMenu = document.getElementById("mobile-menu");
mobileMenuButton?.addEventListener("click", () => {
mobileMenu?.classList.toggle("hidden");
});
// 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>