Skip to content

Commit 6d27400

Browse files
chore(docusaurus-theme-classic/common): copy over applicable code from lex111/prism-styles-fouc
1 parent 2b8245b commit 6d27400

File tree

5 files changed

+3
-36
lines changed

5 files changed

+3
-36
lines changed

packages/docusaurus-theme-classic/src/theme/CodeBlock/Container/index.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@
77

88
import React, {type ComponentProps} from 'react';
99
import clsx from 'clsx';
10-
import {ThemeClassNames, usePrismTheme} from '@docusaurus/theme-common';
11-
import {getPrismCssVariables} from '@docusaurus/theme-common/internal';
10+
import {ThemeClassNames} from '@docusaurus/theme-common';
1211
import styles from './styles.module.css';
1312

1413
export default function CodeBlockContainer<T extends 'div' | 'pre'>({
1514
as: As,
1615
...props
1716
}: {as: T} & ComponentProps<T>): JSX.Element {
18-
const prismTheme = usePrismTheme();
19-
const prismCssVariables = getPrismCssVariables(prismTheme);
2017
return (
2118
<As
2219
// Polymorphic components are hard to type, without `oneOf` generics
2320
{...(props as any)}
24-
style={prismCssVariables}
2521
className={clsx(
2622
props.className,
2723
styles.codeBlockContainer,

packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
import React, {isValidElement, type ReactNode} from 'react';
9-
import useIsBrowser from '@docusaurus/useIsBrowser';
109
import ElementContent from '@theme/CodeBlock/Content/Element';
1110
import StringContent from '@theme/CodeBlock/Content/String';
1211
import type {Props} from '@theme/CodeBlock';
@@ -29,17 +28,8 @@ export default function CodeBlock({
2928
children: rawChildren,
3029
...props
3130
}: Props): JSX.Element {
32-
// The Prism theme on SSR is always the default theme but the site theme can
33-
// be in a different mode. React hydration doesn't update DOM styles that come
34-
// from SSR. Hence force a re-render after mounting to apply the current
35-
// relevant styles.
36-
const isBrowser = useIsBrowser();
3731
const children = maybeStringifyChildren(rawChildren);
3832
const CodeBlockComp =
3933
typeof children === 'string' ? StringContent : ElementContent;
40-
return (
41-
<CodeBlockComp key={String(isBrowser)} {...props}>
42-
{children as string}
43-
</CodeBlockComp>
44-
);
34+
return <CodeBlockComp {...props}>{children as string}</CodeBlockComp>;
4535
}

packages/docusaurus-theme-common/src/internal.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,4 @@ export {useLockBodyScroll} from './hooks/useLockBodyScroll';
118118
export {useSearchPage} from './hooks/useSearchPage';
119119
export {useCodeWordWrap} from './hooks/useCodeWordWrap';
120120
export {useSkipToContent} from './hooks/useSkipToContent';
121-
export {getPrismCssVariables} from './utils/codeBlockUtils';
122121
export {useBackToTopButton} from './hooks/useBackToTopButton';

packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import type {CSSProperties} from 'react';
98
import rangeParser from 'parse-numeric-range';
10-
import type {PrismTheme} from 'prism-react-renderer';
119

1210
const codeBlockTitleRegex = /title=(?<quote>["'])(?<title>.*?)\1/;
1311
const metastringLinesRangeRegex = /\{(?<range>[\d,-]+)\}/;
@@ -233,19 +231,3 @@ export function parseLines(
233231
});
234232
return {lineClassNames, code};
235233
}
236-
237-
export function getPrismCssVariables(prismTheme: PrismTheme): CSSProperties {
238-
const mapping: {[name: keyof PrismTheme['plain']]: string} = {
239-
color: '--prism-color',
240-
backgroundColor: '--prism-background-color',
241-
};
242-
243-
const properties: {[key: string]: string} = {};
244-
Object.entries(prismTheme.plain).forEach(([key, value]) => {
245-
const varName = mapping[key];
246-
if (varName && typeof value === 'string') {
247-
properties[varName] = value;
248-
}
249-
});
250-
return properties;
251-
}

packages/docusaurus-theme-common/src/utils/useThemeConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export type AnnouncementBarConfig = {
5959

6060
export type PrismConfig = {
6161
theme: PrismTheme;
62-
darkTheme?: PrismTheme;
62+
darkTheme: PrismTheme;
6363
defaultLanguage?: string;
6464
additionalLanguages: string[];
6565
magicComments: MagicCommentConfig[];

0 commit comments

Comments
 (0)