Web Preview

The Web Preview component provides a flexible way to showcase the result of a generated UI component, along with its source code. It is designed for documentation and demo purposes, allowing users to interact with live examples and view the underlying implementation.

Installation

pnpm dlx shadcn-svelte@latest add https://svelte-ai-elements.vercel.app/r/web-preview.json

If you prefer using jsrepo, please install via the command below:

pnpm dlx jsrepo add @ai/elements/ai-elements/web-preview

Usage

Usage with AI SDK

Build a simple v0-like clone that generates UI components and previews them in real-time using useChat and the v0-sdk.

First, install the v0-sdk package:

Add the following component to your frontend:

+page.svelte

Add the following route to your backend:

/api/chat/+server.ts

Features

  • Live preview of UI components
  • Composable architecture with dedicated sub-components
  • Responsive design modes (Desktop, Tablet, Mobile)
  • Navigation controls with back/forward functionality
  • URL input and example selector
  • Full screen mode support
  • Console logging with timestamps
  • Context-based state management
  • Consistent styling with the design system
  • Easy integration into documentation pages

Props

WebPreview

PropTypeDefaultDescription
defaultUrlstring""Default URL to display in the preview
onUrlChange(url: string) => void-Callback function called when the URL changes
classstring-Additional CSS classes to apply to the container
childrenSnippet-Child components (typically WebPreviewNavigation and WebPreviewBody)

WebPreviewNavigation

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the navigation bar
childrenSnippet-Navigation content (typically WebPreviewUrl and WebPreviewNavigationButton)

WebPreviewNavigationButton

PropTypeDefaultDescription
onclick(event: MouseEvent) => void-Click handler for the button
disabledboolean-Whether the button is disabled
tooltipstring-Tooltip text to display on hover
childrenSnippet-Button content (typically an icon)

WebPreviewUrl

PropTypeDefaultDescription
valuestring-Current URL value (bindable)
onchange(event: Event) => void-Change event handler
onkeydown(event: KeyboardEvent) => void-Keydown event handler (Enter key updates the preview URL)
classstring-Additional CSS classes to apply to the input

WebPreviewBody

PropTypeDefaultDescription
srcstring-URL to display in the iframe (overrides context URL)
loadingSnippet-Loading state content to display
classstring-Additional CSS classes to apply to the iframe

WebPreviewConsole

PropTypeDefaultDescription
logsLogEntry[]-Array of console log entries to display
classstring-Additional CSS classes to apply to the console container
childrenSnippet-Additional console content