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

Mitigate against excessive flashing between light and dark themes on startup #787

Open
Jaifroid opened this issue Jan 3, 2022 · 22 comments

Comments

@Jaifroid
Copy link
Member

Jaifroid commented Jan 3, 2022

This issue exists in our current code, but it will be exacerbated by #764 / #771 and will particularly affect the Firefox extension. It is also an accessibility issue (flashing can be dangerous for users with certain conditions).

The issue is that if the user has started the app with the default light theme, and then switches to SW mode and turns on dark theme, then (in the Firefox extension) the app will initially load with the light theme, and will only switch to dark theme when it reboots into SW mode. This causes a very visible flashing between themes. In our existing code, in any mode, the app initially loads with a light background (the default for index.html) and only inverts once the app is fully loaded.

Possible mitigations:

  • Set a neutral or intermediate hard-coded background colour in index.html (e.g. a grey colour?);
  • Use CSS custom properties to force the browser to display a theme based on the prefers-color-scheme media query at startup;
  • When user switches themes in SW mode, a message could be sent to the extension to ensure its theme remains synchronized (we have the code for messaging, but it would add some more code complexity);
  • Reload the app in SW mode much earlier (in init.js, before the rest of the app is fully loaded).

Most of these would not fully eliminate flashing (possibly the use of CSS custom properties could).

@n1thun
Copy link

n1thun commented Jan 3, 2022

Hey @Jaifroid do you need help fixing this or will you be taking it on yourself?

@Jaifroid
Copy link
Member Author

Jaifroid commented Jan 3, 2022

@n1thun Thank you for the offer. Is this something you have experience with (UI in HTML/JS/CSS)? Looking at the resources I've linked above, it looks fairly complicated to optimize properly. If you are comfortable with the techniques outlined especially in https://webcloud.se/blog/2020-04-06-flash-of-unstyled-dark-theme/ then help would be appreciated.

You should note that working on this issue would need to wait until #771 is merged, because the issue is exposed particularly in that PR, in a Firefox Extension set to run in Service Worker mode, due to the fact that the browser does a redirect to the PWA version of this app in that mode.

@n1thun
Copy link

n1thun commented Jan 4, 2022 via email

@Jaifroid
Copy link
Member Author

Jaifroid commented Jan 4, 2022

OK, thanks! I'm hoping to merge in the next day or two. I'll post here when it's done.

@Jaifroid
Copy link
Member Author

Jaifroid commented Jan 9, 2022

@n1thun Just to let you know, as promised, that #771 has been merged to master now. Please pull latest code. Would you like me to assign this issue to you, if you're still interested?

@n1thun
Copy link

n1thun commented Jan 11, 2022

@Jaifroid hey can you provide me the directions to reproduce the issue. From what I see the Firefox plugin does seem to switch to/work in SW mode but the issue seems to say that this occurs when we are in SW mode.

@Jaifroid
Copy link
Member Author

@Jaifroid hey can you provide me the directions to reproduce the issue. From what I see the Firefox plugin does seem to switch to/work in SW mode but the issue seems to say that this occurs when we are in SW mode.

@n1thun The extension you download from the Store (or any released version) doesn't have the workaround that allows booting to SW mode. This is in master (it's only just been merged, not released). You could work with one of the nightly builds of the Extension, but this would require using Firefox Developer Edition. What I would suggest is simply working on the Flash Of Incorrect Style in a simple Firefox or Chromium browser pointed at the development implementation here:

https://kiwix.github.io/kiwix-js/

This contains the latest code in master and will stay up to date with the latest code as it is merged.

For heavier-duty development, I'd recommend serving your code from your own machine using (for example) node/npm's http-server. Then you'd serve the root of the repo and point browser to http://localhost:8080 .

Please note this comment about the current need to clear the appCache (Storage tab in Firefox, Application tab in Chromium) before reloading the app, or you won't see changes. I'm working on a way to make this easier for devs.

@n1thun
Copy link

n1thun commented Jan 11, 2022 via email

@Jaifroid Jaifroid assigned mhutti1 and unassigned Jaifroid Jan 11, 2022
@Jaifroid
Copy link
Member Author

Thanks, done!

@Jaifroid Jaifroid added this to the v3.4 milestone Jan 31, 2022
@mossroy mossroy modified the milestones: v3.4, v3.5 Apr 9, 2022
@Jaifroid
Copy link
Member Author

Setting a background_color key in the manifest.json might help a bit with this issue. See https://developer.mozilla.org/en-US/docs/Web/Manifest/background_color.

@mossroy mossroy modified the milestones: v3.5, v3.6 Aug 4, 2022
@Jaifroid Jaifroid modified the milestones: v3.6, v3.7 Nov 9, 2022
@Jaifroid Jaifroid modified the milestones: v3.7, v3.8 Jan 3, 2023
@Niharika-kakumanu-22
Copy link

Hi there , i know how to do this , can u assign this to me .

@Jaifroid
Copy link
Member Author

@Niharika-kakumanu-22 Thanks for the offer! Could you expand a little on how you propose to fix this issue?

@Niharika-kakumanu-22
Copy link

i will set a hard-coded background in Html and change it, CSS custom properties to force the browser to display a theme based on the prefers-color-scheme media query . or
seperate script above html which will initialize theme to localstorage .

@Niharika-kakumanu-22
Copy link

Can u assign this to me .

@Jaifroid
Copy link
Member Author

OK, thanks you @Niharika-kakumanu-22. Assigning you.

@Jaifroid
Copy link
Member Author

Please follow the guidelines in https://github.com/kiwix/kiwix-js/blob/main/CONTRIBUTING.md. Note in particular that the app caches its own code when run from localhost, and this can make it difficult to debug. You need to turn on the option in Expert Settings to bypass the app cache, and you also need to disable browser caching in Dev Tools Network tab. Then, when you do Ctrl-Shift-R while Dev Tools is open, it should fully refresh its code, incorporating any changes you've made.

@Niharika-kakumanu-22
Copy link

heyy, i couldnt see any flashing between light and dark themes but still changed few things which are mentioned in mitigations above and background_color key in the manifest.json and i did npm test where all 44 are success.

@Jaifroid
Copy link
Member Author

@Niharika-kakumanu-22 Thank you! I think that since this issue was created browser vendors themselves may have implemented some internal mitigations against such flashing, because I don't think we implemented anything. Would you like to make a PR with the changes, so I can test?

@Niharika-kakumanu-22
Copy link

yeah sure

@Niharika-kakumanu-22
Copy link

i did PR and can u see and give approval review

@Niharika-kakumanu-22
Copy link

can u see my PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants