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

@@ -1,6 +1,7 @@
import { defineConfig } from 'astro/config'; import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind'; import tailwind from '@astrojs/tailwind';
import node from '@astrojs/node'; import node from '@astrojs/node';
import preact from "@astrojs/preact";
export default defineConfig({ export default defineConfig({
output: 'server', output: 'server',
@@ -12,6 +13,7 @@ export default defineConfig({
port: parseInt(process.env.PORT || '4321'), port: parseInt(process.env.PORT || '4321'),
}, },
integrations: [ integrations: [
preact(),
tailwind({ tailwind({
applyBaseStyles: true applyBaseStyles: true
}) })

View File

@@ -9,8 +9,11 @@
}, },
"dependencies": { "dependencies": {
"@astrojs/node": "^9.5.1", "@astrojs/node": "^9.5.1",
"@astrojs/preact": "^4.1.3",
"@preact/signals": "^2.5.1",
"astro": "^5.16.0", "astro": "^5.16.0",
"js-yaml": "^4.1.0" "js-yaml": "^4.1.0",
"preact": "^10.27.2"
}, },
"devDependencies": { "devDependencies": {
"@astrojs/tailwind": "^5.0.0", "@astrojs/tailwind": "^5.0.0",

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

View File

@@ -1 +1,2 @@
@import "./tailwind.css"; @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;
}