@rspress/plugin-playground
Provides a live editable playground for previewing components in MDX code blocks.
Use this plugin alongside @rspress/plugin-preview. Unlike plugin-preview, plugin-playground compiles code in the browser, so it has more limitations. For example, it cannot import modules from local files. Use plugin-playground as a supplement to plugin-preview when live code editing is required.
Installation
Usage
1. Register the plugin
First, write the following config in the config file:
2. Use in MDX files
Use the ```tsx playground syntax in MDX files:
It renders as follows:
- It currently works only in
.mdxfiles. - Export the component as default; Rspress renders it automatically.
- Type checking is currently not performed for TSX.
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.
It renders as follows:
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:
direction="vertical"
Vertical layout mode, with the editor and preview area arranged top to bottom.
Syntax:
Rendering result:
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.
Priority: Defined directly on the code block > Page frontmatter definition > Plugin configuration.
Options
This plugin accepts a configuration object with the following type definition:
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 ondefaultRenderModeconfiguration```tsx playground: Render as an editable Playground component
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.
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:
render
- Type:
string
You can customize the render file for rendering Playground. Please note that the file name must be Playground.(jsx?|tsx?).
In the custom Playground, you can directly import the original editor and renderer, and import pre-packaged dependencies through _rspress_playground_imports:
You can refer to the built-in Playground.tsx for customization.