Skip to content

Add basic prefers-color-scheme support#3

Open
ZoeBijl wants to merge 3 commits intosarahfossheim:prefers-color-schemefrom
ZoeBijl:pcs-support
Open

Add basic prefers-color-scheme support#3
ZoeBijl wants to merge 3 commits intosarahfossheim:prefers-color-schemefrom
ZoeBijl:pcs-support

Conversation

@ZoeBijl
Copy link
Copy Markdown
Contributor

@ZoeBijl ZoeBijl commented Dec 25, 2022

Add support for prefers-color-scheme (PCS).

Settings, you can set which theme should be used:

  • by default (in case PCS isn’t supported)
  • for dark mode
  • for light mode

Supports live switching theme when PCS changes.

Todo:

  • get review by Sarah
  • cleanup the code a bit
  • add on/off setting?

Tested to work with:

  • Safari on macOS 13.1
  • Firefox 108.0.1 on macOS 13.1
  • Chrome 108.0.5359.124 on macOS 13.1

Comment thread index.html
saved: localStorage.getItem('selected-theme'),
dark: 'blue', // Theme to load when prefers-colors-scheme = light
light: 'pink', // Theme to load when prefers-colors-scheme = dark
default: 'orange' // Theme to load when prefers-colors-scheme isn’t supported
Copy link
Copy Markdown
Contributor Author

@ZoeBijl ZoeBijl Dec 25, 2022

Choose a reason for hiding this comment

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

Not sure if these comments here are helpful/needed.

In addition: might be worth mentioning that without JS it’ll default to whatever you set in your CSS with :root.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I like the comments 👍🏻

Comment thread index.html
prefersColorMediaQuery.addEventListener('change', () => {
setDefaultTheme();
applyInitialTheme();
});
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I feel like this and line 65+66 can be combined somehow. But not sure where to go next tbh.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think so too 🤔 I need to sleep on it and revisit it with fresh eyes tomorrow, but I think something along these lines inside the applyInitialTheme function could maybe simplify it:

  • If a theme is saved in the local storage: apply the saved theme.
  • Otherwise, if prefers-color-scheme is supported: apply themes.dark or themes.light based on the media query.
  • If the prefers-color-scheme is not supported: do nothing (it will apply based on the :root).

@sarahfossheim sarahfossheim changed the base branch from main to prefers-color-scheme January 10, 2023 22:38
@sarahfossheim sarahfossheim marked this pull request as ready for review January 10, 2023 22:39
@sarahfossheim sarahfossheim self-requested a review January 10, 2023 22:39
Copy link
Copy Markdown
Owner

@sarahfossheim sarahfossheim left a comment

Choose a reason for hiding this comment

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

Looks good ✨ I'm just unsure what would happen with the prefersColorMediaQuery when prefers-color-scheme isn't supported (which is only the case for IE if I'm not mistaken).

Once it's approved, let's merge in a separate branch (so the main branch is in sync with the steps of the tutorial) and maybe add an extra note about it in the readme file 😊

Comment thread index.html
Comment on lines +26 to +27
const prefersColorMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
const themes = {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm not very familiar with window.matchMedia, what would the return value be when the media query is not supported?

Comment thread index.html Outdated
}

const setDefaultTheme = () => {
themes.default = (prefersColorMediaQuery.matches === true) ? themes.dark : themes.light
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

On line 31 the default theme is set to orange when prefers-color-scheme isn't supported:

default: 'orange' // Theme to load when prefers-colors-scheme isn’t supported

Would this line overwrite it to the light theme (pink) when the media query isn't supported?

Copy link
Copy Markdown
Contributor Author

@ZoeBijl ZoeBijl Jan 22, 2023

Choose a reason for hiding this comment

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

Oh good question, should add that to the todo list:

  • check which theme loads if pcs isn’t supported

Copy link
Copy Markdown
Contributor Author

@ZoeBijl ZoeBijl Dec 28, 2024

Choose a reason for hiding this comment

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

for what it’s worth, if you check your website in IE11 none of the themes load at the moment.

that sounds meaner than i mean it 🫣. i was being lazy about making a test page with the pcs version.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok, according to my test page it just returns a true or false, so no errors.
IE11 showing the output is false

Copy link
Copy Markdown
Contributor Author

@ZoeBijl ZoeBijl Dec 28, 2024

Choose a reason for hiding this comment

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

i think you’re right that the line always overwrites the thing, which is a problem. i’ll work on that.

background info:window.matchMedia("bleh") would return:

MediaQueryList {
  media: 'bleh',
  matches: false,
  onchange: null
}

so given that IE11 supports matchMedia it would just always return false.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i’ve pushed a fix for this. it’ll now correctly set the default theme if pcs isn’t supported.

Comment thread index.html
saved: localStorage.getItem('selected-theme'),
dark: 'blue', // Theme to load when prefers-colors-scheme = light
light: 'pink', // Theme to load when prefers-colors-scheme = dark
default: 'orange' // Theme to load when prefers-colors-scheme isn’t supported
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I like the comments 👍🏻

Comment thread index.html
prefersColorMediaQuery.addEventListener('change', () => {
setDefaultTheme();
applyInitialTheme();
});
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think so too 🤔 I need to sleep on it and revisit it with fresh eyes tomorrow, but I think something along these lines inside the applyInitialTheme function could maybe simplify it:

  • If a theme is saved in the local storage: apply the saved theme.
  • Otherwise, if prefers-color-scheme is supported: apply themes.dark or themes.light based on the media query.
  • If the prefers-color-scheme is not supported: do nothing (it will apply based on the :root).

@ZoeBijl
Copy link
Copy Markdown
Contributor Author

ZoeBijl commented Dec 28, 2024

ok, i think i need to work on this a bit more. if there’s a saved state it shouldn’t switch themes via pcs. that also means that if pcs is responsible for the current theme it shouldn’t save state.

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