Skip to content
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

White content flash on dark mode #376

Open
Kaedriz opened this issue Dec 20, 2024 · 5 comments
Open

White content flash on dark mode #376

Kaedriz opened this issue Dec 20, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@Kaedriz
Copy link

Kaedriz commented Dec 20, 2024

2024-12-20.15-45-08.mp4

As in the video, bright flash can be seen, even while loading dark mode.

How this could be fixed is up to you, and how do you generate website (SPA, Hybrid or just server rendered). Maybe some cookie, or just color mode detection in plain css which is prefetched. From what I have seen, now, it stores color mode in localStorage, which needs js to access, which in turn does only get interpreted after some milliseconds (but still produces noticeable flash on dark mode).

@KyleDL101
Copy link
Contributor

Myself and @Raiden0456 are looking to fix this sometime soon

@NOCanoa NOCanoa added the bug Something isn't working label Dec 31, 2024
@JSGRD22
Copy link

JSGRD22 commented Jan 5, 2025

This was fixed for a while, but now it's a black flash on light mode instead...

@KyleDL101
Copy link
Contributor

Yes @mr-cheff made dark mode the default mode, so now the page loads dark mode my default first. However, he also deferred the script that loads the theme from local storage, which increases the flash time.

@Kaedriz
Copy link
Author

Kaedriz commented Jan 6, 2025

You have the following code in Layout.astro:

:root {
    --zen-paper: #f2f0e3;
    --zen-dark: #202020;
    --zen-muted: rgba(0, 0, 0, 0.05);

    &[data-theme='dark'] {
      --zen-paper: #202020;
      --zen-dark: #f2f0e3;
      --zen-muted: rgba(255, 255, 255, 0.05);
    }
  }

Couldn't this be change to use prefers-color-scheme first, then if detected, apply chosen color mode? I presume most users use either only dark/light modes and rarely switch from their preferred. This would fix it for users without explicit set color mode in Zen website. (ex. you have selected dark mode in Firefox/Zen, so you get dark mode CSS)

Code then can be changed to first try browser color pref, then JS would check localStorage, if there is any, to change mode to other one. This would still inflict flash on users with explicit set color modes, but would fix it for majority, I guess?

Note that this is just a band-aid solution and not a proper replacement for light/dark mode system.

@Yusyuriv
Copy link

Yusyuriv commented Jan 14, 2025

However, he also deferred the script that loads the theme from local storage, which increases the flash time.

I don't think this is the case. MDN docs say that browsers ignore the defer attribute for inline scripts.

I'm actually curious why the flash happens. I'm currently re-making a website for another open-source project. I also use Astro, and I also use a similar inline script for switching the theme, however, I don't seem to have the same issue with the theme flash. In fact, I forked this repository and deployed it as is, without any changes, and I don't seem to have this issue there either. Could this somehow be related to the hosting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants