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

Avoid theme flicker if system setting is dark #1449

Closed
wants to merge 1 commit into from

Conversation

senekor
Copy link
Contributor

@senekor senekor commented Dec 26, 2024

Previously @media (prefers-color-scheme: dark) was only used in the noscript stylesheet, but that's the best way to immediately set the dark theme without any flickering. That is now integrated into the the main stylesheet.

The flickering is still present if the user has a default system setting of light mode but explicitly set it to dark. Actually, this introduces flickering from dark to light if the user has a system setting of dark but an explicit override of light. So it goes both ways. I think this is a good tradeoff, most users will want the website to just follow the system setting.

@senekor
Copy link
Contributor Author

senekor commented Dec 26, 2024

I'm not a CSS guru, if somebody knows a better way to reuse the dark theme variables inside and outside the media query block, that would be nice.

@senekor
Copy link
Contributor Author

senekor commented Dec 26, 2024

@GuillaumeGomez
Copy link
Member

I don't think this is the right approach. Also, I think it was fixed in #1443.

@senekor
Copy link
Contributor Author

senekor commented Dec 26, 2024

Why don't you think it's the right approach? #1443 didn't fix the issue completely, this does.

@GuillaumeGomez
Copy link
Member

The problem is very likely coming from JS, not from CSS. If the JS changes the theme before the page content rendering starts (like we do in rustdoc for example), then no more flickering. Also, no clue if the website new code was deployed or not.

@senekor
Copy link
Contributor Author

senekor commented Dec 26, 2024

Also, no clue if the website new code was deployed or not.

I checked locally, there is still flickering on master.

I don't know how rustdoc does it, might have to look into it.

Previously, `@media (prefers-color-scheme: dark)` was only used in the
`noscript` stylesheet, but that's the best way to immediately set the
dark theme without any flickering. That is now integrated into the the
main stylesheet.

The flickering is still present if the user has a default system setting
of light mode but explicitly set it to dark. Actually, this introduces
flickering from dark to light if the user has a system setting of dark
but an explicit override of light. So it goes both ways. I think this
is a good tradeoff, most users will want the website to just follow the
system setting.

The `theme-switch.js` can also be simplified a little, since it's not
necessary anymore to set the `data-theme` attribute if there is no
explicit override.
@senekor senekor force-pushed the senekor/wrkqsmkoytlk branch from 8db178d to 2590e94 Compare December 26, 2024 13:31
@senekor
Copy link
Contributor Author

senekor commented Dec 26, 2024

JS could be simplified as well, which has the additional side effect that the page immediately reacts to changed system preferences without a page reload. (not saying that's an important use case)

@senekor
Copy link
Contributor Author

senekor commented Dec 26, 2024

Yeah rustdoc seems to handle it very well. Gonna try to copy that.

Edit: rustdoc does some pretty sophisticated stuff, e.g. sending events to other windows... imo overkill for the blog.

@senekor
Copy link
Contributor Author

senekor commented Dec 26, 2024

superseded by #1450

@senekor senekor closed this Dec 26, 2024
@senekor senekor deleted the senekor/wrkqsmkoytlk branch December 26, 2024 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants