Skip to content

Conversation

@dannycolin
Copy link

This patch adds new CSS styling to support dark mode.

@dannycolin dannycolin changed the title Add support for dark mode WIP: Add support for dark mode May 3, 2025
@dannycolin dannycolin force-pushed the join-the-dark-side branch from 4b9aabb to 645bba0 Compare May 4, 2025 20:16
Comment on lines +51 to +56
function setDefaultTheme() {
const theme = localStorage.getItem('theme');
if (!theme) {
const prefersLightTheme = window.matchMedia('(prefers-color-scheme: light)');
if (prefersLightTheme.matches) {
localStorage.setItem("theme", "light");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, that's a nice angle with just light-dark() having that natively, and setting document.colorScheme for user customization 💟 🚀 — love that approach!

I'd have one wish here though: Please don't store the preference upon first visit, basically unless the user changes the preference from their defaults. The prefers-color-scheme can change over time (my OS sets it light during daylight and dark at sunset, along with any color shift etc.) — this would effectively lock it at a theme based on the moment I've first visited the site.

That's why most of the UIs use a three-way light/dark/auto, to be able to "unset" whatever preference was made back to media preference defaults… To avoid having a third state, I'd imagine just unsetting the cookie every time the theme matches the preferred theme when switching, being a default at that moment, could also mimic an implicit "auto"/reset?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious here regarding https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme#declaring_color_scheme_preferences quoting:

"Along with the above CSS, also include the <meta name="color-scheme"> HTML <meta> tag in the <head>, before any CSS style information, to inform user agents about the preferred color scheme, helping prevent unwanted screen flashes during the page load."

Is that relevant to this use per se, or any flashing-after-parsing-the-stored-prefs is avoided elsewhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't store the preference upon first visit, basically unless the user changes the preference from their defaults. The prefers-color-scheme can change over time (my OS sets it light during daylight and dark at sunset [...] That's why most of the UIs use a three-way light/dark/auto

Oh good point! I will update the implementation to support it.

Is that relevant to this use per se, or any flashing-after-parsing-the-stored-prefs is avoided elsewhere?

I was also wondering if it can happen with the current use case. Maybe we should use a meta tag to be on the safe side. It's only a matter of targeting the tag property instead of document.documentElement so it's an easy modification to make :).

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