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
- Give the root a height; the frame fills what the toolbar leaves.
- Check whether a page can be framed before showing it, and pass
blockedfor 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
sandboxto the least the page needs, especially for pages you don’t control.
- 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.
| Prop | Type | Default |
|---|---|---|
srcThe page to show. | string | — |
blockedThe page refuses to be framed. Shows the fallback and loads nothing. | boolean | false |
timeoutMilliseconds to wait for load before showing the fallback. | number | 10000 |
onStatusChangeCalled whenever the status changes. | (status: WebPreviewStatus, state: WebPreviewState) => void | — |
renderReplaces the rendered element, keeping the styles. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
WebPreviewToolbar
A <div> row above the page.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, keeping the styles. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
WebPreviewAddress
The address, host in full and path dimmed. Children replace it.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, keeping the styles. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
WebPreviewReload
A ghost icon Button that reloads the page. Takes Button’s props.
| Prop | Type | Default |
|---|---|---|
aria-labelAccessible name. | string | 'Reload' |
WebPreviewOpen
A ghost icon ButtonLink to the page in a new tab. Takes ButtonLink’s props.
| Prop | Type | Default |
|---|---|---|
aria-labelAccessible name. | string | 'Open in new tab' |
WebPreviewFrame
The viewport and its <iframe>. Props other than children and sx go to the iframe.
| Prop | Type | Default |
|---|---|---|
titleNames the frame for screen readers. Required. | string | — |
sandboxFrame 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' |
referrerPolicyKeeps the app’s URL from the framed site. | string | 'no-referrer' |
childrenOverlays: WebPreviewLoading and WebPreviewFallback. | ReactNode | — |
sxStyleX styles for the viewport, merged last. | StyleXStyles | — |
WebPreviewLoading
Covers the frame during the first load. Children replace the label.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, keeping the styles. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
WebPreviewFallback
Covers the frame when the page is blocked or timed out.
| Prop | Type | Default |
|---|---|---|
childrenReplaces the default message and link. | ReactNode | ((state: WebPreviewState) => ReactNode) | — |
renderReplaces the rendered element, keeping the styles. | ReactElement | function | — |
sxStyleX 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.
| Prop | Type | Default |
|---|---|---|
childrenReplaces the default text. | ReactNode | ((state: WebPreviewState) => ReactNode) | — |
renderReplaces the rendered element, keeping the styles. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |