Skip to content

Conversation

@samarJ19
Copy link
Contributor

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.js later ran on DOMContentLoaded and reapplied the theme unconditionally via applyTheme(initialTheme).

    • applyTheme() always removed the dark class first → causing:

      • A momentary flash
      • Theme being overridden incorrectly
      • Race condition between inline script and DOMContentLoaded logic

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:

    • Flash of incorrect theme
    • Double theme updates
    • Race conditions between inline script and JS loader

Result

With this fix:

  • System preference is applied correctly on first visit.
  • User's saved preference (localStorage) still correctly overrides system settings.
  • Theme toggle button continues to work as expected.
  • No visual flashing between light/dark.

Contributor

Screenshot 2025-11-16 154856

@vercel
Copy link

vercel bot commented Nov 16, 2025

@samarJ19 is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@gemini-code-assist
Copy link

Summary of Changes

Hello @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

  • Theme Flash Fix: Resolved a regression where the website would momentarily display an incorrect theme or fail to respect the user's system preference on initial page load.
  • Optimized Theme Application: The theme initialization logic has been updated to only apply theme changes if the current DOM theme does not match the resolved preference (system or saved), preventing unnecessary DOM manipulation.
  • Improved User Experience: This change eliminates visual flashing, double theme updates, and race conditions between inline scripts and JavaScript-based theme application, leading to a smoother and more consistent theme experience.
  • UI Modernization: The documentation and demo pages have undergone significant styling updates, leveraging Tailwind CSS for a more modern layout, improved mobile responsiveness, and better dark mode integration.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines 105 to 139
const code = pre.querySelector('code').innerText;
navigator.clipboard.writeText(code);

Choose a reason for hiding this comment

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

high

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.

Suggested change
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);

Comment on lines 10 to 47
(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>

Choose a reason for hiding this comment

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

medium

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.

Suggested change
(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) {}
})();

@styfle
Copy link
Member

styfle commented Nov 17, 2025

@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.

Copy link
Member

@styfle styfle left a 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

Copy link
Contributor Author

@samarJ19 samarJ19 left a 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-preference from 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 legacy theme for compat
  • Removed unused error params (linting fix)

First visit respects OS preference, manual toggle overrides, system mode auto-updates when OS theme changes.

@samarJ19 samarJ19 requested a review from styfle November 21, 2025 11:10
@vercel
Copy link

vercel bot commented Nov 21, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
marked-website Ready Ready Preview Comment Nov 22, 2025 2:58pm

@UziTech
Copy link
Member

UziTech commented Nov 21, 2025

The demo page is always in light mode now

Copy link
Contributor Author

@samarJ19 samarJ19 left a 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

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.

3 participants