close

@rspress/plugin-playground updated

Provide a real-time editable Playground to preview components in MDX file code blocks.

Tip

This plugin can be used together with @rspress/plugin-preview. Unlike plugin-preview, plugin-playground compiles code in the browser, so it has more usage limitations (e.g., it does not support importing modules from local files). It is recommended to use plugin-playground as a supplement to plugin-preview for scenarios that require real-time code editing.

Installation

npm
yarn
pnpm
bun
deno
npm add @rspress/plugin-playground -D

Usage

1. Register the plugin

First, write the following config in the config file:

rspress.config.ts
import { 
function defineConfig(config: UserConfig): UserConfig (+1 overload)

Define a static Rspress configuration object.

@paramconfig - The Rspress configuration object.@returnsThe same configuration object (enables type checking and IDE autocompletion).@example
import { defineConfig } from '@rspress/core';

export default defineConfig({
  title: 'My Site',
});
defineConfig
} from '@rspress/core';
import {
function pluginPlayground(options?: Partial<PlaygroundOptions>): RspressPlugin

The plugin is used to preview component.

pluginPlayground
} from '@rspress/plugin-playground';
export default
function defineConfig(config: UserConfig): UserConfig (+1 overload)

Define a static Rspress configuration object.

@paramconfig - The Rspress configuration object.@returnsThe same configuration object (enables type checking and IDE autocompletion).@example
import { defineConfig } from '@rspress/core';

export default defineConfig({
  title: 'My Site',
});
defineConfig
({
UserConfig.plugins?: RspressPlugin[] | undefined

Doc plugins

plugins
: [
function pluginPlayground(options?: Partial<PlaygroundOptions>): RspressPlugin

The plugin is used to preview component.

pluginPlayground
()],
});

2. Use in mdx files

Use the ```tsx playground syntax in mdx files:

example.mdx
```tsx playground
import { useState } from 'react';

function App() {
  const [count, setCount] = useState(0);

  return (
    <div style={{ textAlign: 'center' }}>
      <p>Current count: {count}</p>
      <button onClick={() => setCount(count + 1)}>+</button>
      <button onClick={() => setCount(count - 1)}>-</button>
    </div>
  );
}

export default App;
```

The rendering result is as follows:

Loading...
Tip
  1. Currently only works in .mdx files.
  2. You need to export the component as default, and Rspress will automatically render this component.
  3. If you use tsx, type checking is currently not performed.

3. Write component code in other files (optional)

In addition to writing component code in the code block of the mdx file, you can also use it with File Code Block to write the example code in other files.

example.mdx
```tsx file="./_playgroundDemo.jsx" playground

```
Loading...

The rendering result is as follows:

Loading...

Adjusting layout direction

You can use the direction parameter to specify the layout direction of the editor and preview area. It supports horizontal or vertical.

direction="horizontal"

Horizontal layout is the default mode, with the editor and preview area arranged side by side.

Syntax:

example.mdx
```tsx playground direction=horizontal

```

direction="vertical"

Vertical layout mode, with the editor and preview area arranged top to bottom.

Syntax:

example.mdx
```tsx playground direction=vertical

```

Rendering result:

Loading...

Define the layout of the entire page

You can write playgroundDirection in frontmatter to define the layout of the editor and preview area for the entire page.

example.mdx
---
title: Title
playgroundDirection: vertical
---

Priority: Defined directly on the code block > Page frontmatter definition > Plugin configuration.

Options

This plugin accepts a configuration object with the following type definition:

interface PlaygroundOptions {
  defaultRenderMode?: 'pure' | 'playground';
  defaultDirection?: 'horizontal' | 'vertical';
  editorPosition?: 'left' | 'right';
  babelUrl?: string;
  monacoLoader?: Parameters<typeof loader.config>[0];
  monacoOptions?: MonacoEditorProps['options'];
  include?: Array<string | [string, string]>;
  render?: string;
}

defaultRenderMode

  • Type: 'pure' | 'playground'
  • Default: 'pure'

Configures the default rendering behavior for code blocks that don't explicitly declare pure or playground.

  • ```tsx pure: Render as a regular code block
  • ```tsx: Render based on defaultRenderMode configuration
  • ```tsx playground: Render as an editable Playground component
Warning

It is not recommended to modify the default value, as it may affect the combined usage with @rspress/plugin-preview.

defaultDirection

  • Type: 'horizontal' | 'vertical'
  • Default: 'horizontal'

Configures the default layout direction of the editor and preview area.

editorPosition

  • Type: 'left' | 'right'
  • Default: 'left'

Configures the position of the editor in horizontal layout (left/right).

babelUrl

  • Type: string
  • Default: 'https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.22.20/babel.min.js'

Playground uses @babel/standalone to compile demo code. You can modify it to a URL provided by other CDNs, such as unpkg, jsdelivr, etc.

monacoLoader

Configures monaco-loader behaviors. Loaded from cdnjs.com by default.

You can modify it to a URL provided by other CDNs, such as unpkg, jsdelivr, etc.

The full documentation can be found at suren-atoyan/monaco-loader

monacoOptions

Configures Monaco Editor options.

Note

monacoLoader and monacoOptions will be serialized to JSON, so some data types, such as functions and circularly referenced objects, are not supported.

include

  • Type: Array<string | [string, string]>

By default, this plugin will automatically scan all import statements in demos; packages not used in demos cannot be used in the Playground. If you want to add other packages to the Playground, you can use the include parameter:

pluginPlayground({
  include: [
    // Add dayjs package
    'dayjs',
    // Add a package named "my-package", actually pointing to "/path/to/package/index.js"
    ['my-package', '/path/to/package/index.js'],
  ],
});

render

  • Type: string

You can customize the render file for rendering Playground. Please note that the file name must be Playground.(jsx?|tsx?).

pluginPlayground({
  render: '/path/to/render/Playground.tsx',
});

In the custom Playground, you can directly import the original editor and renderer, and import pre-packaged dependencies through _rspress_playground_imports:

import getImport from '_rspress_playground_imports';
import { Runner, Editor } from '@rspress/plugin-playground/web';

You can refer to the built-in Playground.tsx for customization.