mirror of
https://github.com/caddyserver/website.git
synced 2025-04-22 04:56:17 -04:00
Avoid flash during color scheme change
This commit is contained in:
parent
c24f50cad3
commit
f830794fc4
1 changed files with 16 additions and 10 deletions
|
@ -49,12 +49,12 @@ function cloneTemplate(tplSelector) {
|
||||||
// return document.importNode(elem.content, true);
|
// return document.importNode(elem.content, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
ready(function() {
|
|
||||||
// on page load, immediately set the configured theme
|
|
||||||
setTheme(getTheme());
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// when the system theme changes, apply that to our site if system theme is configured
|
// when the system theme changes, apply that to our site if system theme is configured
|
||||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', ({ matches }) => {
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', ({ matches }) => {
|
||||||
|
@ -74,11 +74,12 @@ function setTheme(theme) {
|
||||||
theme = "system";
|
theme = "system";
|
||||||
}
|
}
|
||||||
localStorage.setItem("theme", theme);
|
localStorage.setItem("theme", theme);
|
||||||
$('#current-theme').innerText = theme;
|
ready(function() { $('#current-theme').innerText = theme; });
|
||||||
if (theme == "system") {
|
let lightOrDarkTheme = theme;
|
||||||
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? "dark" : "light";
|
if (lightOrDarkTheme == "system") {
|
||||||
|
lightOrDarkTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? "dark" : "light";
|
||||||
}
|
}
|
||||||
applyTheme(theme);
|
applyTheme(lightOrDarkTheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyTheme simply adds or removes the 'dark' class to the HTML.
|
// applyTheme simply adds or removes the 'dark' class to the HTML.
|
||||||
|
@ -122,3 +123,8 @@ on('mouseover', '.button', (e) => {
|
||||||
elem.remove();
|
elem.remove();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// immediately set the configured theme to avoid flash
|
||||||
|
setTheme(getTheme());
|
Loading…
Add table
Add a link
Reference in a new issue