fix: prevent code blocks flash in dark mode (AI-assisted)#11967
fix: prevent code blocks flash in dark mode (AI-assisted)#11967titojuanc wants to merge 1 commit into
Conversation
|
Hi @titojuanc! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
✅ [V2]Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
slorber
left a comment
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Are these functions still used anywhere?
| // 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(); |
There was a problem hiding this comment.
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'}; |
There was a problem hiding this comment.
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
Pre-flight checklist
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
(AI-assisted)
Test links
Related issues/PRs
Closes #11566