mirror of
https://github.com/imfing/hextra.git
synced 2025-06-20 01:21:25 -04:00
feat: add option to set default theme and hide toggle button (#146)
resolves #135 Light / dark theme can be configured via: ```yaml theme: # light | dark | system default: system displayToggle: true ```
This commit is contained in:
@ -33,13 +33,25 @@
|
||||
|
||||
<script>
|
||||
/* Initialize light/dark mode */
|
||||
if (localStorage.getItem("color-theme") === "dark" || (!("color-theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches)) {
|
||||
const defaultTheme = '{{ site.Params.theme.default | default `system`}}';
|
||||
|
||||
const setDarkTheme = () => {
|
||||
document.documentElement.classList.add("dark");
|
||||
document.documentElement.style.colorScheme = "dark";
|
||||
} else {
|
||||
}
|
||||
const setLightTheme = () => {
|
||||
document.documentElement.classList.remove("dark");
|
||||
document.documentElement.style.colorScheme = "light";
|
||||
}
|
||||
|
||||
if ("color-theme" in localStorage) {
|
||||
localStorage.getItem("color-theme") === "dark" ? setDarkTheme() : setLightTheme();
|
||||
} else {
|
||||
defaultTheme === "dark" ? setDarkTheme() : setLightTheme();
|
||||
if (defaultTheme === "system") {
|
||||
window.matchMedia("(prefers-color-scheme: dark)").matches ? setDarkTheme() : setLightTheme();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
{{ partial "custom/head-end.html" . }}
|
||||
|
Reference in New Issue
Block a user