-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix: Theme flicker on page change #12673
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
Conversation
Summary of ChangesHello @dixita0607, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a visual inconsistency where the website's theme would briefly appear incorrect before loading the user's chosen preference. By strategically reordering the theme application script to execute earlier in the page rendering process, the change ensures a seamless and immediate display of the correct theme, enhancing the user experience by eliminating the distracting flicker. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request addresses the theme flickering issue by moving the theme-setting script to execute earlier in the page load process. The change is logical and correctly placed to prevent the flash of an incorrect theme. I've added one suggestion to improve the robustness of the script by handling cases where localStorage might be unavailable.
| <script> | ||
| const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)'); | ||
| const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode'; | ||
| if (storedTheme === 'auto-mode') { | ||
| document.body.classList.add( | ||
| 'auto-mode', | ||
| prefersDarkMode.matches ? 'dark-mode' : 'light-mode', | ||
| ); | ||
| } else { | ||
| document.body.classList.add(storedTheme); | ||
| } | ||
| </script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing window.localStorage can throw an exception if it's disabled by the user (e.g., in browser privacy settings or incognito mode). This would cause the script to fail, preventing the theme from being applied and potentially disrupting page rendering. To make this more robust, it's best to wrap the logic in a try...catch block to handle such errors gracefully.
<script>
try {
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');
const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
if (storedTheme === 'auto-mode') {
document.body.classList.add(
'auto-mode',
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
);
} else {
document.body.classList.add(storedTheme);
}
} catch (e) {
// localStorage is not available, do nothing and fallback to default.
}
</script>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I go ahead with this suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's land this PR as is to keep it focused, but feel free to open a follow-up PR. Thanks for checking though :)
|
/gcbrun |
|
Visit the preview URL for this PR (updated for commit 3d73738): https://flutter-docs-prod--pr12673-fix-flicker-on-page-change-abp44bg0.web.app |
parlough
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for migrating this fix over!
| <script> | ||
| const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)'); | ||
| const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode'; | ||
| if (storedTheme === 'auto-mode') { | ||
| document.body.classList.add( | ||
| 'auto-mode', | ||
| prefersDarkMode.matches ? 'dark-mode' : 'light-mode', | ||
| ); | ||
| } else { | ||
| document.body.classList.add(storedTheme); | ||
| } | ||
| </script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's land this PR as is to keep it focused, but feel free to open a follow-up PR. Thanks for checking though :)
Fix for theme flash issue that we recently fixed on dart's website dart-lang/site-www#7007
Before
https://github.com/user-attachments/assets/7629b755-fe7f-4f40-9664-e285ed336a23
After
https://github.com/user-attachments/assets/db3b20f9-0cc7-4642-960b-e73de152383b
Presubmit checklist
of 80 characters or fewer.