auto switch theme #677
Paul-16098
started this conversation in
Guides and resources
Replies: 1 comment
-
|
Nice idea, didn't think of doing it this way. document:
head: |
<script id="auto-switch-theme">
"use strict";
const get_theme = () =>
window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
const set_theme =() => {
let theme = `default-${get_theme()}`;
const themeStyleElem = document.querySelector("#theme-style");
fetch(`${pageData.baseURL}/api/set-theme/${theme}`, {
method: "POST",
})
.then(response => response.text())
.then(newThemeStyle => {
themeStyleElem.innerHTML = newThemeStyle;
document.documentElement.setAttribute("data-theme", theme);
document.documentElement.setAttribute("data-scheme", get_theme());
pageData.theme = theme;
});
}
(function () {
window.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", set_theme);
set_theme();
})();
</script> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions