-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Delay in dark mode detection #22
Comments
Similar issue 😒 |
Any Update? |
This is a known issue, to lessen the impact I suggest to put the switch and the script as high in the body as possible. |
so... I did a workaround this (basically I'm only using the project's .css)
I know it might not be the best way (and it's required to reload the page) but I thought the drawback would be less worst than the white flash when the page loads (which was seriously hurting my eyes). Best regards. |
Any Update? |
Hello, Thanks for great project. It was a breeze to add dark mode to an existing web site with help of this project and most of the time was used to tune colors, not for implementing code. To prevent light flash during page load, I had to split dark-mode-switch.js into two parts; a theme-init part which is inlined into html and executed as early as possible and switch-init part which initializes dark mode switch functionality same as original. dark-mode-init.js : function initDarkModeTheme() {
var darkThemeSelected =
localStorage.getItem("darkSwitch") !== null &&
localStorage.getItem("darkSwitch") === "dark";
darkThemeSelected
? document.body.setAttribute("data-theme", "dark")
: document.body.removeAttribute("data-theme");
} inlined this script and called very high up within body tag. <script>inlined content of dark-mode-init.js<script>
<script type="text/javascript">initDarkModeTheme();</script> changed dark-mode-switch.js to only include code about switch initialization dark-mode-switch.js: var darkSwitch = document.getElementById("darkSwitch");
window.addEventListener("load", function () {
if (darkSwitch) {
var darkThemeSelected =
localStorage.getItem("darkSwitch") !== null &&
localStorage.getItem("darkSwitch") === "dark";
darkSwitch.checked = darkThemeSelected;
darkSwitch.addEventListener("change", function () {
resetTheme();
});
}
});
function resetTheme() {
if (darkSwitch.checked) {
document.body.setAttribute("data-theme", "dark");
localStorage.setItem("darkSwitch", "dark");
} else {
document.body.removeAttribute("data-theme");
localStorage.removeItem("darkSwitch");
}
} calling block is same as the original. below code is bootstrap 4 sample <div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="darkSwitch" />
<label class="custom-control-label" for="darkSwitch">Dark Mode</label>
</div>
<script src="dark-mode-switch.js"></script> Regards, |
heya @modabas - thanks for posting your solution - this works great! |
I drafted the following snipped and run it directly after the body tag: (function () {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.setAttribute("data-theme", "dark")
} else {
document.body.removeAttribute("data-theme")
}
})(); or as closured: (function(){let a=window,b=document.body,c="data-theme",e=a.matchMedia;e&&e("(prefers-color-scheme: dark)").matches?b.setAttribute(c,"dark"):b.removeAttribute(c);})(); |
This worked well for me @basteyy |
Hi! |
Just implement this early in your code. |
it work for me many Thanks!! @basteyy |
It is more a suggestion than a real issue : when my website theme has just loaded, and dark theme was already activated, I can quickly see white theme before dark theme is activated.
I understand why I can see it considering that main theme is loaded before dark theme tag is added to body.
Do you have an idea how to not see this quick blinking on page load ?
The text was updated successfully, but these errors were encountered: