Expected behavior
Shouldn't there be a function declared that sets the theme based on the systems preference?
Actual behavior
Once the theme is set to system, it should detect if it should set theme to dark or light
Show your code
// On page load or when changing themes, best to add inline in head to avoid FOUC
if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
};
function setDarkTheme() {
document.documentElement.classList.add("dark");
localStorage.theme = "dark";
};
function setLightTheme() {
document.documentElement.classList.remove("dark");
localStorage.theme = "light";
};
function onThemeSwitcherItemClick(event) {
const theme = event.target.dataset.theme;
if (theme === "system") {
localStorage.removeItem("theme");
setSystemTheme(); // <---- This function right here
} else if (theme === "dark") {
setDarkTheme();
} else {
setLightTheme();
}
};
const themeSwitcherItems = document.querySelectorAll("#theme-switcher");
themeSwitcherItems.forEach((item) => {
item.addEventListener("click", onThemeSwitcherItemClick);
});
Suggestion
So the functionality of setSystemTheme() should be something like this:
function setSystemTheme() {
const prefersDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
if (prefersDarkMode) {
setDarkTheme();
} else {
setLightTheme();
}
}
Describe the bug I noticed a function called setSystemTheme() in the docs that is not being used. See https://tailwind-elements.com/docs/standard/getting-started/dark-mode/ under the Manually switching
To Reproduce Steps to reproduce the behavior:
Expected behavior Shouldn't there be a function declared that sets the theme based on the systems preference?
Actual behavior Once the theme is set to system, it should detect if it should set theme to dark or light
Show your code
Suggestion So the functionality of setSystemTheme() should be something like this:
And an extra theme-switcher for the system item: