Skip to content

fix: prevent code blocks flash in dark mode (AI-assisted)#11967

Open
titojuanc wants to merge 1 commit into
facebook:mainfrom
titojuanc:main
Open

fix: prevent code blocks flash in dark mode (AI-assisted)#11967
titojuanc wants to merge 1 commit into
facebook:mainfrom
titojuanc:main

Conversation

@titojuanc
Copy link
Copy Markdown

@titojuanc titojuanc commented Apr 27, 2026

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

Fixes #11566

When loading a Docusaurus page in dark mode, code blocks briefly flash with light theme colors before switching to the correct dark theme. This happens because Prism theme colors were applied as inline styles via JavaScript, which only take effect after React hydration.

This PR injects CSS variables for both light and dark Prism themes directly in a <style> tag in the , leveraging the [data-theme] attribute that is already set by a blocking script. This ensures code blocks display with the correct background color from the very first paint, without waiting for hydration.

Test Plan

  1. Configure a site with different light/dark Prism themes (e.g., oneLight / oneDark)
  2. Switch to dark mode
  3. Hard-reload any page with code blocks
  4. Observe that code blocks no longer flash with light theme colors
    (AI-assisted)

Test links

Related issues/PRs

Closes #11566

@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 27, 2026

Hi @titojuanc!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 27, 2026

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit f51b830
🔍 Latest deploy log https://app.netlify.com/projects/docusaurus-2/deploys/69ef4d64103558000809378b
😎 Deploy Preview https://deploy-preview-11967--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@meta-cla meta-cla Bot added the CLA Signed Signed Facebook CLA label Apr 27, 2026
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 27, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Copy Markdown
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

I agree fixing the background is an improvement.

However, this only partially fixes the original issue and doesn't close it.

Code tokens still flash. This can be seen on your preview: https://deploy-preview-11967--docusaurus-2.netlify.app/docs/creating-pages#add-a-react-page

I wonder what the behavior of a live code block is, too.

I'm also not a fan of adding a global inlined CSS tag on all pages. This adds weight to all html pages that do not even include code blocks.

I would prefer if this were bundled as part of the site CSS file, so that the extra weight is only added once per site instead of once per HTML page.

I'll have to think about it a bit more before considering merging.

...props
}: {as: T} & ComponentProps<T>): ReactNode {
const prismTheme = usePrismTheme();
const prismCssVariables = getPrismCssVariables(prismTheme);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Are these functions still used anywhere?

Comment on lines -32 to -36
// The Prism theme on SSR is always the default theme but the site theme can
// be in a different mode. React hydration doesn't update DOM styles that come
// from SSR. Hence force a re-render after mounting to apply the current
// relevant styles.
const isBrowser = useIsBrowser();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Note to self: I'm not sure why we needed this in the first place.

It seems fine to remove?

const prismCssStyle = `
[data-theme='light'] {
--prism-background-color: ${lightTheme.plain.backgroundColor || 'inherit'};
--prism-color: ${lightTheme.plain.color || 'inherit'};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This tag will appear on all pages of a docs website, including those not even showing code blocks, so I'm not a fan of adding this, and if we do it should be minimal.

Is the --prism-color variable actually used anywhere?

As far as I see, all tokens and token lines always have an explicit color

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

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code blocks flashes in dark mode

2 participants