Skip to content

Commit

Permalink
Refactor ThemeButton and ThemeMenu to set icon theme based on system …
Browse files Browse the repository at this point in the history
…preference
  • Loading branch information
madalinpopa committed Sep 29, 2024
1 parent 1fd7ab3 commit ce0ded9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/buttons/ThemeButton.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { Icon } from "astro-icon/components";
<Icon
name="sun"
x-cloak
x-init="setIconTheme;"
x-show="theme === 'dark'"
class="text-xl hover:text-gray-500 dark:hover:text-gray-400"
id="sun-icon"
/>
<Icon
x-cloak
x-show="theme === 'light'"
x-init="setIconTheme;"
name="moon"
class="text-xl hover:text-gray-500 dark:hover:text-gray-400"
id="moon-icon"
Expand Down
10 changes: 10 additions & 0 deletions src/components/navigation/ThemeMenu.astro
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ import { Icon } from "astro-icon/components";
theme: localStorage.theme || "system",
showThemeMenu: false,

setIconTheme() {
if (this.theme === "system") {
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
this.theme = "dark";
} else {
this.theme = "light";
}
}
},

setTheme(newTheme) {
if (newTheme === "light") {
document.documentElement.classList.remove("dark");
Expand Down

0 comments on commit ce0ded9

Please sign in to comment.