@docusaurus/theme-live-codeblock 3.7.0 → 3.8.0-canary-6327

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/lib/theme/CodeBlock/index.d.ts +8 -4
  2. package/lib/theme/CodeBlock/index.js +12 -13
  3. package/lib/theme/LiveCodeBlock/index.d.ts +9 -0
  4. package/lib/theme/LiveCodeBlock/index.js +12 -0
  5. package/lib/theme/Playground/Container/index.d.ts +9 -0
  6. package/lib/theme/Playground/Container/index.js +11 -0
  7. package/lib/theme/Playground/Container/styles.module.css +13 -0
  8. package/lib/theme/Playground/Editor/index.d.ts +8 -0
  9. package/lib/theme/Playground/Editor/index.js +32 -0
  10. package/lib/theme/Playground/Editor/styles.module.css +17 -0
  11. package/lib/theme/Playground/Header/index.d.ts +10 -0
  12. package/lib/theme/Playground/Header/index.js +12 -0
  13. package/lib/theme/Playground/Header/styles.module.css +21 -0
  14. package/lib/theme/Playground/Layout/index.d.ts +8 -0
  15. package/lib/theme/Playground/Layout/index.js +32 -0
  16. package/lib/theme/Playground/Preview/index.d.ts +8 -0
  17. package/lib/theme/Playground/Preview/index.js +54 -0
  18. package/lib/theme/Playground/Preview/styles.module.css +11 -0
  19. package/lib/theme/Playground/Provider/index.d.ts +9 -0
  20. package/lib/theme/Playground/Provider/index.js +26 -0
  21. package/lib/theme/Playground/index.js +8 -112
  22. package/package.json +7 -7
  23. package/src/theme/CodeBlock/index.tsx +21 -14
  24. package/src/theme/LiveCodeBlock/index.tsx +15 -0
  25. package/src/theme/Playground/Container/index.tsx +16 -0
  26. package/src/theme/Playground/Container/styles.module.css +13 -0
  27. package/src/theme/Playground/Editor/index.tsx +35 -0
  28. package/src/theme/Playground/Editor/styles.module.css +17 -0
  29. package/src/theme/Playground/Header/index.tsx +19 -0
  30. package/src/theme/Playground/Header/styles.module.css +21 -0
  31. package/src/theme/Playground/Layout/index.tsx +37 -0
  32. package/src/theme/Playground/Preview/index.tsx +59 -0
  33. package/src/theme/Playground/Preview/styles.module.css +11 -0
  34. package/src/theme/Playground/Provider/index.tsx +35 -0
  35. package/src/theme/Playground/index.tsx +8 -123
  36. package/src/theme-live-codeblock.d.ts +66 -0
  37. package/lib/theme/Playground/styles.module.css +0 -44
  38. package/src/theme/Playground/styles.module.css +0 -44
  39. package/src/types.d.ts +0 -20
@@ -4,7 +4,11 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import React from 'react';
8
- import { type Props } from '@theme-init/CodeBlock';
9
- declare const _default: (props: Props) => React.JSX.Element;
10
- export default _default;
7
+ import { type ReactNode } from 'react';
8
+ import type { Props as CodeBlockProps } from '@theme/CodeBlock';
9
+ declare module '@theme/CodeBlock' {
10
+ interface Props {
11
+ live?: boolean;
12
+ }
13
+ }
14
+ export default function CodeBlockEnhancer(props: CodeBlockProps): ReactNode;
@@ -5,16 +5,15 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import React from 'react';
8
- import Playground from '@theme/Playground';
9
- import ReactLiveScope from '@theme/ReactLiveScope';
10
- import CodeBlock from '@theme-init/CodeBlock';
11
- const withLiveEditor = (Component) => {
12
- function WrappedComponent(props) {
13
- if (props.live) {
14
- return <Playground scope={ReactLiveScope} {...props} />;
15
- }
16
- return <Component {...props} />;
17
- }
18
- return WrappedComponent;
19
- };
20
- export default withLiveEditor(CodeBlock);
8
+ import OriginalCodeBlock from '@theme-init/CodeBlock';
9
+ import LiveCodeBlock from '@theme/LiveCodeBlock';
10
+ function isLiveCodeBlock(props) {
11
+ return !!props.live;
12
+ }
13
+ export default function CodeBlockEnhancer(props) {
14
+ return isLiveCodeBlock(props) ? (
15
+ <LiveCodeBlock {...props} />
16
+ ) : (
17
+ <OriginalCodeBlock {...props} />
18
+ );
19
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { type ReactNode } from 'react';
8
+ import type { Props } from '@theme/LiveCodeBlock';
9
+ export default function LiveCodeBlock(props: Props): ReactNode;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ import Playground from '@theme/Playground';
9
+ import ReactLiveScope from '@theme/ReactLiveScope';
10
+ export default function LiveCodeBlock(props) {
11
+ return <Playground scope={ReactLiveScope} {...props} />;
12
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { type ReactNode } from 'react';
8
+ import type { Props } from '@theme/Playground/Container';
9
+ export default function PlaygroundContainer({ children }: Props): ReactNode;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ import styles from './styles.module.css';
9
+ export default function PlaygroundContainer({children}) {
10
+ return <div className={styles.playgroundContainer}>{children}</div>;
11
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ .playgroundContainer {
9
+ margin-bottom: var(--ifm-leading);
10
+ border-radius: var(--ifm-global-radius);
11
+ box-shadow: var(--ifm-global-shadow-lw);
12
+ overflow: hidden;
13
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { type ReactNode } from 'react';
8
+ export default function PlaygroundEditor(): ReactNode;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ import {LiveEditor} from 'react-live';
9
+ import useIsBrowser from '@docusaurus/useIsBrowser';
10
+ import Translate from '@docusaurus/Translate';
11
+ import PlaygroundHeader from '@theme/Playground/Header';
12
+ import styles from './styles.module.css';
13
+ export default function PlaygroundEditor() {
14
+ const isBrowser = useIsBrowser();
15
+ return (
16
+ <>
17
+ <PlaygroundHeader>
18
+ <Translate
19
+ id="theme.Playground.liveEditor"
20
+ description="The live editor label of the live codeblocks">
21
+ Live Editor
22
+ </Translate>
23
+ </PlaygroundHeader>
24
+ <LiveEditor
25
+ // We force remount the editor on hydration,
26
+ // otherwise dark prism theme is not applied
27
+ key={String(isBrowser)}
28
+ className={styles.playgroundEditor}
29
+ />
30
+ </>
31
+ );
32
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ .playgroundEditor {
9
+ font: var(--ifm-code-font-size) / var(--ifm-pre-line-height)
10
+ var(--ifm-font-family-monospace) !important;
11
+ /* rtl:ignore */
12
+ direction: ltr;
13
+ }
14
+
15
+ .playgroundEditor pre {
16
+ border-radius: 0;
17
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { type ReactNode } from 'react';
8
+ export default function PlaygroundHeader({ children, }: {
9
+ children: ReactNode;
10
+ }): ReactNode;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ import clsx from 'clsx';
9
+ import styles from './styles.module.css';
10
+ export default function PlaygroundHeader({children}) {
11
+ return <div className={clsx(styles.playgroundHeader)}>{children}</div>;
12
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ .playgroundHeader {
9
+ letter-spacing: 0.08rem;
10
+ padding: 0.75rem;
11
+ text-transform: uppercase;
12
+ font-weight: bold;
13
+ background: var(--ifm-color-emphasis-200);
14
+ color: var(--ifm-color-content);
15
+ font-size: var(--ifm-code-font-size);
16
+ }
17
+
18
+ .playgroundHeader:first-of-type {
19
+ background: var(--ifm-color-emphasis-700);
20
+ color: var(--ifm-color-content-inverse);
21
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { type ReactNode } from 'react';
8
+ export default function PlaygroundLayout(): ReactNode;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ import {useThemeConfig} from '@docusaurus/theme-common';
9
+ import PlaygroundPreview from '@theme/Playground/Preview';
10
+ import PlaygroundEditor from '@theme/Playground/Editor';
11
+ function useLiveCodeBlockThemeConfig() {
12
+ const themeConfig = useThemeConfig();
13
+ return themeConfig.liveCodeBlock;
14
+ }
15
+ export default function PlaygroundLayout() {
16
+ const {playgroundPosition} = useLiveCodeBlockThemeConfig();
17
+ return (
18
+ <>
19
+ {playgroundPosition === 'top' ? (
20
+ <>
21
+ <PlaygroundPreview />
22
+ <PlaygroundEditor />
23
+ </>
24
+ ) : (
25
+ <>
26
+ <PlaygroundEditor />
27
+ <PlaygroundPreview />
28
+ </>
29
+ )}
30
+ </>
31
+ );
32
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { type ReactNode } from 'react';
8
+ export default function PlaygroundPreview(): ReactNode;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ import {LiveError, LivePreview} from 'react-live';
9
+ import BrowserOnly from '@docusaurus/BrowserOnly';
10
+ import {ErrorBoundaryErrorMessageFallback} from '@docusaurus/theme-common';
11
+ import ErrorBoundary from '@docusaurus/ErrorBoundary';
12
+ import Translate from '@docusaurus/Translate';
13
+ import PlaygroundHeader from '@theme/Playground/Header';
14
+ import styles from './styles.module.css';
15
+ function Loader() {
16
+ // Is it worth improving/translating?
17
+ // eslint-disable-next-line @docusaurus/no-untranslated-text
18
+ return <div>Loading...</div>;
19
+ }
20
+ function PlaygroundLivePreview() {
21
+ // No SSR for the live preview
22
+ // See https://github.com/facebook/docusaurus/issues/5747
23
+ return (
24
+ <BrowserOnly fallback={<Loader />}>
25
+ {() => (
26
+ <>
27
+ <ErrorBoundary
28
+ fallback={(params) => (
29
+ <ErrorBoundaryErrorMessageFallback {...params} />
30
+ )}>
31
+ <LivePreview />
32
+ </ErrorBoundary>
33
+ <LiveError />
34
+ </>
35
+ )}
36
+ </BrowserOnly>
37
+ );
38
+ }
39
+ export default function PlaygroundPreview() {
40
+ return (
41
+ <>
42
+ <PlaygroundHeader>
43
+ <Translate
44
+ id="theme.Playground.result"
45
+ description="The result label of the live codeblocks">
46
+ Result
47
+ </Translate>
48
+ </PlaygroundHeader>
49
+ <div className={styles.playgroundPreview}>
50
+ <PlaygroundLivePreview />
51
+ </div>
52
+ </>
53
+ );
54
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ .playgroundPreview {
9
+ padding: 1rem;
10
+ background-color: var(--ifm-pre-background);
11
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import { type ReactNode } from 'react';
8
+ import type { Props } from '@theme/Playground/Provider';
9
+ export default function PlaygroundProvider({ code, children, ...props }: Props): ReactNode;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import React from 'react';
8
+ import {LiveProvider} from 'react-live';
9
+ import {usePrismTheme} from '@docusaurus/theme-common';
10
+ // this should rather be a stable function
11
+ // see https://github.com/facebook/docusaurus/issues/9630#issuecomment-1855682643
12
+ const DEFAULT_TRANSFORM_CODE = (code) => `${code};`;
13
+ export default function PlaygroundProvider({code, children, ...props}) {
14
+ const prismTheme = usePrismTheme();
15
+ const noInline = props.metastring?.includes('noInline') ?? false;
16
+ return (
17
+ <LiveProvider
18
+ noInline={noInline}
19
+ theme={prismTheme}
20
+ {...props}
21
+ code={code?.replace(/\n$/, '')}
22
+ transformCode={props.transformCode ?? DEFAULT_TRANSFORM_CODE}>
23
+ {children}
24
+ </LiveProvider>
25
+ );
26
+ }
@@ -5,119 +5,15 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
  import React from 'react';
8
- import clsx from 'clsx';
9
- import useIsBrowser from '@docusaurus/useIsBrowser';
10
- import {LiveProvider, LiveEditor, LiveError, LivePreview} from 'react-live';
11
- import Translate from '@docusaurus/Translate';
12
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
13
- import BrowserOnly from '@docusaurus/BrowserOnly';
14
- import {
15
- ErrorBoundaryErrorMessageFallback,
16
- usePrismTheme,
17
- } from '@docusaurus/theme-common';
18
- import ErrorBoundary from '@docusaurus/ErrorBoundary';
19
- import styles from './styles.module.css';
20
- function Header({children}) {
21
- return <div className={clsx(styles.playgroundHeader)}>{children}</div>;
22
- }
23
- function LivePreviewLoader() {
24
- // Is it worth improving/translating?
25
- // eslint-disable-next-line @docusaurus/no-untranslated-text
26
- return <div>Loading...</div>;
27
- }
28
- function Preview() {
29
- // No SSR for the live preview
30
- // See https://github.com/facebook/docusaurus/issues/5747
31
- return (
32
- <BrowserOnly fallback={<LivePreviewLoader />}>
33
- {() => (
34
- <>
35
- <ErrorBoundary
36
- fallback={(params) => (
37
- <ErrorBoundaryErrorMessageFallback {...params} />
38
- )}>
39
- <LivePreview />
40
- </ErrorBoundary>
41
- <LiveError />
42
- </>
43
- )}
44
- </BrowserOnly>
45
- );
46
- }
47
- function ResultWithHeader() {
48
- return (
49
- <>
50
- <Header>
51
- <Translate
52
- id="theme.Playground.result"
53
- description="The result label of the live codeblocks">
54
- Result
55
- </Translate>
56
- </Header>
57
- {/* https://github.com/facebook/docusaurus/issues/5747 */}
58
- <div className={styles.playgroundPreview}>
59
- <Preview />
60
- </div>
61
- </>
62
- );
63
- }
64
- function ThemedLiveEditor() {
65
- const isBrowser = useIsBrowser();
66
- return (
67
- <LiveEditor
68
- // We force remount the editor on hydration,
69
- // otherwise dark prism theme is not applied
70
- key={String(isBrowser)}
71
- className={styles.playgroundEditor}
72
- />
73
- );
74
- }
75
- function EditorWithHeader() {
76
- return (
77
- <>
78
- <Header>
79
- <Translate
80
- id="theme.Playground.liveEditor"
81
- description="The live editor label of the live codeblocks">
82
- Live Editor
83
- </Translate>
84
- </Header>
85
- <ThemedLiveEditor />
86
- </>
87
- );
88
- }
89
- // this should rather be a stable function
90
- // see https://github.com/facebook/docusaurus/issues/9630#issuecomment-1855682643
91
- const DEFAULT_TRANSFORM_CODE = (code) => `${code};`;
8
+ import PlaygroundProvider from '@theme/Playground/Provider';
9
+ import PlaygroundContainer from '@theme/Playground/Container';
10
+ import PlaygroundLayout from '@theme/Playground/Layout';
92
11
  export default function Playground({children, transformCode, ...props}) {
93
- const {
94
- siteConfig: {themeConfig},
95
- } = useDocusaurusContext();
96
- const {
97
- liveCodeBlock: {playgroundPosition},
98
- } = themeConfig;
99
- const prismTheme = usePrismTheme();
100
- const noInline = props.metastring?.includes('noInline') ?? false;
101
12
  return (
102
- <div className={styles.playgroundContainer}>
103
- <LiveProvider
104
- code={children?.replace(/\n$/, '')}
105
- noInline={noInline}
106
- transformCode={transformCode ?? DEFAULT_TRANSFORM_CODE}
107
- theme={prismTheme}
108
- {...props}>
109
- {playgroundPosition === 'top' ? (
110
- <>
111
- <ResultWithHeader />
112
- <EditorWithHeader />
113
- </>
114
- ) : (
115
- <>
116
- <EditorWithHeader />
117
- <ResultWithHeader />
118
- </>
119
- )}
120
- </LiveProvider>
121
- </div>
13
+ <PlaygroundContainer>
14
+ <PlaygroundProvider code={children} {...props}>
15
+ <PlaygroundLayout />
16
+ </PlaygroundProvider>
17
+ </PlaygroundContainer>
122
18
  );
123
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/theme-live-codeblock",
3
- "version": "3.7.0",
3
+ "version": "3.8.0-canary-6327",
4
4
  "description": "Docusaurus live code block component.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/theme-live-codeblock.d.ts",
@@ -23,10 +23,10 @@
23
23
  },
24
24
  "license": "MIT",
25
25
  "dependencies": {
26
- "@docusaurus/core": "3.7.0",
27
- "@docusaurus/theme-common": "3.7.0",
28
- "@docusaurus/theme-translations": "3.7.0",
29
- "@docusaurus/utils-validation": "3.7.0",
26
+ "@docusaurus/core": "3.8.0-canary-6327",
27
+ "@docusaurus/theme-common": "3.8.0-canary-6327",
28
+ "@docusaurus/theme-translations": "3.8.0-canary-6327",
29
+ "@docusaurus/utils-validation": "3.8.0-canary-6327",
30
30
  "@philpl/buble": "^0.19.7",
31
31
  "clsx": "^2.0.0",
32
32
  "fs-extra": "^11.1.1",
@@ -34,7 +34,7 @@
34
34
  "tslib": "^2.6.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@docusaurus/types": "3.7.0",
37
+ "@docusaurus/types": "3.8.0-canary-6327",
38
38
  "@types/buble": "^0.20.1"
39
39
  },
40
40
  "peerDependencies": {
@@ -44,5 +44,5 @@
44
44
  "engines": {
45
45
  "node": ">=18.0"
46
46
  },
47
- "gitHead": "dd59750c16fe6908a26f18806a54d4c3dbe6db43"
47
+ "gitHead": "d50539dea74625c994ac3f78926ed7b37d704c84"
48
48
  }
@@ -5,21 +5,28 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import React from 'react';
9
- import Playground from '@theme/Playground';
10
- import ReactLiveScope from '@theme/ReactLiveScope';
11
- import CodeBlock, {type Props} from '@theme-init/CodeBlock';
8
+ import React, {type ReactNode} from 'react';
9
+ import type {Props as CodeBlockProps} from '@theme/CodeBlock';
10
+ import OriginalCodeBlock from '@theme-init/CodeBlock';
11
+ import LiveCodeBlock from '@theme/LiveCodeBlock';
12
12
 
13
- const withLiveEditor = (Component: typeof CodeBlock) => {
14
- function WrappedComponent(props: Props) {
15
- if (props.live) {
16
- return <Playground scope={ReactLiveScope} {...props} />;
17
- }
18
-
19
- return <Component {...props} />;
13
+ // TODO Docusaurus v4: remove special case
14
+ // see packages/docusaurus-mdx-loader/src/remark/mdx1Compat/codeCompatPlugin.ts
15
+ // we can just use the metastring instead
16
+ declare module '@theme/CodeBlock' {
17
+ interface Props {
18
+ live?: boolean;
20
19
  }
20
+ }
21
21
 
22
- return WrappedComponent;
23
- };
22
+ function isLiveCodeBlock(props: CodeBlockProps): boolean {
23
+ return !!props.live;
24
+ }
24
25
 
25
- export default withLiveEditor(CodeBlock);
26
+ export default function CodeBlockEnhancer(props: CodeBlockProps): ReactNode {
27
+ return isLiveCodeBlock(props) ? (
28
+ <LiveCodeBlock {...props} />
29
+ ) : (
30
+ <OriginalCodeBlock {...props} />
31
+ );
32
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode} from 'react';
9
+ import Playground from '@theme/Playground';
10
+ import ReactLiveScope from '@theme/ReactLiveScope';
11
+ import type {Props} from '@theme/LiveCodeBlock';
12
+
13
+ export default function LiveCodeBlock(props: Props): ReactNode {
14
+ return <Playground scope={ReactLiveScope} {...props} />;
15
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode} from 'react';
9
+
10
+ import type {Props} from '@theme/Playground/Container';
11
+
12
+ import styles from './styles.module.css';
13
+
14
+ export default function PlaygroundContainer({children}: Props): ReactNode {
15
+ return <div className={styles.playgroundContainer}>{children}</div>;
16
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ .playgroundContainer {
9
+ margin-bottom: var(--ifm-leading);
10
+ border-radius: var(--ifm-global-radius);
11
+ box-shadow: var(--ifm-global-shadow-lw);
12
+ overflow: hidden;
13
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode} from 'react';
9
+ import {LiveEditor} from 'react-live';
10
+ import useIsBrowser from '@docusaurus/useIsBrowser';
11
+ import Translate from '@docusaurus/Translate';
12
+ import PlaygroundHeader from '@theme/Playground/Header';
13
+
14
+ import styles from './styles.module.css';
15
+
16
+ export default function PlaygroundEditor(): ReactNode {
17
+ const isBrowser = useIsBrowser();
18
+ return (
19
+ <>
20
+ <PlaygroundHeader>
21
+ <Translate
22
+ id="theme.Playground.liveEditor"
23
+ description="The live editor label of the live codeblocks">
24
+ Live Editor
25
+ </Translate>
26
+ </PlaygroundHeader>
27
+ <LiveEditor
28
+ // We force remount the editor on hydration,
29
+ // otherwise dark prism theme is not applied
30
+ key={String(isBrowser)}
31
+ className={styles.playgroundEditor}
32
+ />
33
+ </>
34
+ );
35
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ .playgroundEditor {
9
+ font: var(--ifm-code-font-size) / var(--ifm-pre-line-height)
10
+ var(--ifm-font-family-monospace) !important;
11
+ /* rtl:ignore */
12
+ direction: ltr;
13
+ }
14
+
15
+ .playgroundEditor pre {
16
+ border-radius: 0;
17
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode} from 'react';
9
+ import clsx from 'clsx';
10
+
11
+ import styles from './styles.module.css';
12
+
13
+ export default function PlaygroundHeader({
14
+ children,
15
+ }: {
16
+ children: ReactNode;
17
+ }): ReactNode {
18
+ return <div className={clsx(styles.playgroundHeader)}>{children}</div>;
19
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ .playgroundHeader {
9
+ letter-spacing: 0.08rem;
10
+ padding: 0.75rem;
11
+ text-transform: uppercase;
12
+ font-weight: bold;
13
+ background: var(--ifm-color-emphasis-200);
14
+ color: var(--ifm-color-content);
15
+ font-size: var(--ifm-code-font-size);
16
+ }
17
+
18
+ .playgroundHeader:first-of-type {
19
+ background: var(--ifm-color-emphasis-700);
20
+ color: var(--ifm-color-content-inverse);
21
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode} from 'react';
9
+ import {useThemeConfig} from '@docusaurus/theme-common';
10
+ import PlaygroundPreview from '@theme/Playground/Preview';
11
+ import PlaygroundEditor from '@theme/Playground/Editor';
12
+
13
+ import type {ThemeConfig} from '@docusaurus/theme-live-codeblock';
14
+
15
+ function useLiveCodeBlockThemeConfig() {
16
+ const themeConfig = useThemeConfig() as unknown as ThemeConfig;
17
+ return themeConfig.liveCodeBlock;
18
+ }
19
+
20
+ export default function PlaygroundLayout(): ReactNode {
21
+ const {playgroundPosition} = useLiveCodeBlockThemeConfig();
22
+ return (
23
+ <>
24
+ {playgroundPosition === 'top' ? (
25
+ <>
26
+ <PlaygroundPreview />
27
+ <PlaygroundEditor />
28
+ </>
29
+ ) : (
30
+ <>
31
+ <PlaygroundEditor />
32
+ <PlaygroundPreview />
33
+ </>
34
+ )}
35
+ </>
36
+ );
37
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode} from 'react';
9
+ import {LiveError, LivePreview} from 'react-live';
10
+ import BrowserOnly from '@docusaurus/BrowserOnly';
11
+ import {ErrorBoundaryErrorMessageFallback} from '@docusaurus/theme-common';
12
+ import ErrorBoundary from '@docusaurus/ErrorBoundary';
13
+ import Translate from '@docusaurus/Translate';
14
+ import PlaygroundHeader from '@theme/Playground/Header';
15
+
16
+ import styles from './styles.module.css';
17
+
18
+ function Loader() {
19
+ // Is it worth improving/translating?
20
+ // eslint-disable-next-line @docusaurus/no-untranslated-text
21
+ return <div>Loading...</div>;
22
+ }
23
+
24
+ function PlaygroundLivePreview(): ReactNode {
25
+ // No SSR for the live preview
26
+ // See https://github.com/facebook/docusaurus/issues/5747
27
+ return (
28
+ <BrowserOnly fallback={<Loader />}>
29
+ {() => (
30
+ <>
31
+ <ErrorBoundary
32
+ fallback={(params) => (
33
+ <ErrorBoundaryErrorMessageFallback {...params} />
34
+ )}>
35
+ <LivePreview />
36
+ </ErrorBoundary>
37
+ <LiveError />
38
+ </>
39
+ )}
40
+ </BrowserOnly>
41
+ );
42
+ }
43
+
44
+ export default function PlaygroundPreview(): ReactNode {
45
+ return (
46
+ <>
47
+ <PlaygroundHeader>
48
+ <Translate
49
+ id="theme.Playground.result"
50
+ description="The result label of the live codeblocks">
51
+ Result
52
+ </Translate>
53
+ </PlaygroundHeader>
54
+ <div className={styles.playgroundPreview}>
55
+ <PlaygroundLivePreview />
56
+ </div>
57
+ </>
58
+ );
59
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ .playgroundPreview {
9
+ padding: 1rem;
10
+ background-color: var(--ifm-pre-background);
11
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import React, {type ReactNode} from 'react';
9
+ import {LiveProvider} from 'react-live';
10
+ import {usePrismTheme} from '@docusaurus/theme-common';
11
+
12
+ import type {Props} from '@theme/Playground/Provider';
13
+
14
+ // this should rather be a stable function
15
+ // see https://github.com/facebook/docusaurus/issues/9630#issuecomment-1855682643
16
+ const DEFAULT_TRANSFORM_CODE = (code: string) => `${code};`;
17
+
18
+ export default function PlaygroundProvider({
19
+ code,
20
+ children,
21
+ ...props
22
+ }: Props): ReactNode {
23
+ const prismTheme = usePrismTheme();
24
+ const noInline = props.metastring?.includes('noInline') ?? false;
25
+ return (
26
+ <LiveProvider
27
+ noInline={noInline}
28
+ theme={prismTheme}
29
+ {...props}
30
+ code={code?.replace(/\n$/, '')}
31
+ transformCode={props.transformCode ?? DEFAULT_TRANSFORM_CODE}>
32
+ {children}
33
+ </LiveProvider>
34
+ );
35
+ }
@@ -6,137 +6,22 @@
6
6
  */
7
7
 
8
8
  import React, {type ReactNode} from 'react';
9
- import clsx from 'clsx';
10
- import useIsBrowser from '@docusaurus/useIsBrowser';
11
- import {LiveProvider, LiveEditor, LiveError, LivePreview} from 'react-live';
12
- import Translate from '@docusaurus/Translate';
13
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
14
- import BrowserOnly from '@docusaurus/BrowserOnly';
15
- import {
16
- ErrorBoundaryErrorMessageFallback,
17
- usePrismTheme,
18
- } from '@docusaurus/theme-common';
19
- import ErrorBoundary from '@docusaurus/ErrorBoundary';
9
+ import PlaygroundProvider from '@theme/Playground/Provider';
10
+ import PlaygroundContainer from '@theme/Playground/Container';
11
+ import PlaygroundLayout from '@theme/Playground/Layout';
20
12
 
21
13
  import type {Props} from '@theme/Playground';
22
- import type {ThemeConfig} from '@docusaurus/theme-live-codeblock';
23
-
24
- import styles from './styles.module.css';
25
-
26
- function Header({children}: {children: ReactNode}) {
27
- return <div className={clsx(styles.playgroundHeader)}>{children}</div>;
28
- }
29
-
30
- function LivePreviewLoader() {
31
- // Is it worth improving/translating?
32
- // eslint-disable-next-line @docusaurus/no-untranslated-text
33
- return <div>Loading...</div>;
34
- }
35
-
36
- function Preview() {
37
- // No SSR for the live preview
38
- // See https://github.com/facebook/docusaurus/issues/5747
39
- return (
40
- <BrowserOnly fallback={<LivePreviewLoader />}>
41
- {() => (
42
- <>
43
- <ErrorBoundary
44
- fallback={(params) => (
45
- <ErrorBoundaryErrorMessageFallback {...params} />
46
- )}>
47
- <LivePreview />
48
- </ErrorBoundary>
49
- <LiveError />
50
- </>
51
- )}
52
- </BrowserOnly>
53
- );
54
- }
55
-
56
- function ResultWithHeader() {
57
- return (
58
- <>
59
- <Header>
60
- <Translate
61
- id="theme.Playground.result"
62
- description="The result label of the live codeblocks">
63
- Result
64
- </Translate>
65
- </Header>
66
- {/* https://github.com/facebook/docusaurus/issues/5747 */}
67
- <div className={styles.playgroundPreview}>
68
- <Preview />
69
- </div>
70
- </>
71
- );
72
- }
73
-
74
- function ThemedLiveEditor() {
75
- const isBrowser = useIsBrowser();
76
- return (
77
- <LiveEditor
78
- // We force remount the editor on hydration,
79
- // otherwise dark prism theme is not applied
80
- key={String(isBrowser)}
81
- className={styles.playgroundEditor}
82
- />
83
- );
84
- }
85
-
86
- function EditorWithHeader() {
87
- return (
88
- <>
89
- <Header>
90
- <Translate
91
- id="theme.Playground.liveEditor"
92
- description="The live editor label of the live codeblocks">
93
- Live Editor
94
- </Translate>
95
- </Header>
96
- <ThemedLiveEditor />
97
- </>
98
- );
99
- }
100
-
101
- // this should rather be a stable function
102
- // see https://github.com/facebook/docusaurus/issues/9630#issuecomment-1855682643
103
- const DEFAULT_TRANSFORM_CODE = (code: string) => `${code};`;
104
14
 
105
15
  export default function Playground({
106
16
  children,
107
17
  transformCode,
108
18
  ...props
109
19
  }: Props): ReactNode {
110
- const {
111
- siteConfig: {themeConfig},
112
- } = useDocusaurusContext();
113
- const {
114
- liveCodeBlock: {playgroundPosition},
115
- } = themeConfig as ThemeConfig;
116
- const prismTheme = usePrismTheme();
117
-
118
- const noInline = props.metastring?.includes('noInline') ?? false;
119
-
120
20
  return (
121
- <div className={styles.playgroundContainer}>
122
- <LiveProvider
123
- code={children?.replace(/\n$/, '')}
124
- noInline={noInline}
125
- transformCode={transformCode ?? DEFAULT_TRANSFORM_CODE}
126
- theme={prismTheme}
127
- {...props}>
128
- {playgroundPosition === 'top' ? (
129
- <>
130
- <ResultWithHeader />
131
- <EditorWithHeader />
132
- </>
133
- ) : (
134
- <>
135
- <EditorWithHeader />
136
- <ResultWithHeader />
137
- </>
138
- )}
139
- </LiveProvider>
140
- </div>
21
+ <PlaygroundContainer>
22
+ <PlaygroundProvider code={children} {...props}>
23
+ <PlaygroundLayout />
24
+ </PlaygroundProvider>
25
+ </PlaygroundContainer>
141
26
  );
142
27
  }
@@ -16,6 +16,14 @@ declare module '@docusaurus/theme-live-codeblock' {
16
16
  };
17
17
  }
18
18
 
19
+ declare module '@theme/LiveCodeBlock' {
20
+ import type {Props as BaseProps} from '@theme/CodeBlock';
21
+
22
+ export interface Props extends BaseProps {}
23
+
24
+ export default function LiveCodeBlock(props: Props): ReactNode;
25
+ }
26
+
19
27
  declare module '@theme/Playground' {
20
28
  import type {ReactNode} from 'react';
21
29
  import type {Props as BaseProps} from '@theme/CodeBlock';
@@ -31,6 +39,64 @@ declare module '@theme/Playground' {
31
39
  export default function Playground(props: LiveProviderProps): ReactNode;
32
40
  }
33
41
 
42
+ declare module '@theme/Playground/Provider' {
43
+ import type {ReactNode} from 'react';
44
+ import type {Props as PlaygroundProps} from '@theme/Playground';
45
+
46
+ export interface Props extends Omit<PlaygroundProps, 'children'> {
47
+ code: string | undefined;
48
+ children: ReactNode;
49
+ }
50
+
51
+ export default function PlaygroundProvider(props: Props): ReactNode;
52
+ }
53
+
54
+ declare module '@theme/Playground/Container' {
55
+ import type {ReactNode} from 'react';
56
+
57
+ export interface Props {
58
+ children: ReactNode;
59
+ }
60
+
61
+ export default function PlaygroundContainer(props: Props): ReactNode;
62
+ }
63
+
64
+ declare module '@theme/Playground/Layout' {
65
+ import type {ReactNode} from 'react';
66
+
67
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
68
+ export interface Props {}
69
+
70
+ export default function PlaygroundLayout(props: Props): ReactNode;
71
+ }
72
+
73
+ declare module '@theme/Playground/Preview' {
74
+ import type {ReactNode} from 'react';
75
+
76
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
77
+ export interface Props {}
78
+
79
+ export default function PlaygroundPreview(props: Props): ReactNode;
80
+ }
81
+
82
+ declare module '@theme/Playground/Editor' {
83
+ import type {ReactNode} from 'react';
84
+
85
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
86
+ export interface Props {}
87
+
88
+ export default function PlaygroundEditor(props: Props): ReactNode;
89
+ }
90
+
91
+ declare module '@theme/Playground/Header' {
92
+ import type {ReactNode} from 'react';
93
+
94
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
95
+ export interface Props {}
96
+
97
+ export default function PlaygroundHeader(props: Props): ReactNode;
98
+ }
99
+
34
100
  declare module '@theme/ReactLiveScope' {
35
101
  type Scope = {
36
102
  [key: string]: unknown;
@@ -1,44 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- .playgroundContainer {
9
- margin-bottom: var(--ifm-leading);
10
- border-radius: var(--ifm-global-radius);
11
- box-shadow: var(--ifm-global-shadow-lw);
12
- overflow: hidden;
13
- }
14
-
15
- .playgroundHeader {
16
- letter-spacing: 0.08rem;
17
- padding: 0.75rem;
18
- text-transform: uppercase;
19
- font-weight: bold;
20
- background: var(--ifm-color-emphasis-200);
21
- color: var(--ifm-color-content);
22
- font-size: var(--ifm-code-font-size);
23
- }
24
-
25
- .playgroundHeader:first-of-type {
26
- background: var(--ifm-color-emphasis-600);
27
- color: var(--ifm-color-content-inverse);
28
- }
29
-
30
- .playgroundEditor {
31
- font: var(--ifm-code-font-size) / var(--ifm-pre-line-height)
32
- var(--ifm-font-family-monospace) !important;
33
- /* rtl:ignore */
34
- direction: ltr;
35
- }
36
-
37
- .playgroundEditor pre {
38
- border-radius: 0;
39
- }
40
-
41
- .playgroundPreview {
42
- padding: 1rem;
43
- background-color: var(--ifm-pre-background);
44
- }
@@ -1,44 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- .playgroundContainer {
9
- margin-bottom: var(--ifm-leading);
10
- border-radius: var(--ifm-global-radius);
11
- box-shadow: var(--ifm-global-shadow-lw);
12
- overflow: hidden;
13
- }
14
-
15
- .playgroundHeader {
16
- letter-spacing: 0.08rem;
17
- padding: 0.75rem;
18
- text-transform: uppercase;
19
- font-weight: bold;
20
- background: var(--ifm-color-emphasis-200);
21
- color: var(--ifm-color-content);
22
- font-size: var(--ifm-code-font-size);
23
- }
24
-
25
- .playgroundHeader:first-of-type {
26
- background: var(--ifm-color-emphasis-600);
27
- color: var(--ifm-color-content-inverse);
28
- }
29
-
30
- .playgroundEditor {
31
- font: var(--ifm-code-font-size) / var(--ifm-pre-line-height)
32
- var(--ifm-font-family-monospace) !important;
33
- /* rtl:ignore */
34
- direction: ltr;
35
- }
36
-
37
- .playgroundEditor pre {
38
- border-radius: 0;
39
- }
40
-
41
- .playgroundPreview {
42
- padding: 1rem;
43
- background-color: var(--ifm-pre-background);
44
- }
package/src/types.d.ts DELETED
@@ -1,20 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- /// <reference types="@docusaurus/theme-classic" />
9
- /// <reference types="@docusaurus/module-type-aliases" />
10
-
11
- declare module '@theme-init/CodeBlock' {
12
- import type CodeBlock from '@theme/CodeBlock';
13
- import type {Props as BaseProps} from '@theme/CodeBlock';
14
-
15
- export interface Props extends BaseProps {
16
- live?: boolean;
17
- }
18
- const CodeBlockComp: typeof CodeBlock;
19
- export default CodeBlockComp;
20
- }