Components

Web Preview

Shows a web page inside the app, with a loading state, a fallback for pages that can’t be framed, and how long the load took.

Usage

import {
  WebPreview,
  WebPreviewAddress,
  WebPreviewFallback,
  WebPreviewFrame,
  WebPreviewLoading,
  WebPreviewOpen,
  WebPreviewReload,
  WebPreviewStats,
  WebPreviewToolbar,
} from 'nyctoui/web-preview'

<WebPreview src={bookmark.url} blocked={!probe.embeddable}>
  <WebPreviewToolbar>
    <WebPreviewReload />
    <WebPreviewAddress />
    <WebPreviewOpen />
  </WebPreviewToolbar>
  <WebPreviewFrame title={bookmark.title}>
    <WebPreviewLoading />
    <WebPreviewFallback />
  </WebPreviewFrame>
  <WebPreviewStats />
</WebPreview>

Give the root a height; the frame takes what the toolbar and stats bar leave. Every part but the frame is optional. The root carries data-status: idle, loading, loaded, timeout or blocked.

Good for

  • A live preview of a site in a website builder or a CMS.
  • Bookmarks or links in a read-later app, opened in place.
  • A deploy preview in a CI dashboard.
  • A preview of an app built by an AI agent, beside the chat.

What the browser reports

A frame tells the page around it very little. The load time is always known: it runs from setting src to the frame’s load event. Sizes depend on the origin. A same-origin page shares its own timeline, so the stats show every byte and request it made before load. A cross-origin page reports its document size only when it sends Timing-Allow-Origin, which few sites do.

The browser also hides whether a site refuses to be framed: load fires on its error page all the same. Check X-Frame-Options and CSP frame-ancestors on a server, and pass the answer as blocked.

Examples

Same origin, cross origin, blocked

The same preview with three kinds of page. The same-origin page reports its size and requests, the cross-origin one only its time, and the blocked one shows the fallback without loading anything.

Loading and reload

WebPreviewLoading covers the frame during the first load and fades out when the page arrives. A reload keeps the old page on screen, as a browser does, and only the line along the top edge sweeps. If load hasn’t fired after timeout ms, the fallback appears; the page can still replace it if it arrives late.

Custom stats

Pass a function to WebPreviewStats to write the bar yourself, for example with a size your server measured. formatDuration and formatBytes are exported for this.

Custom fallback

WebPreviewFallback takes children, or a function of the state to word it by status.

Accessibility

The frame needs a title that names the page. The root is aria-busy while loading, and the fallback is a status region, so a blocked or slow page is announced. Reload and open are icon buttons with labels.

Best practices

Do
  • Give the root a height; the frame fills what the toolbar leaves.
  • Check whether a page can be framed before showing it, and pass blocked for those that can’t, so the fallback shows at once.
  • Keep an Open button, so people can always see the page in a real tab.
  • Set sandbox to the least the page needs, especially for pages you don’t control.
Don’t
  • Frame pages that need people to sign in or pay. Send them to a real tab.
  • Hide the address. People should know which page they’re looking at.

API reference

WebPreview

A <div> card that holds the parts and their shared state.

PropTypeDefault
src
The page to show.
string—
blocked
The page refuses to be framed. Shows the fallback and loads nothing.
booleanfalse
timeout
Milliseconds to wait for load before showing the fallback.
number10000
onStatusChange
Called whenever the status changes.
(status: WebPreviewStatus, state: WebPreviewState) => void—
render
Replaces the rendered element, keeping the styles.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

WebPreviewToolbar

A <div> row above the page.

PropTypeDefault
render
Replaces the rendered element, keeping the styles.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

WebPreviewAddress

The address, host in full and path dimmed. Children replace it.

PropTypeDefault
render
Replaces the rendered element, keeping the styles.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

WebPreviewReload

A ghost icon Button that reloads the page. Takes Button’s props.

PropTypeDefault
aria-label
Accessible name.
string'Reload'

WebPreviewOpen

A ghost icon ButtonLink to the page in a new tab. Takes ButtonLink’s props.

PropTypeDefault
aria-label
Accessible name.
string'Open in new tab'

WebPreviewFrame

The viewport and its <iframe>. Props other than children and sx go to the iframe.

PropTypeDefault
title
Names the frame for screen readers. Required.
string—
sandbox
Frame restrictions. With both scripts and same-origin, a same-origin page can lift its own sandbox.
string'allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox'
referrerPolicy
Keeps the app’s URL from the framed site.
string'no-referrer'
children
Overlays: WebPreviewLoading and WebPreviewFallback.
ReactNode—
sx
StyleX styles for the viewport, merged last.
StyleXStyles—

WebPreviewLoading

Covers the frame during the first load. Children replace the label.

PropTypeDefault
render
Replaces the rendered element, keeping the styles.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

WebPreviewFallback

Covers the frame when the page is blocked or timed out.

PropTypeDefault
children
Replaces the default message and link.
ReactNode | ((state: WebPreviewState) => ReactNode)—
render
Replaces the rendered element, keeping the styles.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

WebPreviewStats

A <div> bar under the page with a live timer, then the load time and size.

PropTypeDefault
children
Replaces the default text.
ReactNode | ((state: WebPreviewState) => ReactNode)—
render
Replaces the rendered element, keeping the styles.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—