How do I Make Dark Mode 100% Default! #347
|
How can I set dark mode as the default on my website, so that users see dark mode when they first visit the site, but have the option to toggle to light mode, without the ability to switch back to dark mode by default? |
Answered by
mearashadowfax
Mar 2, 2025
Replies: 1 comment 1 reply
|
Hi @Daremonic! To make dark mode the default, replace the <script is:inline>
const HSThemeAppearance = {
init() {
let theme = localStorage.getItem("hs_theme");
if (theme === null) {
theme = "dark";
localStorage.setItem("hs_theme", theme);
}
this.setAppearance(theme);
},
_resetStylesOnLoad() {
const $resetStyles = document.createElement("style");
$resetStyles.innerText = `*{transition: unset !important;}`;
$resetStyles.setAttribute("data-hs-appearance-onload-styles", "");
document.head.appendChild($resetStyles);
return $resetStyles;
},
setAppearance(theme, saveInStore = true, dispatchEvent = true) {
const $resetStylesEl = this._resetStylesOnLoad();
if (saveInStore) {
localStorage.setItem("hs_theme", theme);
}
if (theme === "auto") {
theme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "default";
}
document.querySelector("html").classList.remove("dark");
document.querySelector("html").classList.remove("default");
document.querySelector("html").classList.remove("auto");
document.querySelector("html").classList.add(theme);
setTimeout(() => {
$resetStylesEl.remove();
});
if (dispatchEvent) {
window.dispatchEvent(
new CustomEvent("on-hs-appearance-change", { detail: theme })
);
}
},
getAppearance() {
let theme = this.getOriginalAppearance();
if (theme === "auto") {
theme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "default";
}
return theme;
},
getOriginalAppearance() {
const defaultTheme = "default";
return localStorage.getItem("hs_theme") || defaultTheme;
},
};
HSThemeAppearance.init();
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", () => {
if (HSThemeAppearance.getOriginalAppearance() === "auto") {
HSThemeAppearance.setAppearance("auto", false);
}
});
window.addEventListener("load", () => {
const $clickableThemes = document.querySelectorAll(
"[data-hs-theme-click-value]"
);
const $switchableThemes = document.querySelectorAll(
"[data-hs-theme-switch]"
);
$clickableThemes.forEach(($item) => {
$item.addEventListener("click", () =>
HSThemeAppearance.setAppearance(
$item.getAttribute("data-hs-theme-click-value"),
true,
$item
)
);
});
$switchableThemes.forEach(($item) => {
$item.addEventListener("change", (e) => {
HSThemeAppearance.setAppearance(e.target.checked ? "dark" : "default");
});
$item.checked = HSThemeAppearance.getAppearance() === "dark";
});
window.addEventListener("on-hs-appearance-change", (e) => {
$switchableThemes.forEach(($item) => {
$item.checked = e.detail === "dark";
});
});
});
</script> |
1 reply
Answer selected by
tekorakle
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @Daremonic! To make dark mode the default, replace the
scriptin your Navbar and/or NavbarMegaMenu components with this code. It defaults to dark mode if no theme preference is stored in localStorage: