2023-02-10 20:22:36 +01:00
|
|
|
(function(){
|
|
|
|
|
2023-03-28 17:26:22 +02:00
|
|
|
const prefersDarkTheme = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
|
|
const prefersLightTheme = window.matchMedia('(prefers-color-scheme: light)').matches;
|
|
|
|
|
|
|
|
const $themeAuto = document.getElementById('theme-auto');
|
|
|
|
const $themeLight = document.getElementById('theme-light');
|
|
|
|
const $themeDark = document.getElementById('theme-dark');
|
|
|
|
|
|
|
|
let currentTheme = localStorage.getItem('theme');
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
if (currentTheme === 'dark') {
|
2023-03-28 17:26:22 +02:00
|
|
|
setModeToDark();
|
2023-02-10 20:22:36 +01:00
|
|
|
} else if (currentTheme === 'light') {
|
2023-03-28 17:26:22 +02:00
|
|
|
setModeToLight();
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
2023-03-28 17:26:22 +02:00
|
|
|
|
|
|
|
$themeAuto.addEventListener('click', _ => {
|
|
|
|
if (currentTheme) {
|
|
|
|
setModeToAuto();
|
|
|
|
} else {
|
|
|
|
setModeToDark();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$themeLight.addEventListener('click', _ => {
|
|
|
|
if (currentTheme !== 'light') {
|
|
|
|
setModeToLight();
|
|
|
|
} else {
|
|
|
|
setModeToAuto();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$themeDark.addEventListener('click', _ => {
|
|
|
|
if (currentTheme !== 'dark') {
|
|
|
|
setModeToDark();
|
2023-02-10 20:22:36 +01:00
|
|
|
} else {
|
2023-03-28 17:26:22 +02:00
|
|
|
setModeToLight();
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-28 17:26:22 +02:00
|
|
|
function setModeToDark() {
|
|
|
|
document.body.classList.remove('light-theme');
|
|
|
|
document.body.classList.add('dark-theme');
|
|
|
|
localStorage.setItem('theme', 'dark');
|
|
|
|
currentTheme = 'dark';
|
|
|
|
|
|
|
|
$themeAuto.classList.remove("selected");
|
|
|
|
$themeLight.classList.remove("selected");
|
|
|
|
$themeDark.classList.add("selected");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setModeToLight() {
|
|
|
|
document.body.classList.remove('dark-theme');
|
|
|
|
document.body.classList.add('light-theme');
|
|
|
|
localStorage.setItem('theme', 'light');
|
|
|
|
currentTheme = 'light';
|
|
|
|
|
|
|
|
$themeAuto.classList.remove("selected");
|
|
|
|
$themeLight.classList.add("selected");
|
|
|
|
$themeDark.classList.remove("selected");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setModeToAuto() {
|
|
|
|
document.body.classList.remove('dark-theme');
|
|
|
|
document.body.classList.remove('light-theme');
|
|
|
|
if (prefersDarkTheme) {
|
|
|
|
document.body.classList.add('dark-theme');
|
|
|
|
} else if (prefersLightTheme) {
|
|
|
|
document.body.classList.add('light-theme');
|
|
|
|
}
|
|
|
|
localStorage.removeItem('theme');
|
|
|
|
currentTheme = undefined;
|
|
|
|
|
|
|
|
$themeAuto.classList.add("selected");
|
|
|
|
$themeLight.classList.remove("selected");
|
|
|
|
$themeDark.classList.remove("selected");
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
})();
|