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

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
package/README.md CHANGED
@@ -1,3 +1,45 @@
1
- # Temporary Holding Version
1
+ # `@docusaurus/theme-live-codeblock`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ You can create live code editors with a code block `live` meta string.
4
+
5
+ Install
6
+
7
+ ```bash
8
+ npm i @docusaurus/theme-live-codeblock # or yarn add @docusaurus/theme-live-codeblock
9
+ ```
10
+
11
+ Modify your `docusaurus.config.js`
12
+
13
+ ```diff
14
+ module.exports = {
15
+ ...
16
+ + themes: ['@docusaurus/theme-live-codeblock'],
17
+ presets: ['@docusaurus/preset-classic']
18
+ ...
19
+ }
20
+ ```
21
+
22
+ Example:
23
+
24
+ ```jsx live
25
+ function Clock(props) {
26
+ const [date, setDate] = useState(new Date());
27
+ useEffect(() => {
28
+ var timerID = setInterval(() => tick(), 1000);
29
+
30
+ return function cleanup() {
31
+ clearInterval(timerID);
32
+ };
33
+ });
34
+
35
+ function tick() {
36
+ setDate(new Date());
37
+ }
38
+
39
+ return (
40
+ <div>
41
+ <h2>It is {date.toLocaleTimeString()}.</h2>
42
+ </div>
43
+ );
44
+ }
45
+ ```
@@ -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
+ import { type ReactNode } from 'react';
8
+ type PlaygroundContextValue = {
9
+ reset: () => void;
10
+ };
11
+ export declare function PlaygroundProvider({ value, children, }: {
12
+ value: PlaygroundContextValue;
13
+ children: ReactNode;
14
+ }): ReactNode;
15
+ export declare function usePlayground(): PlaygroundContextValue;
16
+ export {};
@@ -0,0 +1,18 @@
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 { createContext, useContext } from 'react';
8
+ const PlaygroundContext = createContext(null);
9
+ export function PlaygroundProvider({ value, children, }) {
10
+ return <PlaygroundContext value={value}>{children}</PlaygroundContext>;
11
+ }
12
+ export function usePlayground() {
13
+ const context = useContext(PlaygroundContext);
14
+ if (!context) {
15
+ throw new Error('usePlayground must be used within PlaygroundProvider');
16
+ }
17
+ return context;
18
+ }
@@ -0,0 +1,7 @@
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
+ export { usePlayground, PlaygroundProvider } from './context';
@@ -0,0 +1,7 @@
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
+ export { usePlayground, PlaygroundProvider } from './context';
package/lib/index.d.ts ADDED
@@ -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 { LoadContext, Plugin } from '@docusaurus/types';
8
+ export default function themeLiveCodeblock(context: LoadContext): Plugin;
9
+ export { validateThemeConfig } from './validateThemeConfig';
package/lib/index.js ADDED
@@ -0,0 +1,31 @@
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.validateThemeConfig = void 0;
10
+ exports.default = themeLiveCodeblock;
11
+ const theme_translations_1 = require("@docusaurus/theme-translations");
12
+ function themeLiveCodeblock(context) {
13
+ const { i18n: { currentLocale }, } = context;
14
+ return {
15
+ name: 'docusaurus-theme-live-codeblock',
16
+ getThemePath() {
17
+ return '../lib/theme';
18
+ },
19
+ getTypeScriptThemePath() {
20
+ return '../src/theme';
21
+ },
22
+ getDefaultCodeTranslationMessages() {
23
+ return (0, theme_translations_1.readDefaultCodeTranslationMessages)({
24
+ locale: currentLocale,
25
+ name: 'theme-live-codeblock',
26
+ });
27
+ },
28
+ };
29
+ }
30
+ var validateThemeConfig_1 = require("./validateThemeConfig");
31
+ Object.defineProperty(exports, "validateThemeConfig", { enumerable: true, get: function () { return validateThemeConfig_1.validateThemeConfig; } });
@@ -0,0 +1,14 @@
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 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;
@@ -0,0 +1,22 @@
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 OriginalCodeBlock from '@theme-init/CodeBlock';
8
+ import LiveCodeBlock from '@theme/LiveCodeBlock';
9
+ function isLiveCodeBlock(props) {
10
+ return (
11
+ !!props.live &&
12
+ (typeof props.children === 'undefined' ||
13
+ typeof props.children === 'string')
14
+ );
15
+ }
16
+ export default function CodeBlockEnhancer(props) {
17
+ return isLiveCodeBlock(props) ? (
18
+ <LiveCodeBlock {...props} />
19
+ ) : (
20
+ <OriginalCodeBlock {...props} />
21
+ );
22
+ }
@@ -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,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 Playground from '@theme/Playground';
8
+ import ReactLiveScope from '@theme/ReactLiveScope';
9
+ export default function LiveCodeBlock(props) {
10
+ return <Playground scope={ReactLiveScope} {...props} />;
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/Buttons/ResetButton';
9
+ export default function ResetButton({ className }: Props): ReactNode;
@@ -0,0 +1,45 @@
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 clsx from 'clsx';
8
+ import Translate from '@docusaurus/Translate';
9
+ import {usePlayground} from '@docusaurus/theme-live-codeblock/client';
10
+ import styles from './styles.module.css';
11
+ function Icon() {
12
+ return (
13
+ <svg
14
+ className={styles.resetButtonIcon}
15
+ viewBox="0 0 16 16"
16
+ fill="currentColor"
17
+ aria-hidden="true">
18
+ <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" />
19
+ <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" />
20
+ </svg>
21
+ );
22
+ }
23
+ function Label() {
24
+ return (
25
+ <Translate
26
+ id="theme.Playground.buttons.reset"
27
+ description="The reset button label for live code blocks">
28
+ Reset
29
+ </Translate>
30
+ );
31
+ }
32
+ export default function ResetButton({className}) {
33
+ const {reset} = usePlayground();
34
+ return (
35
+ <button
36
+ type="button"
37
+ aria-label="Reset code to original"
38
+ title="Reset"
39
+ className={clsx('clean-btn', className, styles.resetButton)}
40
+ onClick={() => reset()}>
41
+ <Icon />
42
+ <Label />
43
+ </button>
44
+ );
45
+ }
@@ -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,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,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 styles from './styles.module.css';
8
+ export default function PlaygroundContainer({children}) {
9
+ return <div className={styles.playgroundContainer}>{children}</div>;
10
+ }
@@ -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 {LiveEditor} from 'react-live';
8
+ import useIsBrowser from '@docusaurus/useIsBrowser';
9
+ import Translate from '@docusaurus/Translate';
10
+ import PlaygroundHeader from '@theme/Playground/Header';
11
+ import ResetButton from '@theme/Playground/Buttons/ResetButton';
12
+ import styles from './styles.module.css';
13
+ export default function PlaygroundEditor() {
14
+ const isBrowser = useIsBrowser();
15
+ return (
16
+ <>
17
+ <PlaygroundHeader buttons={<ResetButton />}>
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,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/Header';
9
+ export default function PlaygroundHeader({ children, buttons, }: Props): ReactNode;
@@ -0,0 +1,18 @@
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 clsx from 'clsx';
8
+ import styles from './styles.module.css';
9
+ export default function PlaygroundHeader({children, buttons}) {
10
+ return (
11
+ <div className={clsx(styles.playgroundHeader)}>
12
+ <div className={styles.playgroundHeaderContent}>{children}</div>
13
+ {buttons && (
14
+ <div className={styles.playgroundHeaderButtons}>{buttons}</div>
15
+ )}
16
+ </div>
17
+ );
18
+ }
@@ -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,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/Layout';
9
+ export default function PlaygroundLayout(props: Props): 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 {useThemeConfig} from '@docusaurus/theme-common';
8
+ import PlaygroundPreview from '@theme/Playground/Preview';
9
+ import PlaygroundEditor from '@theme/Playground/Editor';
10
+ function useLiveCodeBlockThemeConfig() {
11
+ const themeConfig = useThemeConfig();
12
+ return themeConfig.liveCodeBlock;
13
+ }
14
+ export default function PlaygroundLayout(props) {
15
+ const themeConfig = useLiveCodeBlockThemeConfig();
16
+ const position = props.position ?? themeConfig.playgroundPosition;
17
+ return (
18
+ <>
19
+ {position === '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,53 @@
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 {LiveError, LivePreview} from 'react-live';
8
+ import BrowserOnly from '@docusaurus/BrowserOnly';
9
+ import {ErrorBoundaryErrorMessageFallback} from '@docusaurus/theme-common';
10
+ import ErrorBoundary from '@docusaurus/ErrorBoundary';
11
+ import Translate from '@docusaurus/Translate';
12
+ import PlaygroundHeader from '@theme/Playground/Header';
13
+ import styles from './styles.module.css';
14
+ function Loader() {
15
+ // Is it worth improving/translating?
16
+ // eslint-disable-next-line @docusaurus/no-untranslated-text
17
+ return <div>Loading...</div>;
18
+ }
19
+ function PlaygroundLivePreview() {
20
+ // No SSR for the live preview
21
+ // See https://github.com/facebook/docusaurus/issues/5747
22
+ return (
23
+ <BrowserOnly fallback={<Loader />}>
24
+ {() => (
25
+ <>
26
+ <ErrorBoundary
27
+ fallback={(params) => (
28
+ <ErrorBoundaryErrorMessageFallback {...params} />
29
+ )}>
30
+ <LivePreview />
31
+ </ErrorBoundary>
32
+ <LiveError />
33
+ </>
34
+ )}
35
+ </BrowserOnly>
36
+ );
37
+ }
38
+ export default function PlaygroundPreview() {
39
+ return (
40
+ <>
41
+ <PlaygroundHeader>
42
+ <Translate
43
+ id="theme.Playground.result"
44
+ description="The result label of the live codeblocks">
45
+ Result
46
+ </Translate>
47
+ </PlaygroundHeader>
48
+ <div className={styles.playgroundPreview}>
49
+ <PlaygroundLivePreview />
50
+ </div>
51
+ </>
52
+ );
53
+ }
@@ -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(props: Props): ReactNode;
@@ -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
+ import {useCallback, useMemo, useState} from 'react';
8
+ import {LiveProvider} from 'react-live';
9
+ import {PlaygroundProvider as PlaygroundProviderComponent} from '@docusaurus/theme-live-codeblock/client';
10
+ import {usePrismTheme} from '@docusaurus/theme-common';
11
+ // this should rather be a stable function
12
+ // see https://github.com/facebook/docusaurus/issues/9630#issuecomment-1855682643
13
+ const DEFAULT_TRANSFORM_CODE = (code) => `${code};`;
14
+ function LiveProviderComponent({code, children, ...props}) {
15
+ const prismTheme = usePrismTheme();
16
+ const noInline = props.metastring?.includes('noInline') ?? false;
17
+ return (
18
+ <LiveProvider
19
+ noInline={noInline}
20
+ theme={prismTheme}
21
+ {...props}
22
+ code={code?.replace(/\n$/, '')}
23
+ transformCode={props.transformCode ?? DEFAULT_TRANSFORM_CODE}>
24
+ {children}
25
+ </LiveProvider>
26
+ );
27
+ }
28
+ export default function PlaygroundProvider(props) {
29
+ const [resetKey, setResetKey] = useState(0);
30
+ const reset = useCallback(() => setResetKey((prev) => prev + 1), []);
31
+ const value = useMemo(() => ({reset}), [reset]);
32
+ return (
33
+ <PlaygroundProviderComponent key={resetKey} value={value}>
34
+ <LiveProviderComponent {...props} />
35
+ </PlaygroundProviderComponent>
36
+ );
37
+ }
@@ -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';
9
+ export default function Playground({ children, transformCode, position, ...props }: Props): ReactNode;
@@ -0,0 +1,23 @@
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 PlaygroundProvider from '@theme/Playground/Provider';
8
+ import PlaygroundContainer from '@theme/Playground/Container';
9
+ import PlaygroundLayout from '@theme/Playground/Layout';
10
+ export default function Playground({
11
+ children,
12
+ transformCode,
13
+ position,
14
+ ...props
15
+ }) {
16
+ return (
17
+ <PlaygroundContainer>
18
+ <PlaygroundProvider code={children} {...props}>
19
+ <PlaygroundLayout position={position} />
20
+ </PlaygroundProvider>
21
+ </PlaygroundContainer>
22
+ );
23
+ }