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

Persist night mode on page load #13229

Merged
merged 1 commit into from
Apr 18, 2017
Merged

Persist night mode on page load #13229

merged 1 commit into from
Apr 18, 2017

Conversation

Bouncey
Copy link
Member

@Bouncey Bouncey commented Feb 7, 2017

Pre-Submission Checklist

  • Your pull request targets the staging branch of freeCodeCamp.
  • Branch starts with either fix/, feature/, or translate/ (e.g. fix/signin-issue)
  • You have only one commit (if not, squash them into one commit).
  • All new and existing tests pass the command npm test. Use git commit --amend to amend any fixes.

Type of Change

  • Small bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds new functionality)
  • Breaking change (fix or feature that would change existing functionality)
  • Add new translation (feature adding new translations)

Checklist:

Description

Adds a key to localStorage which holds the theme preference of the user. This enables night mode to be enabled on page load whilst the bundle evals.

@BerkeleyTrue BerkeleyTrue added the status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. label Feb 7, 2017
@Greenheart Greenheart self-requested a review February 7, 2017 21:36
Copy link
Member

@Greenheart Greenheart left a comment

Choose a reason for hiding this comment

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

@Bouncey Thanks! 😊

This works fine as long as you don't do a hard refresh, so I'd say this is ready!

@Bouncey
Copy link
Member Author

Bouncey commented Feb 7, 2017 via email

@Greenheart
Copy link
Member

@Bouncey It might be caused by the re-downloading bundle? Or maybe the bundle is starting (although this flash doesn't occur on normal pageloads)?

@Bouncey
Copy link
Member Author

Bouncey commented Feb 8, 2017

This post on SO confirmed what I thought. Could it just be an edge case?

@Greenheart
Copy link
Member

@Bouncey As it only occurs when I do hard refreshes, I don't think it will be a problem.

I'm pretty sure this is the expected behavior as clearing the cache will clear the react bundle - which then needs to be re-downloaded before it can detect nightmode again, despite what's in localStorage or not

@Bouncey
Copy link
Member Author

Bouncey commented Feb 8, 2017

Yes, but localStorage remains with the browser, it is independent of the bundle. The code that appends 'night' to the body class list whilst the bundle downloads and evals is outside of the bundle also, it is in a <script> tag at the bottom of the HTML sent from the server.

@Greenheart
Copy link
Member

Greenheart commented Feb 8, 2017

@Bouncey I know about localStorage, but I missed that this code was in the base layout code. Thanks for the clarification!

Then this might be an edge case, or just some lag before the script gets executed (There might be inlined scripts before the one for nightmode?).

@@ -29,6 +29,9 @@ export default function nightModeSaga(
.flatMap(() => {
const { app: { theme } } = getState();
const newTheme = !theme || theme === 'default' ? 'night' : 'default';
if (typeof Storage !== 'undefined') {

This comment was marked as off-topic.

Copy link
Member

@Greenheart Greenheart left a comment

Choose a reason for hiding this comment

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

@BerkeleyTrue Nice catch! Somehow, it worked locally despite using Storage.

This still flashes slightly on pageload, but it might just be how the rendering works.

I think this solves the issue though 😊

@@ -29,6 +29,9 @@ export default function nightModeSaga(
.flatMap(() => {
const { app: { theme } } = getState();
const newTheme = !theme || theme === 'default' ? 'night' : 'default';
if (typeof localStorage !== 'undefined') {
localStorage.setItem('fccTheme', newTheme);
}

This comment was marked as off-topic.

@@ -11,5 +11,12 @@ html(lang='en')
script!= state
script.
window.webpackManifest = !{JSON.stringify(chunkManifest || {})};
script.
if (typeof localStorage !== 'undefined') {
const fccTheme = JSON.parse(localStorage.getItem('fccTheme'));

This comment was marked as off-topic.

This comment was marked as off-topic.

@@ -11,5 +11,12 @@ html(lang='en')
script!= state
script.
window.webpackManifest = !{JSON.stringify(chunkManifest || {})};
script.
if (typeof localStorage !== 'undefined') {
const fccTheme = JSON.parse(localStorage.getItem('fccTheme'));

This comment was marked as off-topic.

@camperbot
Copy link
Contributor

@Bouncey updated the pull request.

@@ -11,5 +11,18 @@ html(lang='en')
script!= state
script.
window.webpackManifest = !{JSON.stringify(chunkManifest || {})};
script.

This comment was marked as off-topic.

if (fccTheme && fccTheme === 'night') {
document.body.classList.add('night');
}
}

This comment was marked as off-topic.

@@ -7,6 +9,10 @@ import {
createErrorObservable
} from '../../common/app/redux/actions';

function persistTheme(theme) {
store.set('fccTheme', theme);

This comment was marked as off-topic.

@camperbot
Copy link
Contributor

@Bouncey updated the pull request.

@BerkeleyTrue BerkeleyTrue merged commit 4942156 into freeCodeCamp:staging Apr 18, 2017
@BerkeleyTrue BerkeleyTrue removed the status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. label Apr 18, 2017
@BerkeleyTrue
Copy link
Contributor

@Bouncey Great work! Thanks for you patience

Happy Coding

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.

None yet

4 participants