fix: prevent code blocks flash in dark mode (AI-assisted)#12221
Open
titojuanc wants to merge 2 commits into
Open
fix: prevent code blocks flash in dark mode (AI-assisted)#12221titojuanc wants to merge 2 commits into
titojuanc wants to merge 2 commits into
Conversation
- Generate Prism theme CSS (vars + token rules) to a file in generatedFilesDir via loadContent() and register it via getClientModules() so it is bundled once in the site CSS, not inlined in every HTML page. - Use BARE_PRISM_THEME in <Highlight> to suppress inline styles on token spans; colors are now applied via [data-theme] CSS selectors. - Remove unused getPrismCssVariables, PrismThemeEntry and related imports from codeBlockUtils and internal.ts. Fixes facebook#11566
✅ [V2]Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Collaborator
|
That looks like an interesting solution, thanks. The preview seems to work as expected. I'll review this more carefully later |
|
The latest updates on your projects. Learn more about Argos notifications ↗︎
|
Collaborator
|
Although maybe not that problematic, our visual regression tools reported visual diffs on code blocks: https://app.argos-ci.com/meta-open-source/docusaurus/builds/1781/overview For example, you can see that
I would expect this PR to preserve the exact same colors as before, or this means there's a bug somewhere in your logic, that may be more impactful in some cases. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Pre-flight checklist
Motivation
Fixes #11566
When loading a page in dark mode, code blocks briefly flash with light theme colors before switching to the correct dark colors. This happens because
prism-react-renderer's<Highlight>applies syntax colors as inline styles on every token<span>(e.g.style="color: #e3116c"). Inline styles have higher specificity than any CSS selector, so the[data-theme="dark"]attribute — already set by Docusaurus's blocking inline script before hydration — could not override them.This is a follow-up to #11967, which was closed after review. This PR addresses the two issues raised by @slorber:
<style>tag viainjectHtmlTags(), adding weight to every SSR page even those without code blocks.What changed (AI-assisted)
packages/docusaurus-theme-classic/src/index.tsgeneratePrismCSS(), which produces CSS rules for both themes:--prism-background-color,--prism-color) for the container.[data-theme='light'] .token.<type>and[data-theme='dark'] .token.<type>.loadContent()togeneratedFilesDir/docusaurus-theme-classic/prism-theme.css.getClientModules()so the bundler includes it in the site CSS bundle — loaded once and cached by the browser, not repeated in every HTML page.injectHtmlTags()headTagswith the inline<style>was removed entirely.packages/docusaurus-theme-classic/src/theme/CodeBlock/Content/index.tsxBARE_PRISM_THEME = {plain: {}, styles: []}passed to<Highlight theme={BARE_PRISM_THEME}>. This suppresses all inline styles on token spans while preserving Prism's token class names (e.g.token keyword), which the generated CSS then targets.packages/docusaurus-theme-common/src/utils/codeBlockUtils.tsxandsrc/internal.tsgetPrismCssVariablesand its re-export, as it is no longer used anywhere.PrismThemeEntry,CSSProperties, andPrismThemeimports fromcodeBlockUtils.tsx.Test Plan
Test links
Deploy preview: https://deploy-preview-_____--docusaurus-2.netlify.app/
//then i'll edit this, dw
Related issues/PRs
useColorMode()cannot safely be used with SSR #9629, Need to improve the rendering mechanism of some components #11296