-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rxyhn
committed
Sep 11, 2024
1 parent
f3075b1
commit 75fe7d0
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
function changeTheme() { | ||
const element = document.documentElement; | ||
const theme = element.classList.contains("dark") ? "light" : "dark"; | ||
|
||
const css = document.createElement("style"); | ||
|
||
css.appendChild( | ||
document.createTextNode( | ||
`* { | ||
-webkit-transition: none !important; | ||
-moz-transition: none !important; | ||
-o-transition: none !important; | ||
-ms-transition: none !important; | ||
transition: none !important; | ||
}`, | ||
), | ||
); | ||
document.head.appendChild(css); | ||
|
||
if (theme === "dark") { | ||
element.classList.add("dark"); | ||
} else { | ||
element.classList.remove("dark"); | ||
} | ||
|
||
window.getComputedStyle(css).opacity; | ||
document.head.removeChild(css); | ||
localStorage.theme = theme; | ||
} | ||
|
||
function preloadTheme() { | ||
const theme = (() => { | ||
const userTheme = localStorage.theme; | ||
|
||
if (userTheme === "light" || userTheme === "dark") { | ||
return userTheme; | ||
} else { | ||
return window.matchMedia("(prefers-color-scheme: dark)").matches | ||
? "dark" | ||
: "light"; | ||
} | ||
})(); | ||
|
||
const element = document.documentElement; | ||
|
||
if (theme === "dark") { | ||
element.classList.add("dark"); | ||
} else { | ||
element.classList.remove("dark"); | ||
} | ||
|
||
localStorage.theme = theme; | ||
} | ||
|
||
window.onload = () => { | ||
function initializeThemeButtons() { | ||
const headerThemeButton = document.getElementById("header-theme-button"); | ||
const drawerThemeButton = document.getElementById("drawer-theme-button"); | ||
headerThemeButton?.addEventListener("click", changeTheme); | ||
drawerThemeButton?.addEventListener("click", changeTheme); | ||
} | ||
|
||
document.addEventListener("astro:after-swap", initializeThemeButtons); | ||
initializeThemeButtons(); | ||
}; | ||
|
||
document.addEventListener("astro:after-swap", preloadTheme); | ||
|
||
preloadTheme(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters