### Quickstart with loadSandpackClient Source: https://sandpack.codesandbox.io/docs/advanced-usage/client Use loadSandpackClient to mount a bundler and update the sandbox content. This helper abstracts common setup tasks for runtime environments. ```typescript import { loadSandpackClient } from "@codesandbox/sandpack-client"; async function main() { // Iframe selector or element itself const iframe = document.getElementById("iframe"); // Files, environment and dependencies const content: SandboxSetup = { files: { // We infer dependencies and the entry point from package.json "/package.json": { code: JSON.stringify({ main: "index.js", dependencies: { uuid: "latest" }, })}, // Main file "/index.js": { code: `console.log(require('uuid'))` } }, environment: "vanilla" }; // Optional options const options: ClientOptions = {}; // Properly load and mount the bundler const client = await loadSandpackClient( iframe, content, options ); /** * When you make a change, you can just run `updateSandbox`. * We'll automatically discover which files have changed * and hot reload them. */ client.updateSandbox({ files: { "/index.js": { code: `console.log('New Text!')`, }, }, entry: "/index.js", dependencies: { uuid: "latest", }, }); } ``` -------------------------------- ### Setup static server with PreviewController Source: https://sandpack.codesandbox.io/docs/advanced-usage/static Create a PreviewController with a base URL pointing to the hosted static server, provide a getFileContent function to serve file contents, initialize the preview to get a URL, and create an iframe with that URL. ```typescript // Map of files const files = new Map(["index.html", "Hello world!"]); // Setup a preview controller const controller = new PreviewController({ baseUrl: "https://preview.sandpack-static-server.codesandbox.io", // Function to get the file content for the server this can also be async getFileContent: (filepath) => { const content = files.get(filepath); if (!content) { throw new Error("File not found"); } return content; }, }); // Initialize the preview // This will start up a relay frame and return a url which you can use to show the preview const previewUrl = await this.previewController.initPreview(); // Create a preview frame and set the src url to the returned preview url const iframe = document.createElement("iframe"); iframe.setAttribute("src", previewUrl); ``` -------------------------------- ### Install @monaco-editor/react Source: https://sandpack.codesandbox.io/docs/guides/integrate-monaco-editor Run this command to install the library that handles the Monaco editor setup in React. ```bash yarn add @monaco-editor/react ``` -------------------------------- ### Install Sandpack Client Source: https://sandpack.codesandbox.io/docs/advanced-usage/client Install the package using npm. ```bash npm i @codesandbox/sandpack-client ``` -------------------------------- ### Fresh design example Source: https://sandpack.codesandbox.io/docs/resources/migration-guide/v1 Example demonstrating the updated design system implementation. ```javascript import "./styles.css"; document.getElementById("app").innerHTML = `

Hello world

`; ``` -------------------------------- ### Install static-browser-server with npm Source: https://sandpack.codesandbox.io/docs/advanced-usage/static Install the static-browser-server package using npm. This is the first step to use the static template. ```bash npm i static-browser-server ``` -------------------------------- ### Install project dependencies Source: https://sandpack.codesandbox.io/docs/resources/community Install all necessary dependencies for the project using yarn. ```bash yarn ``` -------------------------------- ### Run Sandpack Docs Source: https://sandpack.codesandbox.io/docs/resources/community Commands to start the documentation server from the root or docs folder. ```bash yarn dev:docs ``` ```bash yarn run serve ``` -------------------------------- ### Static file usage example Source: https://sandpack.codesandbox.io/docs/advanced-usage/serving-static-files A complete example showing how to define a file in the /public folder and reference it within an application component. ```jsx import { Sandpack } from "@codesandbox/sandpack-react"; export default () => ( React Logo `, "/App.js": `export default function App() { return ( <>

Hello React

); } `, }} options={{ experimental_enableServiceWorker: true, }} template="react" /> ); ``` -------------------------------- ### Quickstart: Basic Sandpack component Source: https://sandpack.codesandbox.io/docs/getting-started/custom-content A minimal React component that renders the Sandpack editor and preview. Import Sandpack from the package and return the component. ```jsx import { Sandpack } from "@codesandbox/sandpack-react"; export default function App() { return } ``` -------------------------------- ### Install tsup as a development dependency Source: https://sandpack.codesandbox.io/docs/guides/local-dependencies Install the tsup bundler to prepare your local library for use in Sandpack. ```bash $ npm install --save-dev tsup ``` -------------------------------- ### Build project dependencies Source: https://sandpack.codesandbox.io/docs/resources/community Build all dependencies before starting development mode. ```bash yarn build ``` -------------------------------- ### Install Sandpack with npm Source: https://sandpack.codesandbox.io/docs/getting-started/custom-content Install the @codesandbox/sandpack-react package using npm. This is the standard installation method for adding Sandpack to a project. ```bash npm i @codesandbox/sandpack-react ``` -------------------------------- ### Initialize Sandpack with Template Source: https://sandpack.codesandbox.io/docs Use the template prop to quickly start a project with pre-configured files and dependencies. ```javascript ``` -------------------------------- ### Run Sandpack Landing page Source: https://sandpack.codesandbox.io/docs/resources/community Commands to start the Next.js landing page from the root or landing folder. ```bash yarn dev:landing ``` ```bash yarn dev ``` -------------------------------- ### Install Sandpack via pnpm Source: https://sandpack.codesandbox.io/docs/getting-started Install the Sandpack dependency using the pnpm package manager. ```bash pnpm add @codesandbox/sandpack-react ``` -------------------------------- ### Install Sandpack via yarn Source: https://sandpack.codesandbox.io/docs/getting-started Install the Sandpack dependency using the yarn package manager. ```bash yarn add @codesandbox/sandpack-react ``` -------------------------------- ### Run Sandpack React in development mode Source: https://sandpack.codesandbox.io/docs/resources/community Start the React development environment, which opens a Storybook playground. ```bash yarn dev:react ``` -------------------------------- ### React Markdown Preview Code Source: https://sandpack.codesandbox.io/docs The code content for the React Markdown example. ```javascript import ReactMarkdown from 'react-markdown' export default function App() { return ( # Hello, *world*! ) } ``` -------------------------------- ### Custom Sandpack implementation with SandpackProvider Source: https://sandpack.codesandbox.io/docs/api/react/interfaces/SandpackPreviewRef A basic implementation of a custom Sandpack setup using the SandpackProvider, SandpackLayout, SandpackCodeEditor, and SandpackPreview components. ```javascript import { SandpackProvider, SandpackLayout, SandpackPreview, SandpackCodeEditor } from "@codesandbox/sandpack-react"; const CustomSandpack = () => ( ); export default () => ``` -------------------------------- ### Basic SandpackCodeEditor configuration Source: https://sandpack.codesandbox.io/docs/advanced-usage/components Example of implementing the SandpackCodeEditor component within a SandpackProvider and SandpackLayout, demonstrating common UI props like showTabs and showInlineErrors. ```javascript import { SandpackProvider, SandpackLayout, SandpackPreview, SandpackCodeEditor, } from "@codesandbox/sandpack-react"; export default () => ( ); ``` -------------------------------- ### Adding support for stream languages Source: https://sandpack.codesandbox.io/docs/advanced-usage/components Example of adding a stream language mode by wrapping it in a LanguageSupport instance. ```javascript import { LanguageSupport, StreamLanguage } from "@codemirror/language"; import { shell } from "@codemirror/legacy-modes/mode/shell"; ; ``` -------------------------------- ### Basic Sandpack component with React template Source: https://sandpack.codesandbox.io/docs/quickstart A minimal Sandpack component setup with a React template and light theme. The files object is empty, so the default template files are used. ```jsx import { Sandpack } from "@codesandbox/sandpack-react"; const App = () => { const files = {} return ( ) } ``` -------------------------------- ### useSandpackConsole: Get logs and reset Source: https://sandpack.codesandbox.io/docs/advanced-usage/hooks Use this hook to consume console logs from a sandpack client. It returns an object with logs and a reset function. The example shows destructuring both. ```typescript const { logs, reset } = useSandpackConsole(); ``` -------------------------------- ### useSandpackClient: Listen to messages and set iframe height Source: https://sandpack.codesandbox.io/docs/advanced-usage/hooks Use this hook to register a new sandpack client and get its instance, listeners, and dispatch function. The example shows listening for resize messages and updating a computed auto height, then rendering an iframe with the ref. Note the eslint-disable comment for exhaustive-deps. ```typescript const { iframe, listen } = useSandpackClient(); React.useEffect(() => { const unsubscribe = listen((message: SandpackMessage) => { if (message.type === "resize") { setComputedAutoHeight(message.height); } }); return unsubscribe; // eslint-disable-next-line react-hooks/exhaustive-deps }, []);