Skip to content

feat: use system theme if default - #48568

Merged
Sembauke merged 8 commits into
freeCodeCamp:mainfrom
bryanleemoore:theme-fix
Dec 14, 2022
Merged

Sembauke merged 8 commits into
freeCodeCamp:mainfrom
bryanleemoore:theme-fix

Conversation

@bryanleemoore

Copy link
Copy Markdown
Contributor

Checklist:

  • I have read freeCodeCamp's contribution guidelines.
  • My pull request has a descriptive title (not a vague title like Update index.md)
  • My pull request targets the main branch of freeCodeCamp.
  • I have tested these changes either locally on my machine, or GitPod.

Closes #47299

Draft PR. Currently, it solves issue #47299 by checking if the user has night mode toggled on or theme set to night (must be logged in). If the user is either not logged in or the user has night mode toggled off, the theme is considered to be set to default. If the theme is set to default, then the user's system theme settings are used instead.

While it seems to resolve the short term issue, probably more will need to be added. Whether have it so this setting only applies to logged out users, or rework the setting button so users can choose between system settings or custom settings for theme (as discussed in the Discord server).

@bryanleemoore
bryanleemoore requested review from a team, naomi-lgbt and raisedadead November 22, 2022 05:18
@gitpod-io

gitpod-io Bot commented Nov 22, 2022

Copy link
Copy Markdown

@github-actions github-actions Bot added the platform: learn UI side of the client application that needs familiarity with React, Gatsby etc. label Nov 22, 2022
@ghost

ghost commented Nov 22, 2022 •

Copy link
Copy Markdown

👀 Review this PR in a CodeSee Review Map

View the CodeSee Map of this change

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

@Sboonny
Sboonny marked this pull request as draft November 22, 2022 05:27
@Sboonny

Sboonny commented Nov 22, 2022 •

Copy link
Copy Markdown
Member

I have turn this to draft because of Draft PR mention.

About the code itself, it works but if someone has the system default to dark, they can't access light mode

image
image


Just wondering is it better to wrap the values in variable.css in media query related to preferred theme something like

@media (prefers-color-scheme: dark) {
  .dark-palette {
    --primary-color: var(--gray-00);
    --secondary-color: var(--gray-05);
    --tertiary-color: var(--gray-10);
    --quaternary-color: var(--gray-15);
    --quaternary-background: var(--gray-75);
    --tertiary-background: var(--gray-80);
    --secondary-background: var(--gray-85);
    --primary-background: var(--gray-90);
    --highlight-color: var(--blue-light);
    --highlight-background: var(--blue-dark);
    --selection-color: var(--blue-light-translucent);
    --success-color: var(--green-light);
    --success-background: var(--green-dark);
    --danger-color: var(--red-light);
    --danger-background: var(--red-dark);
    --love-color: var(--love-light);
    --editor-background: var(--editor-background-dark);
  }
}

@media (prefers-color-scheme: light) {
  .light-palette {
    --primary-color: var(--gray-90);
    --secondary-color: var(--gray-85);
    --tertiary-color: var(--gray-80);
    --quaternary-color: var(--gray-75);
    --quaternary-background: var(--gray-15);
    --tertiary-background: var(--gray-10);
    --secondary-background: var(--gray-05);
    --primary-background: var(--gray-00);
    --highlight-color: var(--blue-dark);
    --highlight-background: var(--blue-light);
    --selection-color: var(--blue-dark-translucent);
    --success-color: var(--green-dark);
    --success-background: var(--green-light);
    --danger-color: var(--red-dark);
    --danger-background: var(--red-light);
    --love-color: var(--love-dark);
    --editor-background: var(--editor-background-light);
  }
}

I know there confusion of what is needed, so here are the requirements

  • when user isn't logged in, we should display theme related to their system preference
  • when user login, they should be able to opt out of the system preference, if they want to
  • we should store the preference, so when they log in different devices, the system they opt into be respected and carried between devices

@Sboonny

Sboonny commented Nov 22, 2022

Copy link
Copy Markdown
Member

I have tested the suggestion below and it breaks the website, in weird ways. need to investigate deeply why later.

Just wondering is it better to wrap the values in variable.css in media query related to preferred theme something like

@media (prefers-color-scheme: dark) {
  .dark-palette {
    --primary-color: var(--gray-00);
    --secondary-color: var(--gray-05);
    --tertiary-color: var(--gray-10);
    --quaternary-color: var(--gray-15);
    --quaternary-background: var(--gray-75);
    --tertiary-background: var(--gray-80);
    --secondary-background: var(--gray-85);
    --primary-background: var(--gray-90);
    --highlight-color: var(--blue-light);
    --highlight-background: var(--blue-dark);
    --selection-color: var(--blue-light-translucent);
    --success-color: var(--green-light);
    --success-background: var(--green-dark);
    --danger-color: var(--red-light);
    --danger-background: var(--red-dark);
    --love-color: var(--love-light);
    --editor-background: var(--editor-background-dark);
  }
}

@media (prefers-color-scheme: light) {
  .light-palette {
    --primary-color: var(--gray-90);
    --secondary-color: var(--gray-85);
    --tertiary-color: var(--gray-80);
    --quaternary-color: var(--gray-75);
    --quaternary-background: var(--gray-15);
    --tertiary-background: var(--gray-10);
    --secondary-background: var(--gray-05);
    --primary-background: var(--gray-00);
    --highlight-color: var(--blue-dark);
    --highlight-background: var(--blue-light);
    --selection-color: var(--blue-dark-translucent);
    --success-color: var(--green-dark);
    --success-background: var(--green-light);
    --danger-color: var(--red-dark);
    --danger-background: var(--red-light);
    --love-color: var(--love-dark);
    --editor-background: var(--editor-background-light);
  }
}

@bryanleemoore
bryanleemoore marked this pull request as ready for review November 29, 2022 04:52
@bryanleemoore

bryanleemoore commented Nov 29, 2022 •

Copy link
Copy Markdown
Contributor Author

I have updated the pull request with the new code, and read the ideas in the Discord server. When a camper is logged out, they will be using their system settings for the theme. When the camper is logged in, their theme settings will be based off of the Night Mode toggle in the drop down or user settings, this way they can toggle between dark and light mode still.

@Sboonny Sboonny left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the awesome work, this should solve what Naomi asked in #47299 (comment)

Although, there is a point for convenient, I think this is an improvement and the point can be handled in 2nd PR. I am okay with this being merged, if no one else have issue with it being merged.


I have tested this, here is my tests:

  • While signed out
  • if I set the browser appearance in the setting to system, it is respected
  • if I set the browser appearance in the setting to light, it is respected
  • if I set the browser appearance in the setting to dark, it is respected
  • While signned in
  • if I set the browser appearance to dark, it is respected. (although this is inconvenient, it isn't something that I would worry about currently, as the user has the ability to change it, and we save this in the database for future usage)
  • if I opt into night mode, my preference is updated
  • if I opt into light mode, my preference is updated

@Sboonny Sboonny added the status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. label Nov 29, 2022
bryanleemoore and others added 2 commits November 30, 2022 16:52
Co-authored-by: Muhammed Mustafa <[email protected]>
Co-authored-by: Muhammed Mustafa <[email protected]>

@Sboonny Sboonny left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 🎉, thank you for your contribution to the page! 👍.

@Sembauke Sembauke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@bryanleemoore

bryanleemoore commented Dec 8, 2022 •

Copy link
Copy Markdown
Contributor Author

https://drive.google.com/file/d/17KH-6t3rgW7DB7qsPGVckY41NdkkcZQy/view?usp=sharing

Shown in the video above I can still not change my theme to light if I want to.

@Sembauke thanks for letting me know. I wonder if it has anything to do with Safari. Did you test it on another browser by any chance? I'll look into this.

@Sboonny

Sboonny commented Dec 8, 2022

Copy link
Copy Markdown
Member

https://drive.google.com/file/d/17KH-6t3rgW7DB7qsPGVckY41NdkkcZQy/view?usp=sharing
Shown in the video above I can still not change my theme to light if I want to.

@Sembauke thanks for letting me know. I wonder if it has anything to do with Safari. Did you test it on another browser by any chance? I'll look into this.

I wish but unfortunately, I didn't do my due diligence.

The setting is using component that use theme.tsx, in theme.tsx there is this logic that stops the theme from changing in the setting page

        toggleFlag={() => {
          toggleNightMode(
            currentTheme === Themes.Night ? Themes.Default : Themes.Night
          );
        }}

Applying similar changes there should fix this, here is the link https://github.com/bryanleemoore/freeCodeCamp/blob/a4c459365cf63763c38ed96538b3f2a18d482a6d/client/src/components/settings/theme.tsx#L34-L38

@bryanleemoore

Copy link
Copy Markdown
Contributor Author

https://drive.google.com/file/d/17KH-6t3rgW7DB7qsPGVckY41NdkkcZQy/view?usp=sharing
Shown in the video above I can still not change my theme to light if I want to.

@Sembauke thanks for letting me know. I wonder if it has anything to do with Safari. Did you test it on another browser by any chance? I'll look into this.

I wish but unfortunately, I didn't do my due diligence.

The setting is using component that use theme.tsx, in theme.tsx there is this logic that stops the theme from changing in the setting page

        toggleFlag={() => {
          toggleNightMode(
            currentTheme === Themes.Night ? Themes.Default : Themes.Night
          );
        }}

Applying similar changes there should fix this, here is the link https://github.com/bryanleemoore/freeCodeCamp/blob/a4c459365cf63763c38ed96538b3f2a18d482a6d/client/src/components/settings/theme.tsx#L34-L38

I figured it out. It had to do with the previous change from system back to default in default.tsx. I either missed this bug or I did not do enough testing when making this change from the most recent commit.

Because default represents light, if you were to log in with night mode already toggled off in your user settings the condition useSystemTheme = fetchState.complete && theme === 'default would evaluate to true, therefore using system theme when logged in which is not what we want. Basically, this would affect those with night mode on their system who want to turn night mode off when logged in. I think checking if isSignedIn === false would be better in this case instead of theme === 'default

Changing useSystemTheme = fetchState.complete && theme === 'default to useSystemTheme = fetchState.complete && isSignedIn === false I believe should do the trick in fixing this.

@bryanleemoore
bryanleemoore requested review from Sembauke and removed request for naomi-lgbt and raisedadead December 8, 2022 22:16
@Sembauke
Sembauke requested a review from Sboonny December 14, 2022 14:19
@Sembauke
Sembauke merged commit 628764f into freeCodeCamp:main Dec 14, 2022
@Sembauke

Copy link
Copy Markdown
Member

Thanks it works great!

@ojeytonwilliams ojeytonwilliams mentioned this pull request Dec 15, 2022
4 tasks done
oriooctopus pushed a commit to oriooctopus/freeCodeCamp that referenced this pull request Jan 1, 2023
* feat: use system theme if default

* feat: add system to theme

* feat: detect if system theme should be used

* fix: remove system from Themes

* fix: change 'system' to 'default'

Co-authored-by: Muhammed Mustafa <[email protected]>

* fix: change 'system' to 'default'

Co-authored-by: Muhammed Mustafa <[email protected]>

* fix: check if user is logged in for system theme

Co-authored-by: Muhammed Mustafa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform: learn UI side of the client application that needs familiarity with React, Gatsby etc. status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use System Default for Initial Theme?

3 participants