Web Preview

A composable component for previewing the result of a generated UI, with support for live examples and code display.

The WebPreview 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.

Install using CLI

AI Elements Vue
shadcn-vue CLI
npx ai-elements-vue@latest add web-preview

Install Manually

WebPreview.vue
WebPreviewBody.vue
WebPreviewConsole.vue
WebPreviewNavigation.vue
WebPreviewNavigationButton.vue
WebPreviewUrl.vue
context.ts
index.ts
<script setup lang="ts">
import type { HTMLAttributes } from 'vue'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { computed, ref } from 'vue'
import {
  provideWebPreviewContext,
} from './context'

interface Props extends /* @vue-ignore */ HTMLAttributes {
  class?: HTMLAttributes['class']
  defaultUrl?: string
}

const props = withDefaults(defineProps<Props>(), {
  defaultUrl: '',
})

const emit = defineEmits<{
  (e: 'update:url', value: string): void
  (e: 'urlChange', value: string): void
  (e: 'update:consoleOpen', value: boolean): void
  (e: 'consoleOpenChange', value: boolean): void
}>()

const url = ref(props.defaultUrl)
const consoleOpen = ref(false)

function setUrl(value: string) {
  url.value = value
  emit('update:url', value)
  emit('urlChange', value)
}

function setConsoleOpen(value: boolean) {
  consoleOpen.value = value
  emit('update:consoleOpen', value)
  emit('consoleOpenChange', value)
}

provideWebPreviewContext({
  url,
  setUrl,
  consoleOpen,
  setConsoleOpen,
})

const vBind = computed(() => {
  const { class: _, ...rest } = props
  return {
    class: cn('flex size-full flex-col rounded-lg border bg-card', props.class),
    ...rest,
  }
})
</script>

<template>
  <div v-bind="vBind">
    <slot />
  </div>
</template>

Usage with AI SDK

Build a simple v0 clone using the v0 Platform API.

Install the v0-sdk package:

npm
pnpm
bun
yarn
npm i v0-sdk

Add the following component to your frontend:

app.vue
<script setup lang="ts">
import { Loader } from '@/components/ai-elements/loader'
import {
  Input,
  PromptInputSubmit,
  PromptInputTextarea,
} from '@/components/ai-elements/prompt-input'
import {
  WebPreview,
  WebPreviewBody,
  WebPreviewNavigation,
  WebPreviewUrl,
} from '@/components/ai-elements/web-preview'

const previewUrl = ref('')
const prompt = ref('')
const isGenerating = ref(false)

async function handleSubmit(e: Event) {
  e.preventDefault()
  if (!prompt.value.trim())
    return
  prompt.value = ''

  isGenerating.value = true
  try {
    const response = await fetch('/api/v0', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ prompt: prompt.value }),
    })

    const data = await response.json()
    previewUrl.value = data.demo || '/'
    console.log('Generation finished:', data)
  }
  catch (error) {
    console.error('Generation failed:', error)
  }
  finally {
    isGenerating.value = false
  }
}
</script>

<template>
  <div class="max-w-4xl mx-auto p-6 relative size-full rounded-lg border h-[600px]">
    <div class="flex flex-col h-full">
      <div class="flex-1 mb-4">
        <div v-if="isGenerating" class="flex flex-col items-center justify-center h-full">
          <Loader />
          <p v-if="isGenerating" class="mt-4 text-muted-foreground">
            Generating app, this may take a few seconds...
          </p>
        </div>
        <WebPreview v-else-if="previewUrl" :default-url="previewUrl">
          <WebPreviewNavigation>
            <WebPreviewUrl />
          </WebPreviewNavigation>
          <WebPreviewBody :src="previewUrl" />
        </WebPreview>
        <div v-else class="flex items-center justify-center h-full text-muted-foreground">
          Your generated app will appear here
        </div>
      </div>

      <Input
        class="w-full max-w-2xl mx-auto relative"
        @submit="handleSubmit"
      >
        <PromptInputTextarea
          :value="prompt"
          placeholder="Describe the app you want to build..."
          class="pr-12 min-h-[60px]"
          @change="(e: any) => (prompt = e?.target?.value ?? '')"
        >
          <PromptInputSubmit
            :status="isGenerating ? 'streaming' : 'ready'"
            :disabled="!prompt.trim()"
            class="absolute bottom-1 right-1"
          />
        </PromptInputTextarea>
      </Input>
    </div>
  </div>
</template>

Add the following route to your backend:

server/api/v0.post.ts
import type { ChatsCreateResponse } from 'v0-sdk'
import { defineEventHandler, readBody } from 'h3'
import { v0 } from 'v0-sdk'

export default defineEventHandler(async (event) => {
  const { prompt }: { prompt: string } = await readBody(event)
  const result = await v0.chats.create({
    system: 'You are an expert coder',
    message: prompt,
    modelConfiguration: {
      modelId: 'v0-1.5-sm',
      imageGenerations: false,
      thinking: false,
    },
  }) as ChatsCreateResponse

  return {
    demo: result.demo,
    webUrl: result.webUrl,
  }
})

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 />

defaultUrlstring
''
The initial URL to load in the preview.
@urlChange(url: string) => void
Callback fired when the URL changes.
...propsHTMLAttributes
Any other props are spread to the root div.

<WebPreviewNavigation />

...propsHTMLAttributes
Any other props are spread to the navigation container.

<WebPreviewNavigationButton />

tooltipstring
Tooltip text to display on hover.
...propstypeof Button
Any other props are spread to the underlying shadcn-vue/ui Button component.

<WebPreviewUrl />

...propstypeof Input
Any other props are spread to the underlying shadcn-vue/ui Input component.

<WebPreviewBody />

loadingSlot
Optional loading indicator to display over the preview.
...propsIframeHTMLAttributes
Any other props are spread to the underlying iframe.

<WebPreviewConsole />

logsArray<LogItem>
Console log entries to display in the console panel.
LogItem
type LogItem = { level: "log" | "warn" | "error"; message: string; timestamp: Date }
Example
[
  {
    "level": "log",
    "message": "Page loaded successfully",
    "timestamp": "2025-01-01T00:00:00.000Z"
  }
]
...propsHTMLAttributes
Any other props are spread to the root div.