Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Web Preview for Svelte UI SDK

A small browser for the page an agent built. For Svelte with UI SDK: back, forward, reload, an address field, the page in a sandbox, and its console.

On this page11 sections

Web Preview is a small browser inside your app: back, forward and reload, an address field, the page in a sandboxed frame, and a console that opens onto the page's logs. Put it beside the chat to show what generated code renders, whether that's HTML a model just wrote or a dev server's page, so people can click through it and read its errors.

For a still screenshot framed as a browser window on a landing page, use Safari instead.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Web Preview anatomy: root, navigation, navigation button, url, body, console

Usage

<script lang="ts">
import {
  ArrowLeftIcon, ArrowRightIcon, RefreshCcwIcon,
} from "lucide-svelte";
import {
  WebPreview, WebPreviewBody, WebPreviewConsole, WebPreviewNavigation,
  WebPreviewNavigationButton, WebPreviewUrl,
} from "@/components/ui/web-preview";
</script>
<WebPreview class="h-96" defaultUrl="http://localhost:3000/">
  <WebPreviewNavigation>
    <WebPreviewNavigationButton action="back" tooltip="Go back">
      <ArrowLeftIcon aria-hidden="true" />
    </WebPreviewNavigationButton>
    <WebPreviewNavigationButton action="forward" tooltip="Go forward">
      <ArrowRightIcon aria-hidden="true" />
    </WebPreviewNavigationButton>
    <WebPreviewNavigationButton action="reload" tooltip="Reload">
      <RefreshCcwIcon aria-hidden="true" />
    </WebPreviewNavigationButton>
    <WebPreviewUrl />
  </WebPreviewNavigation>
  <WebPreviewBody />
  <WebPreviewConsole />
</WebPreview>

action gives a button its job in the preview's history. Back and forward disable themselves at either end, and reload loads the open page again. A button without an action does only what its handler says, like the open-in-a-new-tab and maximize buttons in the first demo.

Accessibility

Each toolbar button is named by its tooltip, which also opens on keyboard focus, so a button without one needs an aria-label. The address field is labeled Address and always reads left to right. The frame is titled Preview, which is what a screen reader announces on entering it, so pass WebPreviewBody a title that names the page. The console's toggle is a button that reports whether it's open, and its lines sit in a log region, each warning and error marked in words as well as by icon and color.

Tab
Moves through the toolbar's buttons and the address field, into the page in the frame, and on to the console's toggle.
Enter
In the address field, opens the typed address, or loads the open one again if nothing changed. On a button or the console's toggle, presses it.
Space
On a button or the console's toggle, presses it.
Escape
In the address field, puts the open address back in place of what was typed. On a toolbar button, closes its tooltip.

Demos

Console

A page with three buttons that log, warn and throw, over a console that starts open through defaultOpen. A script in the page posts each console call and each uncaught error to the parent, and the demo appends it to logs after checking that it came from its own frame. Warnings and errors get their own color and icon, and Clear console empties the list.

Preview width

Controlled

Three pages listed beside the preview, with the address kept in state through url and onUrlChange. The list, the address field, the page's links and Back all change that one value, and an address set from outside is a visit Back returns from. The links post the address they want to the parent, since a plain link in srcDoc HTML would load your own site's path in the frame.

Preview width

Loading

A generated landing page and a sparkles button that asks for another version. For a second and a half, loading covers the frame with a spinner while the button is disabled, then the page comes back with a new headline. The overlay shows whatever you pass for as long as you pass it, so drive it from your own state, such as a tool call that hasn't finished.

Preview width

AI SDK

A model builds a page through a tool, createPage here, whose output is the HTML. Hand that HTML to WebPreviewBody as srcDoc once the call has finished, and cover the frame while it runs.

import { Chat } from "@ai-sdk/svelte";

const chat = new Chat({});

const page = $derived(
  chat.messages
    .flatMap((message) => message.parts)
    .filter((part) => part.type === "tool-createPage")
    .at(-1)
);

When the tool starts a dev server in a sandbox instead, it returns the server's address. Pass that as url and leave srcDoc out.

Migrating from AI SDK Elements

AI SDK Elements is for React only, so there is nothing to migrate in Svelte. Web Preview has the same parts and props here, and the demos above are the place to start.

FAQ

When should I use Web Preview instead of Safari?

Use Web Preview when the page is live and people should use it: follow its links, reload it, type an address and read its console. It runs the real page in a frame.

Safari is a picture of a browser around a screenshot, a video or a page, for a landing page or a changelog. It has no history and nothing to type into, which is right for a hero image and wrong for a page someone needs to test.

How do I preview HTML a model wrote instead of a URL?

Pass the HTML to WebPreviewBody as srcDoc. It runs in a sandbox without allow-same-origin, which gives it an origin of its own, so its scripts can't read your page's cookies or storage.

Its links still resolve against your site, so a plain link would load one of your own pages in the frame. Have the page post the address it wants to the parent and set url from that message, as the Controlled demo does.

What does the Web Preview open for an address typed without https://?

The address field adds the scheme a browser's address bar would. A bare host such as example.com opens as https://example.com, and localhost:3000 or an IP address opens with http://, so a local dev server loads as typed.

A path that starts with a slash goes to the frame as it is, and the frame resolves it like a link on your page, so /pricing loads that path on your own site. If people will type paths for an app the model built, resolve them yourself before they reach the frame, as the first demo on this page does by keeping the address in state and choosing the HTML from it.

Why does the Web Preview show a blank page for some sites?

Those sites refuse to be framed. An X-Frame-Options header or a frame-ancestors rule in their Content Security Policy tells the browser not to show them inside another page, and no component can get around that.

Preview your own dev server or the generated HTML, and keep an open-in-a-new-tab button for everything else, like the one in the first demo.

Do back and forward work in the Web Preview?

They walk the addresses the preview opened itself: the ones typed into the address field and the ones you set through url. Give a button action="back" or action="forward" and it moves through them, disabled at either end.

Navigation inside the frame isn't recorded: the preview doesn't watch the frame, and it couldn't read the address of a page on another origin anyway. To put a page's own navigation in the history, have it post the new address and set url, as the Controlled demo does.

How do I show the page's console logs in the Web Preview?

Pass them to WebPreviewConsole as logs, each with a level of log, warn or error, a message and a timestamp. For HTML you control, add a script that posts every console call to the parent and append what arrives, as the Console demo does. For a dev server in a sandbox, stream its logs from your server instead.

The console starts closed. Pass defaultOpen to open it from the start, or open with onOpenChange to drive it from state.

What can a page do inside the Web Preview's sandbox?

It can run scripts, submit forms and open popups. A page loaded from an address also keeps its own origin, so it can use its storage and cookies as far as the browser allows a framed page, while HTML from srcDoc gets an opaque origin of its own. Pass sandbox to WebPreviewBody to set the flags yourself.

Serve untrusted pages from a different origin than your app. A same-origin page that may run scripts and keep its origin can reach into your page and lift the sandbox.

How do I make the Web Preview fill its panel?

Give WebPreview a height, or set it to fill a parent that has one. It's a flex column, so the toolbar and the console keep their size and the frame takes everything left over.

Without a height, the frame falls back to the browser's default of 150 pixels for an iframe. The demos on this page each set a height on the root with a class.

API Reference

WebPreview

PropType
url?string

The address, controlled: set it with onUrlChange. Two-way with bind:url.

defaultUrl?string

The address it opens at, when url isn't set.

Default
""
onUrlChange?(url: string) → void

Called with the new address when the reader navigates.

Parameters

url – string

Returns
void
class?string

Merged with the part's classes.

WebPreviewNavigation

<div> data-slot="web-preview-navigation"

PropType
class?string

Merged with the part's classes.

WebPreviewNavigationButton

data-slot="web-preview-navigation-button"

PropType
action?"back" | "forward" | "reload"

What it does in the preview's history: back, forward (each disabled at its end) or reload. Without it, it does what onClick says.

Type
"back" | "forward" | "reload"
clickEffect?boolean

Apply a click effect to the button.

Default
true
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled button in the tab order with aria-disabled instead of the native attribute, so it can hold focus while it becomes disabled and host a tooltip that says why. Presses still do nothing.

Default
false
isLoading?boolean

Show a loading indicator.

Default
false
onclick?( event: MouseEvent, details?: ButtonActionDetails ) → unknown

Runs when the button is pressed. Return a Promise to keep it pending (data-pending) until it settles. details.source says how it was pressed; a wrapper passing the click on may leave it out.

Type
( event: MouseEvent, details?: ButtonActionDetails ) → unknown
Parameters

event – React.MouseEvent<HTMLButtonElement>

details? – ButtonActionDetails

Returns
unknown
pendingAnnouncement?string

What screen readers hear once the button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.

Default
"Loading"
tooltip?string

Shown on hover and focus, and its accessible name.

type?"button" | "reset" | "submit"

What a native button does in a form: "submit" submits it and "reset" resets it. Ignored when the button renders as something else.

Type
"button" | "reset" | "submit"
Default
"button"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

WebPreviewUrl

data-slot="web-preview-url"

PropType
value?string | number

The value, for a controlled field. A number is written as its text, as a native input does. Two-way with bind:value.

Type
string | number
Default
draft
defaultValue?string | number

The value it starts with. A form reset returns here.

Type
string | number
onValueChange?(value: string, details: ChangeDetails<InputValueChangeReason>) → void

Called when the user changes the value: typing ("input-change"), a Clear part ("clear-press"), a form reset ("reset") or a controller ("imperative"). details.cancel() keeps the value as it was.

Type
(value: string, details: ChangeDetails<InputValueChangeReason>) → void
Parameters

value – string

details – ChangeDetails<InputValueChangeReason>

Returns
void
controller?InputController

Reads and writes the field from anywhere. Create one with createInputController().

debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Disables the field.

readOnly?boolean

Makes the field read-only.

required?boolean

Makes the field required.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

WebPreviewBody

<div> data-slot="web-preview-body"

PropType
loading?Snippet

Shown over the page while it's set: a spinner while the page is built.

src?string

The page's address, in place of the preview's.

srcDoc?string

The page's HTML, in place of an address: what a model just wrote.

class?string

Merged with the part's classes.

WebPreviewConsole

data-slot="web-preview-console"

PropType
open?boolean

Whether the panel is open. Pass it to control the collapsible. Two-way with bind:open.

defaultOpen?boolean

Whether the panel is open at first, when uncontrolled.

Default
false
onOpenChange?(open: boolean, details: ChangeDetails<CollapsibleOpenChangeReason>) → void

Runs when the panel wants to open or close. details.reason says why, and details.cancel() stops it.

Type
(open: boolean, details: ChangeDetails<CollapsibleOpenChangeReason>) → void
Parameters

open – boolean

details – ChangeDetails<CollapsibleOpenChangeReason>

Returns
void
controller?CollapsibleController

Opens and closes the panel from anywhere. Create one with createCollapsibleController().

debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops the trigger from opening or closing the panel. The trigger stays focusable.

Default
false
logs?WebPreviewLog[]

The lines to show, oldest first, each colored by its level.

Default
[]
onOpenChangeComplete?(open: boolean) → void

Runs once the panel has finished opening or closing, after any animation.

Parameters

open – boolean

Returns
void
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-level?string

Mirrors level.