-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPage.astro
More file actions
43 lines (37 loc) · 1.19 KB
/
Page.astro
File metadata and controls
43 lines (37 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
---
import "./Page.scss";
import ThemeIcon from "../ThemeIcon/ThemeIcon.astro";
---
<div class="Page">
<ThemeIcon />
<slot />
</div>
<script type="module">
document.addEventListener("DOMContentLoaded", () => {
const toggleButton = document.getElementById("theme-toggle");
// Check if dark mode is already set in localStorage
const isDarkMode = localStorage.getItem("theme") === "dark";
if (isDarkMode) {
document.body.classList.add("dark-mode");
} else {
document.body.classList.add("light-mode");
}
// Update icon based on the theme
const updateIcon = () => {
const isDarkMode = document.body.classList.contains("dark-mode");
document.querySelector(".sun").style.fill = isDarkMode
? "transparent"
: "black";
document.querySelector(".moon").style.fill = isDarkMode
? "white"
: "transparent";
};
updateIcon();
toggleButton.addEventListener("click", () => {
const isDarkMode = document.body.classList.toggle("dark-mode");
document.body.classList.toggle("light-mode", !isDarkMode);
localStorage.setItem("theme", isDarkMode ? "dark" : "light");
updateIcon();
});
});
</script>