-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use prefers-color-scheme over manual selection #28
Comments
Hi @xeruf thank you for checking We usually use JS to match users' preferences when necessary, We left out the if (window.matchMedia("(prefers-color-scheme:dark)").matches) document.body.classList.add('dark-mode'); If you have any suggestions on how this can be implemented without forcing every project to have a dark version by default feel free to share your thoughts here or to open a pull request. Thanks |
Thank you, I will look into it, Skelet looks like a great project and is the only still active Skeleton fork I found :) |
Thanks a lot. Yes, It seems like everybody jumped on the Tailwind bandwagon and nobody wants to maintain CSS frameworks anymore. |
Match users' preferencesif (window.matchMedia("(prefers-color-scheme:dark)").matches) document.body.classList.add('dark-mode') Avoid FOUC on page loadif (localStorage.getItem("dark-mode") === 'on' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme:dark)').matches)) {
document.documentElement.classList.add('dark-mode')
localStorage.setItem('dark-mode', 'on')
} else {
document.documentElement.classList.remove('dark-mode')
} Toggle dark mode<label for="lightBulb">Light</label>
<input id="lightBulb" type="checkbox" class="switch">
<label for="lightBulb">Dark</label> let checkBox = document.getElementById("lightBulb");
const enableDarkMode = () => {
document.body.classList.add("dark-mode")
checkBox.checked = true;
localStorage.setItem("dark-mode", "on")
};
const disableDarkMode = () => {
document.body.classList.remove("dark-mode")
checkBox.checked = false;
localStorage.setItem("dark-mode", "off")
};
if (localStorage.getItem("dark-mode") === "on") enableDarkMode();
checkBox.addEventListener("click", () => {
if (localStorage.getItem("dark-mode") === "off") { enableDarkMode() }
else { disableDarkMode() }
}); |
https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme
Use the query
@media (prefers-color-scheme: dark)
I want to use only the CSS but still have adaptive dark mode following user preferences.
The text was updated successfully, but these errors were encountered: