@azerion/bunosaurus-theme-live-codeblock 0.0.0-stage → 4.0.0

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 (58) hide show
  1. package/README.md +44 -2
  2. package/lib/client/context.d.ts +16 -0
  3. package/lib/client/context.js +18 -0
  4. package/lib/client/index.d.ts +7 -0
  5. package/lib/client/index.js +7 -0
  6. package/lib/index.d.ts +9 -0
  7. package/lib/index.js +31 -0
  8. package/lib/theme/CodeBlock/index.d.ts +14 -0
  9. package/lib/theme/CodeBlock/index.js +22 -0
  10. package/lib/theme/LiveCodeBlock/index.d.ts +9 -0
  11. package/lib/theme/LiveCodeBlock/index.js +11 -0
  12. package/lib/theme/Playground/Buttons/ResetButton/index.d.ts +9 -0
  13. package/lib/theme/Playground/Buttons/ResetButton/index.js +45 -0
  14. package/lib/theme/Playground/Buttons/ResetButton/styles.module.css +36 -0
  15. package/lib/theme/Playground/Container/index.d.ts +9 -0
  16. package/lib/theme/Playground/Container/index.js +10 -0
  17. package/lib/theme/Playground/Container/styles.module.css +13 -0
  18. package/lib/theme/Playground/Editor/index.d.ts +8 -0
  19. package/lib/theme/Playground/Editor/index.js +32 -0
  20. package/lib/theme/Playground/Editor/styles.module.css +17 -0
  21. package/lib/theme/Playground/Header/index.d.ts +9 -0
  22. package/lib/theme/Playground/Header/index.js +18 -0
  23. package/lib/theme/Playground/Header/styles.module.css +29 -0
  24. package/lib/theme/Playground/Layout/index.d.ts +9 -0
  25. package/lib/theme/Playground/Layout/index.js +32 -0
  26. package/lib/theme/Playground/Preview/index.d.ts +8 -0
  27. package/lib/theme/Playground/Preview/index.js +53 -0
  28. package/lib/theme/Playground/Preview/styles.module.css +11 -0
  29. package/lib/theme/Playground/Provider/index.d.ts +9 -0
  30. package/lib/theme/Playground/Provider/index.js +37 -0
  31. package/lib/theme/Playground/index.d.ts +9 -0
  32. package/lib/theme/Playground/index.js +23 -0
  33. package/lib/theme/ReactLiveScope/index.d.ts +8 -0
  34. package/lib/theme/ReactLiveScope/index.js +13 -0
  35. package/lib/validateThemeConfig.d.ts +13 -0
  36. package/lib/validateThemeConfig.js +26 -0
  37. package/package.json +56 -4
  38. package/src/client/context.tsx +32 -0
  39. package/src/client/index.ts +8 -0
  40. package/src/index.ts +35 -0
  41. package/src/theme/CodeBlock/index.tsx +38 -0
  42. package/src/theme/LiveCodeBlock/index.tsx +15 -0
  43. package/src/theme/Playground/Buttons/ResetButton/index.tsx +51 -0
  44. package/src/theme/Playground/Buttons/ResetButton/styles.module.css +36 -0
  45. package/src/theme/Playground/Container/index.tsx +16 -0
  46. package/src/theme/Playground/Container/styles.module.css +13 -0
  47. package/src/theme/Playground/Editor/index.tsx +35 -0
  48. package/src/theme/Playground/Editor/styles.module.css +17 -0
  49. package/src/theme/Playground/Header/index.tsx +26 -0
  50. package/src/theme/Playground/Header/styles.module.css +29 -0
  51. package/src/theme/Playground/Layout/index.tsx +39 -0
  52. package/src/theme/Playground/Preview/index.tsx +59 -0
  53. package/src/theme/Playground/Preview/styles.module.css +11 -0
  54. package/src/theme/Playground/Provider/index.tsx +43 -0
  55. package/src/theme/Playground/index.tsx +28 -0
  56. package/src/theme/ReactLiveScope/index.tsx +16 -0
  57. package/src/theme-live-codeblock.d.ts +142 -0
  58. package/src/validateThemeConfig.ts +33 -0
@@ -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
+ declare const ReactLiveScope: unknown;
8
+ export default ReactLiveScope;
@@ -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
+ import React from 'react';
8
+ // Add react-live imports you need here
9
+ const ReactLiveScope = {
10
+ React,
11
+ ...React,
12
+ };
13
+ export default ReactLiveScope;
@@ -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
+ import { Joi } from '@docusaurus/utils-validation';
8
+ import type { ThemeConfig, ThemeConfigValidationContext } from '@docusaurus/types';
9
+ export declare const DEFAULT_CONFIG: {
10
+ playgroundPosition: string;
11
+ };
12
+ export declare const Schema: Joi.ObjectSchema<ThemeConfig>;
13
+ export declare function validateThemeConfig({ validate, themeConfig, }: ThemeConfigValidationContext<ThemeConfig>): ThemeConfig;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Schema = exports.DEFAULT_CONFIG = void 0;
10
+ exports.validateThemeConfig = validateThemeConfig;
11
+ const utils_validation_1 = require("@docusaurus/utils-validation");
12
+ exports.DEFAULT_CONFIG = {
13
+ playgroundPosition: 'bottom',
14
+ };
15
+ exports.Schema = utils_validation_1.Joi.object({
16
+ liveCodeBlock: utils_validation_1.Joi.object({
17
+ playgroundPosition: utils_validation_1.Joi.string()
18
+ .equal('top', 'bottom')
19
+ .default(exports.DEFAULT_CONFIG.playgroundPosition),
20
+ })
21
+ .label('themeConfig.liveCodeBlock')
22
+ .default(exports.DEFAULT_CONFIG),
23
+ });
24
+ function validateThemeConfig({ validate, themeConfig, }) {
25
+ return validate(exports.Schema, themeConfig);
26
+ }
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
2
  "name": "@azerion/bunosaurus-theme-live-codeblock",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "4.0.0",
4
+ "description": "Docusaurus live code block component.",
5
+ "main": "lib/index.js",
6
+ "types": "src/theme-live-codeblock.d.ts",
7
+ "sideEffects": [
8
+ "lib/theme/Playground/*"
9
+ ],
10
+ "exports": {
11
+ "./lib/*": "./lib/*",
12
+ "./src/*": "./src/*",
13
+ "./client": {
14
+ "type": "./lib/client/index.d.ts",
15
+ "default": "./lib/client/index.js"
16
+ },
17
+ ".": {
18
+ "types": "./src/theme-live-codeblock.d.ts",
19
+ "default": "./lib/index.js"
20
+ }
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc --build && copy-untyped-files && oxfmt \"lib/theme/**/*.js\"",
27
+ "watch": "run-p -c copy:watch build:watch",
28
+ "build:watch": "tsc --build --watch",
29
+ "copy:watch": "copy-untyped-files --watch"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/azerion/bunosaurus.git",
34
+ "directory": "packages/docusaurus-theme-live-codeblock"
35
+ },
36
+ "license": "MIT",
37
+ "dependencies": {
38
+ "@docusaurus/core": "npm:@azerion/bunosaurus-core@4.0.0",
39
+ "@docusaurus/theme-classic": "npm:@azerion/bunosaurus-theme-classic@4.0.0",
40
+ "@docusaurus/theme-common": "npm:@azerion/bunosaurus-theme-common@4.0.0",
41
+ "@docusaurus/theme-translations": "npm:@azerion/bunosaurus-theme-translations@4.0.0",
42
+ "@docusaurus/utils-validation": "npm:@azerion/bunosaurus-utils-validation@4.0.0",
43
+ "clsx": "^2.1.1",
44
+ "react-live": "^5.0.0",
45
+ "tslib": "^2.8.1"
46
+ },
47
+ "devDependencies": {
48
+ "@docusaurus/module-type-aliases": "npm:@azerion/bunosaurus-module-type-aliases@4.0.0",
49
+ "@docusaurus/types": "npm:@azerion/bunosaurus-types@4.0.0"
50
+ },
51
+ "peerDependencies": {
52
+ "react": "^19.3.0",
53
+ "react-dom": "^19.3.0"
54
+ },
55
+ "engines": {
56
+ "bun": ">=1.4.1"
57
+ }
58
+ }
@@ -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
+
8
+ import {createContext, useContext, type ReactNode} from 'react';
9
+
10
+ type PlaygroundContextValue = {
11
+ reset: () => void;
12
+ };
13
+
14
+ const PlaygroundContext = createContext<PlaygroundContextValue | null>(null);
15
+
16
+ export function PlaygroundProvider({
17
+ value,
18
+ children,
19
+ }: {
20
+ value: PlaygroundContextValue;
21
+ children: ReactNode;
22
+ }): ReactNode {
23
+ return <PlaygroundContext value={value}>{children}</PlaygroundContext>;
24
+ }
25
+
26
+ export function usePlayground(): PlaygroundContextValue {
27
+ const context = useContext(PlaygroundContext);
28
+ if (!context) {
29
+ throw new Error('usePlayground must be used within PlaygroundProvider');
30
+ }
31
+ return context;
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
+
8
+ export {usePlayground, PlaygroundProvider} from './context';
package/src/index.ts ADDED
@@ -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 {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
9
+ import type {LoadContext, Plugin} from '@docusaurus/types';
10
+
11
+ export default function themeLiveCodeblock(context: LoadContext): Plugin {
12
+ const {
13
+ i18n: {currentLocale},
14
+ } = context;
15
+
16
+ return {
17
+ name: 'docusaurus-theme-live-codeblock',
18
+
19
+ getThemePath() {
20
+ return '../lib/theme';
21
+ },
22
+ getTypeScriptThemePath() {
23
+ return '../src/theme';
24
+ },
25
+
26
+ getDefaultCodeTranslationMessages() {
27
+ return readDefaultCodeTranslationMessages({
28
+ locale: currentLocale,
29
+ name: 'theme-live-codeblock',
30
+ });
31
+ },
32
+ };
33
+ }
34
+
35
+ export {validateThemeConfig} from './validateThemeConfig';
@@ -0,0 +1,38 @@
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 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
+
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;
19
+ }
20
+ }
21
+
22
+ function isLiveCodeBlock(
23
+ props: CodeBlockProps,
24
+ ): props is {live: true; children: string | undefined} {
25
+ return (
26
+ !!props.live &&
27
+ (typeof props.children === 'undefined' ||
28
+ typeof props.children === 'string')
29
+ );
30
+ }
31
+
32
+ export default function CodeBlockEnhancer(props: CodeBlockProps): ReactNode {
33
+ return isLiveCodeBlock(props) ? (
34
+ <LiveCodeBlock {...props} />
35
+ ) : (
36
+ <OriginalCodeBlock {...props} />
37
+ );
38
+ }
@@ -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 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,51 @@
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 type {ReactNode} from 'react';
9
+ import clsx from 'clsx';
10
+ import Translate from '@docusaurus/Translate';
11
+ import {usePlayground} from '@docusaurus/theme-live-codeblock/client';
12
+ import type {Props} from '@theme/Playground/Buttons/ResetButton';
13
+ import styles from './styles.module.css';
14
+
15
+ function Icon() {
16
+ return (
17
+ <svg
18
+ className={styles.resetButtonIcon}
19
+ viewBox="0 0 16 16"
20
+ fill="currentColor"
21
+ aria-hidden="true">
22
+ <path d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z" />
23
+ <path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z" />
24
+ </svg>
25
+ );
26
+ }
27
+
28
+ function Label() {
29
+ return (
30
+ <Translate
31
+ id="theme.Playground.buttons.reset"
32
+ description="The reset button label for live code blocks">
33
+ Reset
34
+ </Translate>
35
+ );
36
+ }
37
+
38
+ export default function ResetButton({className}: Props): ReactNode {
39
+ const {reset} = usePlayground();
40
+ return (
41
+ <button
42
+ type="button"
43
+ aria-label="Reset code to original"
44
+ title="Reset"
45
+ className={clsx('clean-btn', className, styles.resetButton)}
46
+ onClick={() => reset()}>
47
+ <Icon />
48
+ <Label />
49
+ </button>
50
+ );
51
+ }
@@ -0,0 +1,36 @@
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
+ .resetButton {
9
+ display: inline-flex;
10
+ align-items: center;
11
+ gap: 0.25rem;
12
+ padding: 0.25rem 0.5rem;
13
+ font-size: 0.875rem;
14
+ line-height: 1.5;
15
+ border: 1px solid var(--ifm-color-emphasis-300);
16
+ border-radius: var(--ifm-global-radius);
17
+ background: var(--ifm-button-background-color);
18
+ color: var(--ifm-font-color-base);
19
+ cursor: pointer;
20
+ transition: all var(--ifm-transition-fast);
21
+ }
22
+
23
+ .resetButton:hover {
24
+ background: var(--ifm-color-emphasis-200);
25
+ border-color: var(--ifm-color-emphasis-400);
26
+ }
27
+
28
+ .resetButton:active {
29
+ background: var(--ifm-color-emphasis-300);
30
+ }
31
+
32
+ .resetButtonIcon {
33
+ width: 1rem;
34
+ height: 1rem;
35
+ flex-shrink: 0;
36
+ }
@@ -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 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 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
+ import ResetButton from '@theme/Playground/Buttons/ResetButton';
14
+ import styles from './styles.module.css';
15
+
16
+ export default function PlaygroundEditor(): ReactNode {
17
+ const isBrowser = useIsBrowser();
18
+ return (
19
+ <>
20
+ <PlaygroundHeader buttons={<ResetButton />}>
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,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
+
8
+ import type {ReactNode} from 'react';
9
+ import clsx from 'clsx';
10
+ import type {Props} from '@theme/Playground/Header';
11
+
12
+ import styles from './styles.module.css';
13
+
14
+ export default function PlaygroundHeader({
15
+ children,
16
+ buttons,
17
+ }: Props): ReactNode {
18
+ return (
19
+ <div className={clsx(styles.playgroundHeader)}>
20
+ <div className={styles.playgroundHeaderContent}>{children}</div>
21
+ {buttons && (
22
+ <div className={styles.playgroundHeaderButtons}>{buttons}</div>
23
+ )}
24
+ </div>
25
+ );
26
+ }
@@ -0,0 +1,29 @@
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
+ display: flex;
10
+ justify-content: space-between;
11
+ align-items: center;
12
+ letter-spacing: 0.08rem;
13
+ padding: 0.75rem;
14
+ text-transform: uppercase;
15
+ font-weight: bold;
16
+ background: var(--ifm-color-emphasis-200);
17
+ color: var(--ifm-color-content);
18
+ font-size: var(--ifm-code-font-size);
19
+ }
20
+
21
+ .playgroundHeaderContent {
22
+ font-weight: var(--ifm-font-weight-bold);
23
+ font-size: 0.875rem;
24
+ }
25
+
26
+ .playgroundHeaderButtons {
27
+ display: flex;
28
+ gap: 0.5rem;
29
+ }
@@ -0,0 +1,39 @@
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 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
+ import type {Props} from '@theme/Playground/Layout';
13
+
14
+ import type {ThemeConfig} from '@docusaurus/theme-live-codeblock';
15
+
16
+ function useLiveCodeBlockThemeConfig() {
17
+ const themeConfig = useThemeConfig() as unknown as ThemeConfig;
18
+ return themeConfig.liveCodeBlock;
19
+ }
20
+
21
+ export default function PlaygroundLayout(props: Props): ReactNode {
22
+ const themeConfig = useLiveCodeBlockThemeConfig();
23
+ const position = props.position ?? themeConfig.playgroundPosition;
24
+ return (
25
+ <>
26
+ {position === 'top' ? (
27
+ <>
28
+ <PlaygroundPreview />
29
+ <PlaygroundEditor />
30
+ </>
31
+ ) : (
32
+ <>
33
+ <PlaygroundEditor />
34
+ <PlaygroundPreview />
35
+ </>
36
+ )}
37
+ </>
38
+ );
39
+ }
@@ -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 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,43 @@
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 {type ReactNode, useCallback, useMemo, useState} from 'react';
9
+ import {LiveProvider} from 'react-live';
10
+ import {PlaygroundProvider as PlaygroundProviderComponent} from '@docusaurus/theme-live-codeblock/client';
11
+ import {usePrismTheme} from '@docusaurus/theme-common';
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
+ function LiveProviderComponent({code, children, ...props}: Props): ReactNode {
19
+ const prismTheme = usePrismTheme();
20
+ const noInline = props.metastring?.includes('noInline') ?? false;
21
+ return (
22
+ <LiveProvider
23
+ noInline={noInline}
24
+ theme={prismTheme}
25
+ {...props}
26
+ code={code?.replace(/\n$/, '')}
27
+ transformCode={props.transformCode ?? DEFAULT_TRANSFORM_CODE}>
28
+ {children}
29
+ </LiveProvider>
30
+ );
31
+ }
32
+
33
+ export default function PlaygroundProvider(props: Props): ReactNode {
34
+ const [resetKey, setResetKey] = useState(0);
35
+ const reset = useCallback(() => setResetKey((prev) => prev + 1), []);
36
+ const value = useMemo(() => ({reset}), [reset]);
37
+
38
+ return (
39
+ <PlaygroundProviderComponent key={resetKey} value={value}>
40
+ <LiveProviderComponent {...props} />
41
+ </PlaygroundProviderComponent>
42
+ );
43
+ }