fix(ui): follow OS dark/light mode changes after manual toggle
Only persist theme to localStorage when the user's choice differs from the OS preference. Clear it when they match so the prefers-color-scheme listener remains active and automatically tracks system theme changes.
This commit is contained in:
parent
837c548fd3
commit
195c4e5ef4
1 changed files with 6 additions and 2 deletions
|
|
@ -48,12 +48,16 @@
|
||||||
const toggleTheme = () => {
|
const toggleTheme = () => {
|
||||||
const html = document.documentElement;
|
const html = document.documentElement;
|
||||||
const isDark = html.getAttribute("data-theme") === "dark";
|
const isDark = html.getAttribute("data-theme") === "dark";
|
||||||
|
const osDark = matchMedia("(prefers-color-scheme: dark)").matches;
|
||||||
if (isDark) {
|
if (isDark) {
|
||||||
html.removeAttribute("data-theme");
|
html.removeAttribute("data-theme");
|
||||||
localStorage.setItem("theme", "light");
|
// Only persist if this choice differs from OS preference
|
||||||
|
if (osDark) localStorage.setItem("theme", "light");
|
||||||
|
else localStorage.removeItem("theme");
|
||||||
} else {
|
} else {
|
||||||
html.setAttribute("data-theme", "dark");
|
html.setAttribute("data-theme", "dark");
|
||||||
localStorage.setItem("theme", "dark");
|
if (!osDark) localStorage.setItem("theme", "dark");
|
||||||
|
else localStorage.removeItem("theme");
|
||||||
}
|
}
|
||||||
updateThemeIcons();
|
updateThemeIcons();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue