-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(theme): corrige erreurs TS sur theme-switcher (via PearAI)
- Loading branch information
1 parent
b84f8b1
commit 7a436e3
Showing
1 changed file
with
23 additions
and
24 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 |
---|---|---|
@@ -1,40 +1,39 @@ | ||
// import "./styles.css" | ||
import "./assets/styles.css" | ||
import "virtual:uno.css" | ||
|
||
// Light Dark mode switcher | ||
// JS Source : https://hidde.blog/dark-light/ | ||
var button = document.querySelector(".theme-switcher") | ||
var prefersDark = window.matchMedia("(prefers-color-scheme: dark)") | ||
var currentTheme | ||
let currentTheme = "light" | ||
|
||
if (localStorage.getItem("theme-preference")) { | ||
currentTheme = localStorage.getItem("theme-preference") | ||
const button = document.querySelector(".theme-switcher") // Use the correct selector | ||
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)") | ||
const storedTheme = localStorage.getItem("theme-preference") | ||
if (storedTheme) { | ||
currentTheme = storedTheme | ||
} else if (prefersDark.matches) { | ||
currentTheme = "dark" | ||
} else { | ||
// default | ||
currentTheme = "light" | ||
} | ||
|
||
button.addEventListener("click", function (event) { | ||
currentTheme = | ||
document.documentElement.getAttribute("data-theme-preference") === "dark" | ||
? "light" | ||
: "dark" | ||
setTheme(currentTheme) | ||
}) | ||
if (button) { | ||
button.addEventListener("click", function (event) { | ||
currentTheme = | ||
document.documentElement.getAttribute("data-theme-preference") === "dark" | ||
? "light" | ||
: "dark" | ||
setTheme(currentTheme) | ||
}) | ||
} else { | ||
console.warn("Theme toggle button not found") | ||
} | ||
|
||
prefersDark.addEventListener("change", function (event) { | ||
currentTheme = event.matches ? "dark" : "light" | ||
setTheme(currentTheme) | ||
}) | ||
|
||
function setTheme(currentTheme) { | ||
var pressed = currentTheme === "dark" ? "true" : "false" | ||
document.documentElement.setAttribute("data-theme-preference", currentTheme) | ||
localStorage.setItem("theme-preference", currentTheme) | ||
button.setAttribute("aria-pressed", pressed) | ||
function setTheme(theme: string) { | ||
document.documentElement.setAttribute("data-theme-preference", theme) | ||
localStorage.setItem("theme-preference", theme) | ||
if (button) { | ||
button.setAttribute("aria-pressed", theme === "dark" ? "true" : "false") | ||
} | ||
} | ||
|
||
setTheme(currentTheme) |