Live CodeBlock Theme
Install
First:
- npm
- Yarn
npm install --save @docusaurus/theme-live-codeblock
yarn add @docusaurus/theme-live-codeblock
Code above will cause error when npm run start
:
Bad docusaurus-theme-live-codeblock version 2.0.0-alpha.70.
All official @docusaurus/* packages should have the exact same version as @docusaurus/core (2.0.0-alpha.69).
Maybe you want to check, or regenerate your yarn.lock or package-lock.json file?
then, first uninstall previous:
npm uninstall @docusaurus/theme-live-codeblock
then install this:
npm install --save @docusaurus/theme-live-codeblock@next
It didn't work for me! I have updated Docusaurus! And it helped.
Then, add to docusaurus.config.js
module.exports = {
// ...
themes: ['@docusaurus/theme-live-codeblock'],
// ...
};
To use the plugin, create a code block with live attached to the language meta string.
```jsx live
function Clock(props) {
const [date, setDate] = useState(new Date());
useEffect(() => {
var timerID = setInterval(() => tick(), 1000);
return function cleanup() {
clearInterval(timerID);
};
});
function tick() {
setDate(new Date());
}
return (
<div>
<h2>It is {date.toLocaleTimeString()}.</h2>
</div>
);
}
```
The code block will be rendered as an interactive editor. Changes to the code will reflect on the result panel live.
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:
- npm
- Yarn
npm run swizzle @docusaurus/theme-live-codeblock ReactLiveScope
yarn run swizzle @docusaurus/theme-live-codeblock ReactLiveScope
If code above doesn't work, try this:
npm run swizzle @docusaurus/theme-live-codeblock ReactLiveScope -- --danger
As a workaround you can just copy the folder
node_modules/@docusaurus/theme-live-codeblock/src/theme/CodeBlock
and put it inwebsite/src/theme/CodeBlock
, that's basically what the command does. - Slorber, Main of Docusaurus
After running the command above, you should have this file below, but almost empty, and you need to copy/past this content into it:
import React from 'react';
const ButtonExample = (props) => (
<button
{...props}
style={{
border: 'solid red',
borderRadius: 20,
padding: 10,
cursor: 'pointer',
...props.style,
}}
/>
);
// Add react-live imports you need here
const ReactLiveScope = {
React,
...React,
ButtonExample,
};
export default ReactLiveScope;
Or (as in Docusaurus page) the content is split into two files. First file:
/**
* 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';
import * as components from './components';
// Add react-live imports you need here
const ReactLiveScope = {
React,
...React,
...components,
};
export default ReactLiveScope;
and second file:
import React from 'react';
export const ButtonExample = (props) => (
<button
{...props}
style={{
border: 'solid red',
borderRadius: 20,
padding: 10,
cursor: 'pointer',
...props.style,
}}
/>
);
The ButtonExample
component is now available to use:
Examples
See first page: https://reactjs.org/