fix dark mode

This commit is contained in:
ClovertaTheTrilobita 2026-04-04 13:14:25 +03:00
parent c8fbea7b07
commit 3f23b930d3

View file

@ -6,13 +6,17 @@
let pagePath = `/posts/${slug}/`; let pagePath = `/posts/${slug}/`;
function getTheme() { function getTheme() {
return localStorage.getItem("color-theme") === "dark" ? "dark" : "light"; return document.documentElement.classList.contains("dark")
? "dark"
: "light";
} }
function syncRemark42Theme() { function applyRemark42Theme() {
if (window.REMARK42?.changeTheme) { if (window.REMARK42 && typeof window.REMARK42.changeTheme === "function") {
window.REMARK42.changeTheme(getTheme()); window.REMARK42.changeTheme(getTheme());
return true;
} }
return false;
} }
onMount(() => { onMount(() => {
@ -46,10 +50,14 @@
document.body.appendChild(script); document.body.appendChild(script);
} }
syncRemark42Theme(); const applyWhenReady = setInterval(() => {
if (applyRemark42Theme()) {
clearInterval(applyWhenReady);
}
}, 200);
const observer = new MutationObserver(() => { const observer = new MutationObserver(() => {
syncRemark42Theme(); applyRemark42Theme();
}); });
observer.observe(document.documentElement, { observer.observe(document.documentElement, {
@ -57,11 +65,9 @@
attributeFilter: ["class"], attributeFilter: ["class"],
}); });
window.addEventListener("storage", syncRemark42Theme);
return () => { return () => {
clearInterval(applyWhenReady);
observer.disconnect(); observer.disconnect();
window.removeEventListener("storage", syncRemark42Theme);
}; };
}); });
</script> </script>