Skip to content

Commit

Permalink
feat: support changes in system theme (jackyzha0#484)
Browse files Browse the repository at this point in the history
* feat: support changes in system theme

* fix: run prettier

* fix: add content/.gitkeep
  • Loading branch information
konstfish authored Sep 19, 2023
1 parent 1af9a20 commit b8f17d0
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions quartz/components/scripts/darkmode.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ document.addEventListener("nav", () => {
if (currentTheme === "dark") {
toggleSwitch.checked = true
}

// Listen for changes in prefers-color-scheme
const colorSchemeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)")
colorSchemeMediaQuery.addEventListener("change", (e) => {
const newTheme = e.matches ? "dark" : "light"
document.documentElement.setAttribute("saved-theme", newTheme)
localStorage.setItem("theme", newTheme)
toggleSwitch.checked = e.matches
})
})

0 comments on commit b8f17d0

Please sign in to comment.