mirror of
https://seed.radicle.dev/rad:z371PVmDHdjJucejRoRYJcDEvD5pp.git
synced 2026-10-31 08:50:47 +08:00
This works without JavaScript, and starts out with the user preference (according to the browser) by default. If JavaScript is available, the toggle writes to LocalStorage.
12 lines
418 B
JavaScript
12 lines
418 B
JavaScript
function loadTheme() {
|
|
const theme = localStorage.getItem("theme") || (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
|
|
document.documentElement.style.colorScheme = theme;
|
|
document.getElementById(theme).checked = true;
|
|
}
|
|
|
|
loadTheme();
|
|
|
|
function toggleTheme(theme) {
|
|
document.documentElement.style.colorScheme = theme;
|
|
localStorage.setItem("theme", theme);
|
|
} |