# Preview Frame

> Origin-gated sandboxed iframe block for embedding a live second-origin site preview or local dev server.

`preview_frame()` embeds a live web page served from an explicitly approved second origin (such as a compiled docs staging host or a local dev server) inside a restrictive sandbox while preserving parent navigation isolation.

Unlike [`html()`](https://ui.praison.ai/docs/components/text-and-markdown.md) (which renders inline HTML inside a constrained `srcDoc` frame), `preview_frame()` loads a real URL so themed stylesheets, scripts, and relative assets resolve against that origin.

## Example

```python
import praisonaiui as aiui

aiui.preview_frame(
    "http://127.0.0.1:4173/docs/getting-started",
    allowed_origins=["http://127.0.0.1:4173"],
    title="Staging site preview",
    height="640px",
    viewport="desktop",
)
```

In a React plugin using [`<BlockTree>`](https://ui.praison.ai/docs/features/plugins/browser-sdk.md):

```js
import { jsx } from 'react/jsx-runtime'
import { BlockTree } from '@aiui/sdk'

export default function StagingPreview({ previewUrl, previewOrigin }) {
  return jsx(BlockTree, {
    node: {
      type: 'preview_frame',
      props: {
        title: 'Staging preview',
        url: previewUrl,
        allowed_origins: [previewOrigin],
        height: '640px',
        viewport: 'desktop',
      },
    },
  })
}
```

## Security model

- **Explicit origin allowlist required:** `allowed_origins` must list exact `http(s)://host[:port]` origins. Wildcards (`"*"`), empty lists, `javascript:`, `data:`, embedded credentials, and unapproved origins render an accessible alert and **never** mount an `<iframe>`.
- **Parent navigation isolation:** Sandbox defaults to `allow-scripts allow-same-origin allow-forms` and automatically strips top-navigation escape tokens (`allow-top-navigation`, `allow-popups-to-escape-sandbox`).
- **CSP stays authoritative:** When your host sets a strict `Content-Security-Policy` (such as `create_shell_app(..., csp={"frame-src": ["http://127.0.0.1:4173"]})`), the browser enforces `frame-src` alongside the block's allowlist.
- **Honest loading state:** The frame displays a neutral loading status and an **Open in new tab** (`rel="noopener noreferrer"`) link without assuming an iframe `load` event proves page content succeeded.

## Props

| Parameter | Type | Default | Description |
|---|---|---|---|
| `url` | `str` | required | Absolute `http(s)://` URL to frame |
| `allowed_origins` | `list[str]` | required | Approved `http(s)://host[:port]` origins (no wildcards) |
| `title` | `str` | `"Site preview"` | Accessible iframe title and header label |
| `height` | `str \| int` | `"640px"` | Frame height (`px`, `vh`, `rem`, `%`, or pixel int `120–4000`) |
| `viewport` | `"desktop" \| "tablet" \| "mobile"` | `"desktop"` | Initial viewport preset (`100%`, `768px`, `390px`) |
| `sandbox` | `str \| None` | `"allow-scripts allow-same-origin allow-forms"` | Optional sandbox flags (top-navigation tokens are stripped) |
| `open_in_new_tab` | `bool` | `True` | Show external link button in frame header |
| `viewport_controls` | `bool` | `True` | Show Desktop / Tablet / Mobile viewport toggle buttons |

## Related pages

- [Content Security Policy](https://ui.praison.ai/docs/features/content-security-policy.md)
- [Compiled Site Hosting & Docs Inventory](https://ui.praison.ai/docs/features/plugins/site-hosting-and-inventory.md)
- [Media, Downloads & Sandboxed HTML](https://ui.praison.ai/docs/components/media-players.md)
