25 lines
619 B
JavaScript
25 lines
619 B
JavaScript
(function () {
|
|
const root = document.documentElement;
|
|
|
|
function applyTheme(theme) {
|
|
root.setAttribute("data-theme", theme);
|
|
localStorage.setItem("theme", theme);
|
|
}
|
|
|
|
function getSystemTheme() {
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
? "dark"
|
|
: "light";
|
|
}
|
|
|
|
function initTheme() {
|
|
const savedTheme = localStorage.getItem("theme");
|
|
applyTheme(savedTheme || getSystemTheme());
|
|
}
|
|
|
|
// expose globally
|
|
window.setTheme = applyTheme;
|
|
|
|
document.addEventListener("DOMContentLoaded", initTheme);
|
|
})();
|