@rspress/plugin-preview
Preview components from code blocks in MDX files. This is useful for component library documentation.
Installation
Usage
1. Install the plugin
First, add the following configuration:
2. Use in mdx files
Use the ```tsx preview syntax in MDX files:
It renders as follows:
Current count: 0
- Currently only works in
.mdxfiles. - Export the component as the default export, and Rspress will render it automatically.
3. Write component code in other files (optional)
Instead of writing component code directly in an MDX code block, you can use it with File Code Block and keep example code in separate files.
It renders as follows:
Current count: 0
Using iframe preview mode
This plugin supports multiple preview modes. You can switch between them by adjusting the preview="..." meta information. For example, you can use ```tsx preview="iframe-follow" to switch to iframe-follow mode.
```tsx preview is equivalent to ```tsx preview="{defaultPreviewMode}", which is determined by the defaultPreviewMode configuration.
Iframe preview mode has separate compilation and runtime environments.
-
Separate compilation environment: example code in the code block is compiled as an entry by a separate Rsbuild instance, allowing Sass/Less variables and other setup to be injected.
-
Separate runtime environment: style conflicts with the documentation site are avoided, and the component library can load its own
base.css.
preview="internal"
"internal" is the default preview mode, where the component is rendered directly within the document.
Syntax:
or
Rendering result:
Current count: 0
preview="iframe-follow"
This mode displays an iframe preview area on the right side of the code block that follows the content flow.
Syntax:
Rendering result:
preview="iframe-fixed"
This mode displays a fixed iframe preview area on the right side of the page, ideal for mobile component library documentation.
Syntax:
Rendering result:

You can inject global scripts or styles into the iframe preview environment using iframeOptions.builderConfig.source.preEntry. Here are some common use cases:
-
Mobile touch event emulation: Emulate mobile touch events on PC by importing @vant/touch-emulator.
-
Dark mode handling: Inject a
MutationObserverto watch forhtml.darkclass changes, then sync tobody.darkor perform other dark mode processing. -
Using Tailwind CSS: Since the iframe preview environment is a separate Rsbuild instance, if your previewed components depend on Tailwind CSS v4, configure
@rsbuild/plugin-tailwindcssiniframeOptions.builderConfig.pluginsand inject your Tailwind CSS entry viapreEntry.
Options
This plugin accepts a configuration object with the following type definition:
defaultRenderMode
- Type:
'pure' | 'preview' - Default:
'pure'
Configures the default rendering behavior for code blocks that don't explicitly declare pure or preview.
It is not recommended to modify the default value, as it may affect the combined usage with @rspress/plugin-playground.
```tsx pure: Render as a regular code block```tsx: Render based ondefaultRenderModeconfiguration```tsx preview: Render as a code block with preview component
defaultPreviewMode
- Type:
'internal' | 'iframe-follow' | 'iframe-fixed' - Default:
'internal'
Configures the default preview mode for ```tsx preview.
```tsx preview: Render based ondefaultPreviewModeconfiguration```tsx preview="internal": Render using internal mode```tsx preview="iframe-follow": Render using follow iframe mode```tsx preview="iframe-fixed": Render using fixed iframe mode
iframeOptions
This plugin starts a separate Rsbuild instance for the iframe mode's dev server and build process, completely isolated from the Rspress documentation compilation.
iframeOptions.devPort
- Type:
number - Default:
7890
Configures the dev server port for iframe preview. If the specified port is already occupied, the plugin will automatically try the next port, up to 20 attempts.
iframeOptions.builderConfig
Configures Rsbuild build options for the iframe, such as adding global styles or scripts.
For example, to add Less or Sass support in previews, install and configure the corresponding Rsbuild plugin:
iframeOptions.customEntry
Configures a custom entry to support other frameworks like Vue.
Only available in preview="iframe-follow" mode.
Here is an example for the Vue framework:
previewLanguages
- Type:
string[] - Default:
['jsx', 'tsx']
Configures the code languages that support preview. To support other formats like JSON or YAML, use this in conjunction with previewCodeTransform.
previewCodeTransform
- Type:
(codeInfo: { language: string; code: string }) => string - Default:
({ code }) => code
Performs custom transformation on code before preview.
The following example shows how to transform JSON Schema into a renderable React component:
You can configure it as follows:
Migrating from V1
When migrating from Rspress V1, the plugin functionality remains unchanged. Only the MDX source code syntax has the following adjustments:
<code src="./foo.tsx"/>should be migrated to File Code Block```tsx file="./foo.tsx"- The
defaultPreviewModeoption replacesiframeOptions.positionandpreviewMode - The default value of
defaultRenderModechanged from'preview'to'pure' @rsbuild/plugin-lessand@rsbuild/plugin-sassare no longer built-in. If you need Less or Sass support in previews, install the corresponding plugin and configure it viaiframeOptions.builderConfig:
Example 1:
Before: Required declarations in both config file and MDX file.
After: Only declare in the MDX file.
Example 2:
Before: Using iframe or previewMode="iframe" attribute.
After: Use the unified preview="..." attribute.