Overview of vitepress-plugin-sandpack
static, angular, react, react-ts, vanilla, vanilla-ts, vue, vue3, vue3-ts, svelte, solid, and test-ts.website·Indexed 31 days ago
https://vitepress-sandbox.js-bridge.comA plugin for VitePress that integrates Sandpack to create live-running code editing experiences. It supports multiple languages and frameworks including Angular, React, React-TS, Vanilla, Vanilla-TS, Vue, Vue3, Vue3-TS, Svelte, and Solid. Features include custom file content, snippet support, theme customization (including dark mode), and RTL layout support.
static, angular, react, react-ts, vanilla, vanilla-ts, vue, vue3, vue3-ts, svelte, solid, and test-ts.snippets) and place your source files within that directory. These files can then be referenced using the <<< syntax within a sandbox container.vite-vue template (Vue version >= 3.2.0), you can use the <script setup> syntax to define reactive state and render it in the template. This example demonstrates a simple component that uses a reactive reference to display a greeting.<script setup>
import { ref } from "vue";
const data = ref("world");
</script>
<template>
<h1>Hello {{ data }}</h1>
</template>
<style>
h1 {
font-size: 1.5rem;
}
</style>vite-vue-ts template (compatible with Vue version >= 3.2.0), you can use <script setup lang="ts"> to define reactive state with TypeScript generics. This allows for type-safe references using the ref function.<script setup lang="ts">
import { ref } from "vue";
const data = ref<string>("world");
</script>
<template>
<h1>Hello {{ data }}</h1>
</template>
<style>
h1 {
font-size: 1.5rem;
}
</style>vitepress-plugin-sandpack package as a development dependency in your VitePress project folder. If you are using pnpm, you must also install markdown-it-container.cd vitepress-project-folder
npm i vitepress-plugin-sandpack -D
# If using pnpm:
pnpm add markdown-it-container -DshowLineNumbers, resizablePanels, and closableTabs, create a wrapper component that extends Sandbox from vitepress-plugin-sandpack. Using TSX is recommended for simplicity, though Vue SFC is also supported.// OptsSandbox.tsx
import { defineComponent } from 'vue';
import { Sandbox, sandboxProps } from 'vitepress-plugin-sandpack';
export const OptsSandbox = defineComponent({
name: 'OptsSandbox',
props: sandboxProps,
setup(props, { slots }) {
return () => (
<Sandbox
{...props}
options={{
showLineNumbers: true,
resizablePanels: false,
closableTabs: true,
}}
>
{ slots?.default ? slots.default() : null }
</Sandbox>
);
},
});Because vitepress-plugin-sandpack is an ESM-only package, attempting to load it via require in a CommonJS environment will result in a 'Failed to resolve' error. To fix this, convert your VitePress configuration to ESM using one of the following methods:
"type": "module" to the nearest package.json file.docs/.vitepress/config.js (or .ts) to docs/.vitepress/config.mjs (or .mts).You can change the entry file path to a custom location (e.g., moving it from /src/main.ts to a root directory file like /myEntry.js).
Caveat: When changing the entry file path, you must manually ensure all required files are included in the bundle process, as pre-existing template settings may no longer apply. For example, if you move a component from /MyApp.vue to /src/MyApp.vue, you must update the import paths accordingly.
autorun option determines if the code executes automatically. If set to false, the user must manually click the run button in the editor area to execute the code.{
autorun: false // Default: true. Requires manual click of the run button.
}readOnly option can be applied globally to all files or specifically to individual files. Use showReadOnly to toggle the visibility of the read-only label on those files.{
readOnly: false, // Default: false. Set to true for global read-only mode.
showReadOnly: true // Default: true. Shows the read-only label of the file.
}{
hideEditor: false, // Default: false. Hides the editor area.
coderHeight: undefined, // Default: undefined. Sets the height of the editor.
previewHeight: undefined // Default: undefined. Sets the height of the preview area.
}{
showNavigator: false, // Default: false. Shows the navigator.
showLineNumbers: false, // Default: false. Shows line numbers in the editor.
wrapContent: false, // Default: false. Wraps content in the editor.
showRefreshButton: true, // Default: true. Shows the refresh button (disabled if showNavigator is true).
showTabs: true, // Default: true. Shows file tabs.
closableTabs: false // Default: false. Shows the close icon on tabs.
}