# Playground Render

`PlaygroundRender` is a playground for code that renders in the browser. It needs no WebContainer. You give it a `render` function, the editor panes, and the examples. It shows the editors, the examples, the issues, and the preview.

_Interactive preview available on the rendered documentation page._


## Installation

Install `@vite-hub/ui` as described in [Installation](https://vitehub.dev/docs/ui/installation). The Nuxt module auto-imports `PlaygroundRender`. In Vue with Vite, import it from `vite-hub/ui`:

```ts
import { PlaygroundRender } from "vite-hub/ui";
```

## Usage

```vue
<script setup lang="ts">
import type { PlaygroundFiles } from "vite-hub/ui";
import { marked } from "marked";

const presets = [{ id: "hello", label: "Hello", files: { "content.md": "# Hello\n" } }];
const panes = [{ file: "content.md", label: "Markdown", language: "markdown" }];
const languages = [() => import("shiki/langs/markdown.mjs")];

async function render(files: PlaygroundFiles) {
  return { html: await marked.parse(files["content.md"] ?? ""), issues: [] };
}
</script>

<template>
  <PlaygroundRender :presets :panes :languages :render class="h-[70vh]" />
</template>
```

Each edit renders 200 ms after the last key, and a render that ends after a newer one is dropped. The URL hash holds the files as a share link, with the open example as its template. `render(files)` returns `{ html, issues? }`, where an issue is `{ message, line? }`. A thrown error shows as an issue.

## Accessibility

The examples are a list of buttons, and the open one has `aria-current`. The file tabs are a group of toggle buttons. Each editor is a text area named by the label of its pane. Reset asks first in a dialog. The preview runs in a sandboxed frame with its own origin, so a shared link cannot reach your page.

## API Reference

### PlaygroundRender

#### Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `presets` | `PlaygroundPreset[]` | Required | The examples, each `{ id, label, files, group? }`. A share link names one of them as its template. |
| `preset` | `string` | | The id of the example that opens when the URL has no link. Without it, the first preset opens. |
| `panes` | `PlaygroundPane[]` | Required | The editor tabs, each `{ file, label, language?, slot? }`. A pane with `slot` shows that slot in place of the editor. |
| `render` | `(files) => Promise<PlaygroundResult>` | Required | Renders the files to `{ html, issues? }`. |
| `languages` | `LanguageInput[]` | | Shiki languages for the panes. They load after the first render. Without them, the panes are plain text. |
| `previewDocument` | `(html, files) => string` | | Wraps the HTML in the preview page, with your stylesheets and theme. Without it, the preview is a plain page. |

#### Events

| Event | Payload | Description |
| --- | --- | --- |
| `load` | `PlaygroundOpenEvent` | `{ preset, files }`. The files opened from the URL hash, or from the opening example when the URL has no link. It fires on mount and each time the hash changes. `preset` is the example's id, or `undefined` for a link without one. |
| `preset-change` | `PlaygroundOpenEvent` | `{ preset, files }`. Another example opened: the reader picked it, or a link names it. Reset and edits do not fire it. |

#### Slots

| Slot | Props | Description |
| --- | --- | --- |
| `toolbar` | `{ files, preset, setFile }` | Controls in the editor bar. |
| `preset` | `{ preset, active }` | Draws one example in the list. |
| `group` | `{ group, presets }` | Draws the heading of a group of examples, such as an icon before its name. Examples without a `group` have no heading. |
| The `slot` of a pane | `{ pane, value, files, setFile }` | Replaces the editor of that pane, such as a theme form that writes `theme.json`. |

## Related

- [Device frame](https://vitehub.dev/docs/ui/device-frame)
- [Copy button](https://vitehub.dev/docs/ui/copy-button)

<!-- vitehub-docs:section-links -->
## In this section

- [Start](https://vitehub.dev/raw/docs/ui.md)
- [AI components](https://vitehub.dev/raw/docs/ui/ai-components.md)
- [Blocks](https://vitehub.dev/raw/docs/ui/blocks/chat-app.md)
- [Parts](https://vitehub.dev/raw/docs/ui/message.md)
- [Chat](https://vitehub.dev/raw/docs/ui/chat.md)
- [Collaboration](https://vitehub.dev/raw/docs/ui/avatar-stack.md)
- Agent work
  - [Tool List](https://vitehub.dev/raw/docs/ui/tool-list.md)
  - [Diff](https://vitehub.dev/raw/docs/ui/diff.md)
  - [Code View](https://vitehub.dev/raw/docs/ui/code-view.md)
  - [File Tree](https://vitehub.dev/raw/docs/ui/file-tree.md)
  - [Device Frame](https://vitehub.dev/raw/docs/ui/device-frame.md)
  - [Playground Render](https://vitehub.dev/raw/docs/ui/playground-render.md) (this page)
- [Utilities](https://vitehub.dev/raw/docs/ui/attachments.md)

Next: [Diagnostics](https://vitehub.dev/raw/docs/ui/diagnostics.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
