Add Preact, oraz przełącznika zmiany theme

This commit is contained in:
dm
2025-11-21 03:39:33 +01:00
parent 7600b12341
commit 728aefed31
6 changed files with 83 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
import { useEffect, useState } from "preact/hooks";
export default function ThemeToggle() {
const [isDark, setIsDark] = useState(false);
const [mounted, setMounted] = useState(false);
useEffect(() => {
// unikamy SSR, działamy dopiero po stronie klienta
if (typeof window === "undefined") return;
const saved = localStorage.getItem("theme");
const systemPrefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
const dark = saved === "dark" || (!saved && systemPrefersDark);
setIsDark(dark);
document.documentElement.classList.toggle("dark", dark);
setMounted(true);
}, []);
const toggleTheme = () => {
const newDark = !isDark;
setIsDark(newDark);
document.documentElement.classList.toggle("dark", newDark);
localStorage.setItem("theme", newDark ? "dark" : "light");
};
if (!mounted) return null;
return (
<button
type="button"
onClick={toggleTheme}
class="theme-toggle-btn"
aria-label="Zmień motyw"
>
{isDark ? "🌙" : "🌞"}
</button>
);
}

View File

@@ -3,6 +3,8 @@ import "../styles/base.css";
import BaseHead from "./BaseHead.astro";
import Header from "../components/layout/Header.astro";
import Footer from "../components/layout/Footer.astro";
import ThemeToggle from "../islands/ThemeToggle.jsx";
const { seo } = Astro.props;
---
@@ -18,5 +20,9 @@ const { seo } = Astro.props;
</main>
<Footer />
<div class="fixed right-4 bottom-4 z-50">
<ThemeToggle client:load />
</div>
</body>
</html>

View File

@@ -1 +1,2 @@
@import "./tailwind.css";
@import "./theme.css";

28
src/styles/theme.css Normal file
View File

@@ -0,0 +1,28 @@
:root {
--fuz-bg: #ffffff;
--fuz-text: #1a1a1a;
--fuz-accent: #0066ff;
}
:root.dark {
--fuz-bg: #0d1117;
--fuz-text: #e6edf3;
--fuz-accent: #58a6ff;
}
/* Ustawienia bazowe motywu */
body {
background-color: var(--fuz-bg);
color: var(--fuz-text);
}
/* Przycisk przełącznika */
.theme-toggle-btn {
@apply text-xl p-2 rounded-full cursor-pointer transition-colors;
color: var(--fuz-text);
}
.theme-toggle-btn:hover {
opacity: 0.8;
}