Skip to content

Commit

Permalink
fix dark mode error
Browse files Browse the repository at this point in the history
  • Loading branch information
naltatis committed Jun 15, 2024
1 parent 3dcf44f commit 8199ccd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 19 deletions.
16 changes: 3 additions & 13 deletions src/components/DarkModeToggle.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
---

<mode-toggle class="flex">
<button
class="justify-self-end bg-black dark:bg-white ml-4 inline-flex h-6 w-11 items-center rounded-full"
Expand All @@ -21,24 +23,12 @@

<script>
function updateHTMLTheme(mode: "light" | "dark") {
const themeColorMetaTag = document.head.querySelector(
'meta[name="theme-color"]'
);

if (mode === "dark") {
document.documentElement.classList.remove("light");
document.documentElement.classList.add("dark");
if (themeColorMetaTag) {
// @ts-ignore
themeColorMetaTag.content = "#262626";
}
} else {
document.documentElement.classList.remove("dark");
document.documentElement.classList.add("light");
if (themeColorMetaTag) {
// @ts-ignore
themeColorMetaTag.content = "#ffffff";
}
}
}
function updateStorageTheme(mode: "light" | "dark") {
Expand Down Expand Up @@ -107,6 +97,6 @@
}
.dark {
transform: translateX(1.5rem);
background-color: #FF5A55;
background-color: #ff5a55;
}
</style>
7 changes: 1 addition & 6 deletions src/layouts/ThemeScript.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@
(function () {
if (typeof window !== "undefined") {
const isSystemColorSchemeDark = window.matchMedia(
"(prefers-color-scheme: dark)"
"(prefers-color-scheme: dark)",
).matches;
const storageTheme = sessionStorage.getItem("theme");
if (!storageTheme && isSystemColorSchemeDark) {
document.documentElement.classList.add("dark");
document.head.children.namedItem("theme-color").content = "#0f172a";
} else if (storageTheme === "dark") {
document.documentElement.classList.add("dark");
document.head.children.namedItem("theme-color").content = "#0f172a";
} else {
// we already server render light theme
document.head.children.namedItem("theme-color").content = "#ffffff";
}
}
})();
Expand Down

0 comments on commit 8199ccd

Please sign in to comment.