-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(theme): prevent theme flash by respecting system preference on initial load #3840
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
base: master
Are you sure you want to change the base?
Conversation
|
@samarJ19 is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize it. |
Summary of ChangesHello @samarJ19, 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 addresses a critical theme-related regression that caused an inconsistent and visually jarring experience for users on initial page load. By refining the theme application process to be more intelligent and only trigger updates when necessary, the PR ensures that the website consistently respects user preferences without any visual glitches. Additionally, it includes a comprehensive UI modernization effort across documentation and demo pages, enhancing their aesthetic and responsiveness. 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 effectively resolves the theme flashing issue on initial load by introducing an inline script to set the theme before the first paint and refining the JavaScript logic to avoid unnecessary theme applications. The solution is robust, handling both user-saved preferences and system settings correctly. Beyond the primary fix, this PR includes a significant and welcome modernization of the entire documentation UI using Tailwind CSS, which greatly improves the visual design, responsiveness, and maintainability of the codebase. The refactoring of CSS and JavaScript is clean and follows modern best practices. I've included a few minor suggestions to further enhance code quality and robustness.
| const code = pre.querySelector('code').innerText; | ||
| navigator.clipboard.writeText(code); |
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.
This line could potentially throw a runtime error if a <pre> tag exists without a nested <code> tag, as pre.querySelector('code') would return null. To make this more robust, it's good practice to check if the code element exists before accessing its innerText property. You could fall back to pre.innerText if the <code> tag is not found.
| const code = pre.querySelector('code').innerText; | |
| navigator.clipboard.writeText(code); | |
| const codeElement = pre.querySelector('code'); | |
| const code = codeElement ? codeElement.innerText : pre.innerText; | |
| navigator.clipboard.writeText(code); |
| (function () { | ||
| try { | ||
| var theme = localStorage.getItem("theme"); | ||
| var prefersDark = | ||
| window.matchMedia && | ||
| window.matchMedia("(prefers-color-scheme: dark)").matches; | ||
| if (theme === "dark" || (!theme && prefersDark)) { | ||
| document.documentElement.classList.add("dark"); | ||
| } | ||
| } catch (e) {} | ||
| })(); | ||
| </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.
For better code consistency and to adhere to modern JavaScript standards, it's recommended to use const and let instead of var. Since theme and prefersDark are not reassigned, they can be declared as const.
| (function () { | |
| try { | |
| var theme = localStorage.getItem("theme"); | |
| var prefersDark = | |
| window.matchMedia && | |
| window.matchMedia("(prefers-color-scheme: dark)").matches; | |
| if (theme === "dark" || (!theme && prefersDark)) { | |
| document.documentElement.classList.add("dark"); | |
| } | |
| } catch (e) {} | |
| })(); | |
| </script> | |
| (function () { | |
| try { | |
| const theme = localStorage.getItem("theme"); | |
| const prefersDark = | |
| window.matchMedia && | |
| window.matchMedia("(prefers-color-scheme: dark)").matches; | |
| if (theme === "dark" || (!theme && prefersDark)) { | |
| document.documentElement.classList.add("dark"); | |
| } | |
| } catch (e) {} | |
| })(); |
|
@samarJ19 Please rebase from the main branch. I'm seeing 17 commits and 16 of those are merged already. I would expect to see only one commit with your change. |
d51b0ad to
cc63a61
Compare
styfle
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.
Please fix the merge conflicts
<<<<<<< HEAD
cc63a61 to
90cfa99
Compare
samarJ19
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.
fix: three-mode theme system with live OS sync
- First fixed git merge conflicts
Replaced binary toggle with tri-state (system/light/dark): - Added inline script in HTML head to apply theme before first paint (prevents FOUC)
- Reads
theme-preferencefrom localStorage, defaults to 'system' - System mode actively tracks OS changes via matchMedia listener
- Light/dark modes override and ignore OS changes
- Toggle cycles: System 🌓 → Light ☀️ → Dark 🌙
- Syncs theme to demo iframe on load/change
- Normalized storage key to
theme-preference, kept legacythemefor compat - Removed unused error params (linting fix)
First visit respects OS preference, manual toggle overrides, system mode auto-updates when OS theme changes.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
The demo page is always in light mode now |
samarJ19
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.
Fixed the demo page CSS problem
Marked version:
(same version used in PR #3814)
Markdown flavor:
GitHub Flavored Markdown
Description
Fixes regression introduced after Modernize Docs UI with Tailwind, Dark Mode, and Improved Layout (Modernize Docs UI with Tailwind, Dark Mode, and Improved Layout #3814).
The website was not consistently respecting system theme preference on the first load.
The issue occurred because:
The inline
<script>in the<head>correctly applied dark/light mode before render.index.jslater ran onDOMContentLoadedand reapplied the theme unconditionally viaapplyTheme(initialTheme).applyTheme()always removed thedarkclass first → causing:Solution
This PR updates the theme initialization logic so that applyTheme() only runs when necessary.
Specifically:
It now checks whether the current DOM theme already matches the resolved theme (system preference or saved preference).
applyTheme()is called only if a mismatch exists.Prevents unnecessary DOM manipulation and eliminates:
Result
With this fix:
Contributor