Skip to content

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

Components

Free

Web Preview for React

A small browser for the page an agent built. For React with Base UI: 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

import {
  ArrowLeftIcon, ArrowRightIcon, RefreshCcwIcon,
} from "lucide-react";
import {
  WebPreview, WebPreviewBody, WebPreviewConsole, WebPreviewNavigation,
  WebPreviewNavigationButton, WebPreviewUrl,
} from "@/components/ui/web-preview";
<WebPreview className="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.

const { messages } = useChat();

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

const building =
  page?.state === "input-streaming" || page?.state === "input-available";

return (
  <WebPreviewBody
    loading={building ? <Spinner /> : undefined}
    srcDoc={page?.state === "output-available" ? page.output.html : undefined}
  />
);

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

If you added Web Preview from AI SDK Elements (opens in a new tab), your project has components/ai-elements/web-preview.tsx. Install this one, change the imports to @/components/ui/web-preview and delete the old file. Every part keeps its name, and defaultUrl, onUrlChange, tooltip, loading and logs mean what they did. What changes:

  • WebPreview also takes url, to control the address, and keeps a history, so onUrlChange fires on back and forward as well.
  • WebPreviewNavigationButton takes action (back, forward or reload). Without it, the button does what your handler says, as before.
  • WebPreviewBody takes srcDoc for HTML and drops allow-same-origin from the sandbox when it does. loading now covers the frame rather than sitting under it.
  • WebPreviewConsole is the collapsible itself, opened through its own props rather than a context shared with the root. Times read as hh:mm:ss on a 24-hour clock in every locale.

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.

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
className?string

Merged with the part's classes.

WebPreviewNavigation

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

PropType
className?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"
focusableWhenDisabled?boolean

Whether the button should be focusable when disabled.

Default
false
nativeButton?boolean

Whether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).

Default
true
tooltip?string

Shown on hover and focus, and its accessible name.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

WebPreviewUrl

data-slot="web-preview-url"

PropType
value?React.ComponentProps<"input">["value"]

The value of the input. Use when controlled.

Type
React.ComponentProps<"input">["value"]
Default
draft
defaultValue?Field.Control.Props["defaultValue"]

The default value of the input. Use when uncontrolled.

Type
Field.Control.Props["defaultValue"]
onValueChange?(value: string, eventDetails: Input.ChangeEventDetails) → void

Callback fired when the value changes. Use when controlled.

Type
(value: string, eventDetails: Input.ChangeEventDetails) → void
Parameters

value – string

eventDetails – Input.ChangeEventDetails

Returns
void
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

WebPreviewBody

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

PropType
loading?React.ReactNode

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.

className?string

Merged with the part's classes.

WebPreviewConsole

data-slot="web-preview-console"

PropType
open?boolean

Whether the collapsible panel is currently open. To render an uncontrolled collapsible, use the defaultOpen prop instead.

defaultOpen?boolean

Whether the collapsible panel is initially open. To render a controlled collapsible, use the open prop instead.

Default
false
onOpenChange?(open: boolean, eventDetails: CollapsibleRootChangeEventDetails) → void

Event handler called when the panel is opened or closed.

Type
(open: boolean, eventDetails: CollapsibleRootChangeEventDetails) → void
Parameters

open – boolean

eventDetails – CollapsibleRootChangeEventDetails

Returns
void
disabled?boolean

Whether the component should ignore user interaction.

Default
false
logs?WebPreviewLog[]

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

Default
[]
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-level?string

Mirrors level.