CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-vue--repl

Vue component for editing Vue components with interactive REPL functionality.

Pending
Overview
Eval results
Files

preview-system.mddocs/

Preview System

The preview system provides standalone components for displaying compiled Vue components in a sandboxed iframe environment. It includes runtime error handling, theme support, and SSR rendering capabilities.

Capabilities

Sandbox Component

Standalone sandbox component for preview-only mode without the editor interface.

/**
 * Standalone sandbox component for preview-only mode
 * Provides iframe-based Vue component execution with error handling
 * @param props - Configuration options for the sandbox
 */
interface SandboxProps {
  /** Store instance containing files and compilation state */
  store: Store;
  /** Whether to display the sandbox (useful for mobile responsive behavior) */
  show?: boolean;
  /** Enable server-side rendering mode */
  ssr?: boolean;
  /** Clear console output on file changes */
  clearConsole?: boolean;
  /** Theme for the preview iframe */
  theme?: 'dark' | 'light';
  /** Preview customization options */
  previewOptions?: PreviewOptions;
  /** Whether to automatically initialize the store (default: true) */
  autoStoreInit?: boolean;
}

interface PreviewOptions {
  /** Custom HTML to inject into iframe head section */
  headHTML?: string;
  /** Custom HTML to inject into iframe body section */
  bodyHTML?: string;
  /** HTML content to show while preview is loading */
  placeholderHTML?: string;
  /** Advanced code injection options */
  customCode?: {
    /** Custom import statements for the preview context */
    importCode?: string;
    /** Custom code to execute in the preview environment */
    useCode?: string;
  };
  /** Whether to display runtime JavaScript errors */
  showRuntimeError?: boolean;
  /** Whether to display runtime console warnings */
  showRuntimeWarning?: boolean;
}

Usage Examples:

import { Sandbox, useStore } from "@vue/repl";

// Basic sandbox usage
<template>
  <Sandbox :store="store" />
</template>

<script setup>
const store = useStore({}, location.hash);
</script>

// Advanced sandbox with custom options
<template>
  <Sandbox 
    :store="store"
    theme="dark"
    :clearConsole="true"
    :previewOptions="{
      headHTML: '<meta name=\"viewport\" content=\"width=device-width\">',
      bodyHTML: '<div id=\"app\"></div>',
      showRuntimeError: true,
      showRuntimeWarning: false,
      customCode: {
        importCode: 'import { createApp } from \"vue\";',
        useCode: 'app.config.errorHandler = (err) => console.error(err);'
      }
    }"
  />
</template>

Preview Component

Wrapper around Sandbox component used within the full REPL interface.

/**
 * Preview wrapper component used within REPL interface
 * Provides theme integration and reload functionality
 * @param props - Preview configuration
 */
interface PreviewProps {
  /** Whether to show the preview panel */
  show: boolean;
  /** Enable server-side rendering mode */
  ssr: boolean;
}

Usage Example:

// Used internally by Repl component
<Preview :show="showPreview" :ssr="ssrMode" />

Sandbox Methods

Methods exposed by the Sandbox component for programmatic control.

/**
 * Reload the preview iframe, clearing all runtime state
 */
function reload(): void;

/**
 * Get reference to the preview container DOM element
 * @returns HTMLDivElement container or null
 */
readonly container: ComputedRef<HTMLDivElement | null>;

Usage Example:

<template>
  <Sandbox ref="sandbox" :store="store" />
  <button @click="$refs.sandbox.reload()">Reload Preview</button>
</template>

<script setup>
import { ref } from 'vue';

const sandbox = ref();

// Access container element
const containerEl = computed(() => sandbox.value?.container);
</script>

Runtime Error Handling

The preview system includes comprehensive error handling for runtime issues.

/**
 * Runtime error information captured in preview
 */
interface RuntimeError {
  /** Error message */
  message: string;
  /** Stack trace if available */
  stack?: string;
  /** Source filename */
  filename?: string;
  /** Line number */
  lineno?: number;
  /** Column number */
  colno?: number;
}

/**
 * Runtime warning information
 */
interface RuntimeWarning {
  /** Warning message */
  message: string;
  /** Warning type/category */
  type: string;
}

Error Display:

Runtime errors and warnings are displayed in the preview when enabled:

// Enable error display
<Sandbox 
  :store="store"
  :previewOptions="{
    showRuntimeError: true,
    showRuntimeWarning: true
  }"
/>

SSR Support

Server-side rendering support for testing SSR-compatible Vue components.

/**
 * SSR output structure
 */
interface SSROutput {
  /** Rendered HTML string */
  html: string;
  /** SSR context object */
  context: unknown;
}

Usage Example:

// Enable SSR mode
<template>
  <Sandbox :store="store" :ssr="true" />
</template>

// Access SSR output from store
<script setup>
const store = useStore();
console.log(store.ssrOutput.html); // Server-rendered HTML
</script>

Preview Iframe Management

The preview system manages iframe creation, communication, and lifecycle.

Iframe Features:

  • Sandboxed execution: Isolated JavaScript environment
  • Hot reloading: Automatic refresh on code changes
  • Console integration: Runtime logs displayed in REPL
  • Error boundaries: Runtime error capture and display
  • Module resolution: Dynamic import map support
  • Vue devtools: Development tools integration when available

Security Considerations:

  • All preview code runs in a sandboxed iframe
  • No access to parent window or external domains
  • Safe execution of untrusted user code
  • XSS protection through iframe isolation

Default Values

The Sandbox component provides sensible defaults:

  • show: true
  • ssr: false
  • theme: 'light'
  • clearConsole: true
  • previewOptions: {} (empty object)
  • autoStoreInit: true

Integration with Store

The preview system integrates closely with the store system:

  • File watching: Automatically recompiles and refreshes on file changes
  • Error reporting: Compilation errors displayed in preview
  • Import map support: Dynamic module resolution using store configuration
  • Vue version switching: Automatic Vue runtime updates
  • State persistence: Preview state maintained across store serialization

Install with Tessl CLI

npx tessl i tessl/npm-vue--repl

docs

editor-components.md

file-system.md

import-map-management.md

index.md

preview-system.md

repl-component.md

store-management.md

tile.json