docusaurus/packages/docusaurus-theme-live-codeblock
Joshua Chen e5bf59fd9b
refactor: mark a few client-side packages as side-effect-free (#7085)
* refactor: mark a few client-side packages as side-effect-free

* fix

* fix again

* fix...

* revert sideeffect

* revert

* fix again...

* properly fix

* fix

* properly fix
2022-04-09 09:08:18 +08:00
..
src chore: disable string escaping in snapshots (#7131) 2022-04-08 11:23:19 +08:00
.npmignore misc: convert all internal scripts to ESM (#6286) 2022-01-08 12:59:28 +08:00
copyUntypedFiles.mjs misc: convert all internal scripts to ESM (#6286) 2022-01-08 12:59:28 +08:00
package.json refactor: mark a few client-side packages as side-effect-free (#7085) 2022-04-09 09:08:18 +08:00
README.md docs(v2): Add READMEs to v2 packages (#4034) 2021-01-14 17:16:26 +01:00
tsconfig.client.json refactor(live-codeblock): migrate theme to TS (#6583) 2022-02-02 17:48:06 +08:00
tsconfig.json refactor(live-codeblock): migrate package to TS (#5851) 2021-11-02 12:28:41 +08:00
tsconfig.server.json refactor(live-codeblock): migrate theme to TS (#6583) 2022-02-02 17:48:06 +08:00

Docusaurus Live Codeblock

You can create live code editors with a code block live meta string.

Install

npm i @docusaurus/theme-live-codeblock # or yarn add @docusaurus/theme-live-codeblock

Modify your docusaurus.config.js

module.exports = {
  ...
+ themes: ['@docusaurus/theme-live-codeblock'],
  presets: ['@docusaurus/preset-classic']
  ...
}

Example:

```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>
  );
}
```