-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add prefers-color-scheme support #601
Conversation
I just noticed that the "dark mode" checkbox in the preferences doesn't work if the user has not selected a preference for dark mode (i.e. the user has not clicked on the toggle) and goes into the preferences. This can be seen if the OS is in light mode and one goes to the preferences and checks "dark mode"—dark mode is enabled for a fraction of a second and then light mode takes effect again due to the system theme. I think a tri-state preference that affects both the cookie and the current appearance is the solution for that, mirroring what has been done in |
Very much appreciated! I'll hopefully have time to test this out properly sometime tomorrow. As you mentioned I expect some handling server-side needs to be added before this is merged. Rather than change the type for |
Thank you. I was just now trying to see if I could change the preferences page, but as you mentioned there is some server-side code (including a As for the Maybe instead of the dark/light button in the top right, there could be a dropdown with the same theme options available from the settings. |
Apologies for the delay. I pushed the necessary changes for this to be supported with existing preferences, cookies, etc. I encountered an issue when testing without localStorage set. To reproduce:
The page will have the desired |
I think the only issue is when first visiting the site without |
I'm sorry, I just now saw your comment detailing the issue with |
When changing a user's theme, the client will send a |
Thank you for the information, and sorry for the delay. I have made some changes so that the |
From some quick testing it LGTM. I'd like to test it out a bit more but otherwise I think this should be ready to merge. |
The above comments are comments that I had written previously in response to the review comments that the two of you had posted. I did not fully understand how GitHub's review system works and I had not noticed that these comments were "Pending" and that I had to "submit" the review to make them public. In any case, all of these review comments concern outdated code or are otherwise resolved. |
This should fix <iv-org#559>. The cookie storage format has been changed from boolean ("true"/"false") to tri-state ("dark"/"light"/""), so that users without a cookie set will get dark mode if they have enabled the dark theme in their operating system. The code for handling the cookie state, along with the user's operating system theme, has been factored out into a new function `update_mode`, which is called both at window load and at the "storage" event listener, because the "storage" event listener is only trigerred when a change is made to the localStorage from another tab/window (for more info - see <https://stackoverflow.com/a/4679754>).
This should clear up some confusion with the way that I had implemented mode setting, making the code more readable.
Use strict equality, to avoid type coercion and conform to JS best practices (as suggested by @leonklingele).
Changed the default value for the preference `dark-mode` from "light" to "", to reflect the fact that `dark-mode` is tri-state and that the default (no preference set) may be the dark theme (depending on `prefers-color-scheme`).
The JS frontend now reads from the preference stored in the Crystal backend to update the localStorage on page load, and checks the preference stored in the backend when updating the mode. This should fix the issues noted in PR iv-org#601 regarding the frontend and backend prefs not being synchronized properly.
Thank you! ❤️ |
* upstream/master: Add 'playlistThumbnail' to playlist objects Use accurate sub count when available Refactor search extractor Fix allowed_regions for globally blocked videos js: add support to detect alt, meta and control key in keydown handler (iv-org#704) Fix playlist_thumbnail extractor js: add support for keydown events (iv-org#678) Change font family to better native selection (iv-org#679) Fix season playlists Add prefers-color-scheme support (iv-org#601)
This pull request has been automatically locked since there has not been any activity in it in the last 30 days. If you want to tell us about needed or wanted changes or if problems related to this code are discovered, feel free to open an issue or a new pull request. |
This should fix #559.
The cookie storage format has been changed from boolean (
"true"
/"false"
) to tri-state ("dark"
/"light"
/""
), so that users without a cookie set will get dark mode if they have enabled the dark theme in their operating system. The code for handling the cookie state, along with the user's operating system theme, has been factored out into a new functionupdate_mode
, which is called both at window load and at the "storage" event listener, because the "storage" event listener is only trigerred when a change is made to the localStorage from another tab/window (for more info, see https://stackoverflow.com/a/4679754).This has worked in my limited testing using Firefox with the "Dark Website Forcer" add-on, using the Docker install instructions on GNU+Linux.