From 76df524d85d5823278dc6780db6a7cf25335878c Mon Sep 17 00:00:00 2001 From: slorber Date: Thu, 28 May 2020 15:01:51 +0200 Subject: [PATCH 1/5] feat(v2): add components to react-live scope (#2807) --- .../package.json | 1 + .../src/theme/CodeBlock/index.js | 17 ++++++- .../src/theme/ReactLiveScope/index.js | 16 +++++++ website/docs/markdown-features.mdx | 47 +++++++++++++++++++ .../src/theme/ReactLiveScope/components.js | 15 ++++++ website/src/theme/ReactLiveScope/index.js | 18 +++++++ 6 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 packages/docusaurus-theme-live-codeblock/src/theme/ReactLiveScope/index.js create mode 100644 website/src/theme/ReactLiveScope/components.js create mode 100644 website/src/theme/ReactLiveScope/index.js diff --git a/packages/docusaurus-theme-live-codeblock/package.json b/packages/docusaurus-theme-live-codeblock/package.json index 9325bbf7f1c8..1c2401c889a7 100644 --- a/packages/docusaurus-theme-live-codeblock/package.json +++ b/packages/docusaurus-theme-live-codeblock/package.json @@ -9,6 +9,7 @@ "license": "MIT", "dependencies": { "@philpl/buble": "^0.19.7", + "@mdx-js/react": "^1.5.8", "classnames": "^2.2.6", "clipboard": "^2.0.6", "parse-numeric-range": "^0.0.2", diff --git a/packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js b/packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js index bb00038a81c3..f1a958eb2d51 100644 --- a/packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js +++ b/packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js @@ -9,18 +9,33 @@ import React from 'react'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import usePrismTheme from '@theme/hooks/usePrismTheme'; import Playground from '@theme/Playground'; +import ReactLiveScope from '@theme/ReactLiveScope'; import CodeBlock from '@theme-init/CodeBlock'; +import {useMDXComponents} from '@mdx-js/react'; + +// Returns components that are available to use by the react-live playground +// It's not possible to import anything in the live playground, +// we need to provide the available imports manually +// See https://github.com/facebook/docusaurus/issues/2807 +const useReactLiveScope = () => { + const mdxComponents = useMDXComponents(); + return { + ...mdxComponents, + ...ReactLiveScope, + }; +}; const withLiveEditor = (Component) => { const WrappedComponent = (props) => { const {isClient} = useDocusaurusContext(); const prismTheme = usePrismTheme(); + const reactLiveScope = useReactLiveScope(); if (props.live) { return ( diff --git a/packages/docusaurus-theme-live-codeblock/src/theme/ReactLiveScope/index.js b/packages/docusaurus-theme-live-codeblock/src/theme/ReactLiveScope/index.js new file mode 100644 index 000000000000..f971d6af1a83 --- /dev/null +++ b/packages/docusaurus-theme-live-codeblock/src/theme/ReactLiveScope/index.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import React from 'react'; + +// Add react-live imports you need here +const ReactLiveScope = { + React, + ...React, +}; + +export default ReactLiveScope; diff --git a/website/docs/markdown-features.mdx b/website/docs/markdown-features.mdx index d4b74d2cafbd..1656ebe63f75 100644 --- a/website/docs/markdown-features.mdx +++ b/website/docs/markdown-features.mdx @@ -735,6 +735,53 @@ function Clock(props) { } ``` +:::caution It is not possible to import components directly from the react-live code editor, you have to define available imports upfront. ::: + +By default, all React imports are available. If you need more imports available, swizzle the react-live scope: + +```bash npm2yarn +npm run swizzle @docusaurus/theme-live-codeblock ReactLiveScope +``` + +```jsx +import React from 'react'; + +const ButtonExample = (props) => ( +