aiellie

Search

Find a component, template or page

GitHub
Components

Web Preview

A page in a frame with a browser's chrome: back, forward and reload, an address bar with the connection's details, a console that folds out, an annotate toggle, and a menu with zoom and a phone-width view.

No console output

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/web-preview"

shadcn needs the @aiellie registry in components.json first. npx aiellie add web-preview adds it on its first run, then installs the same way.

Web Preview frames a page with the parts of a browser people reach for: back, forward and reload, an address bar, a console, and a menu with zoom and a phone-width view. It's for showing what an agent built, or a page someone is debugging, inside the app.

The address bar shows the URL without its scheme and gives it back when you press Enter, so localhost keeps its http://. Enter reloads even when the URL didn't change, the way a real address bar does. The icon at its start opens the connection's details.

Getting started

  1. Frame a page

    WebPreview holds the URL and the console's state. Put a navigation bar, the body and the console inside it.

    components/preview.tsx
    import {
      WebPreview,
      WebPreviewBody,
      WebPreviewConsole,
      WebPreviewNavigation,
      WebPreviewUrl,
    } from "@/components/aiellie/web-preview"
    
    export function Preview({ url }: { url: string }) {
      return (
        <WebPreview defaultUrl={url} className="h-[480px]">
          <WebPreviewNavigation>
            <WebPreviewUrl />
          </WebPreviewNavigation>
          <WebPreviewBody />
          <WebPreviewConsole />
        </WebPreview>
      )
    }
  2. Add history

    Control url to keep your own history, and wire it to navigation buttons. useWebPreview gives any part inside the preview its reload.

    components/preview.tsx
    const [history, setHistory] = useState({ stack: [url], index: 0 })
    
    <WebPreview
      url={history.stack[history.index]}
      onUrlChange={(next) =>
        setHistory(({ stack, index }) => ({
          stack: [...stack.slice(0, index + 1), next],
          index: index + 1,
        }))
      }
    >
      <WebPreviewNavigation>
        <WebPreviewNavigationButton
          tooltip="Back"
          disabled={history.index === 0}
          onClick={() => setHistory((h) => ({ ...h, index: h.index - 1 }))}
        >
          <HugeiconsIcon icon={ArrowLeft01Icon} />
        </WebPreviewNavigationButton>
        <WebPreviewUrl />
        <WebPreviewMenu zoom={zoom} onZoomChange={setZoom} />
      </WebPreviewNavigation>
      <WebPreviewBody zoom={zoom} />
    </WebPreview>

Anatomy

<div data-slot="web-preview">
  <div data-slot="web-preview-navigation">
    <button data-slot="web-preview-navigation-button" />
    <div data-slot="web-preview-url" role="group">
      {/* connection popover, the address, open in a new tab */}
    </div>
    <button data-slot="web-preview-annotate-toggle" aria-pressed />
    {/* WebPreviewMenu */}
  </div>
  <div data-slot="web-preview-body" data-device>
    <iframe title="Preview" sandbox="…" />
  </div>
  <div data-slot="web-preview-console" data-open>
    <div role="log" />
  </div>
</div>

A live bar sweeps along the top of the body until the frame loads. Reload remounts the frame, so it works without a handler. The console folds open, counts errors and warnings in its bar, and keeps to the bottom as logs arrive.

Every menu entry stays disabled until you pass its handler. The password and cookie entries are labels only: nothing in the component reads or stores a credential.

Many sites refuse to be framed. Preview pages you serve yourself, or pass srcDoc. The console only shows the logs you give it, so the framed page has to post them to you.

Examples

A dev server's console

Pass logs to fill the console. Errors are tinted and counted in its bar.

20:53:44ready on :3000
20:53:44Fast Refresh had to do a full reload
20:53:44Hydration failed: the server rendered 3 items, the client 4

API reference

WebPreview

PropTypeDefaultDescription
url, defaultUrl, onUrlChangestring""
onReload() => voidFires on every reload, including Enter on the same URL.
consoleOpen, defaultConsoleOpen, onConsoleOpenChangebooleanfalse

All other div props are forwarded.

WebPreviewNavigationButton

PropTypeDefaultDescription
tooltipstringIts tooltip and accessible name.
shortcutstringKeys shown after the tooltip.

All other Button props are forwarded.

WebPreviewUrl

PropTypeDefaultDescription
connectionContentReactNode<WebPreviewConnection />The connection popover's body.
onOpenExternal(url: string) => voidReplaces opening the URL in a new tab.

All other input props are forwarded.

WebPreviewBody

PropTypeDefaultDescription
srcstringthe URL
srcDocstringA page to render instead of a URL.
zoomnumber100In percent.
devicebooleanfalseA phone's width, centred.
loadingReactNodeShown over the frame. Left out, a bar sweeps while it loads.

All other iframe props are forwarded.

WebPreviewConsole

PropTypeDefaultDescription
logs{ level: "log" | "warn" | "error"; message: string; timestamp: Date }[][]
onClear() => voidAdds Clear to its bar while open.

WebPreviewAnnotateToggle

PropTypeDefaultDescription
activebooleanfalse
onActiveChange(active: boolean) => void
labelstring"Annotating"Shown while active, and its name.

WebPreviewMenu

PropTypeDefaultDescription
zoom, onZoomChangenumber100Steps through Chrome's zoom levels, from 25 to 200.
fullScreen, onFullScreenChangebooleanfalse
deviceToolbar, onDeviceToolbarChangebooleanfalse
onFindInPage, onPrint, onScreenshot, onDownloads, onBrowserSettings() => voidEach enables its entry.
onImportCookiesAndPasswords, onPasswordManager, onContactInfo() => voidEach enables its entry.
onClearCookies, onClearCache, onDeleteDownloadHistory() => voidEach enables its entry in Clear browsing data.