Skip to content

Commit 6422af2

Browse files
committed
try dark theme switcher
1 parent 152f784 commit 6422af2

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/components/NavBar.astro

+33-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,44 @@
33
---
44

55
<header
6-
class="bg-neutral-300 border-gray-200 border-b-2 backdrop-filter backdrop-blur-lg bg-opacity-30 fixed w-screen top-0 z-50"
6+
class="bg-neutral-300 dark:bg-zinc-900 border-gray-200 border-b-2 backdrop-filter backdrop-blur-lg bg-opacity-30 fixed w-screen top-0 z-50"
77
>
88
<nav class="flex justify-between p-4 font-semibold">
99
<a href="/">ayats.org</a>
1010
<ul>
1111
<a href="/blog/">Blog</a>
12+
<a dark-theme-button>Theme</a>
1213
</ul>
1314
</nav>
1415
</header>
16+
17+
<script>
18+
function loadTheme() {
19+
if (
20+
localStorage.theme === "dark" ||
21+
(!("theme" in localStorage) &&
22+
window.matchMedia("(prefers-color-scheme: dark)").matches)
23+
) {
24+
document.documentElement.classList.add("dark");
25+
} else {
26+
document.documentElement.classList.remove("dark");
27+
}
28+
}
29+
loadTheme();
30+
31+
const btn = document.querySelectorAll("[dark-theme-button]");
32+
console.log(btn);
33+
34+
btn.forEach((btn) => {
35+
btn.addEventListener("click", () => {
36+
if (localStorage.theme === "dark") {
37+
localStorage.theme = "light";
38+
} else {
39+
localStorage.theme = "dark";
40+
}
41+
loadTheme();
42+
});
43+
});
44+
45+
console.log("HELLO");
46+
</script>

src/layouts/Base.astro

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Footer from '../components/Footer.astro';
1111
<meta name="generator" content={Astro.generator} />
1212
</head>
1313

14-
<body class="m-auto text-lg">
14+
<body class="m-auto text-base md:text-lg">
1515

1616
<NavBar class="h-screen"/>
1717

tailwind.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Config } from 'tailwindcss'
22

33
export default {
4+
darkMode: 'selector',
45
content: ['./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}'],
56
theme: {
67
extend: {

0 commit comments

Comments
 (0)