unlayer-svelte-sdk Svelte Themes

Unlayer Svelte Sdk

Lightweight Svelte 5 + TypeScript wrapper for the Unlayer embeddable email editor, with a demo app and CDN fallback support.

Unlayer Svelte SDK

A lightweight, production-ready Svelte wrapper around Unlayer's embeddable email editor. Built with Svelte 5, TypeScript, and modern development practices.

โœจ Features

  • ๐Ÿš€ Svelte 5 Native: Built with latest Svelte 5 syntax and features
  • ๐ŸŽจ Full Editor Integration: Complete access to Unlayer's drag-and-drop email editor
  • ๐Ÿ“ฑ Responsive Design: Automatically adapts to parent container dimensions
  • ๐Ÿ”ง TypeScript First: Full type safety with comprehensive interfaces
  • ๐Ÿ“ค Export Capabilities: Export HTML and design JSON with ease
  • ๐ŸŽฏ Event-Driven: Comprehensive event system for editor interactions
  • ๐Ÿงน Memory Safe: Proper cleanup and lifecycle management
  • ๐Ÿ“ฆ NPM Ready: Production-ready package with proper exports

๐Ÿš€ Quick Start

Installation

npm install unlayer-svelte-sdk

Basic Usage

<script lang="ts">
  import UnlayerEditor from 'unlayer-svelte-sdk';
</script>

<UnlayerEditor />

With Design and Events

<script lang="ts">
  import UnlayerEditor from 'unlayer-svelte-sdk';
  import type { Design } from 'unlayer-svelte-sdk';
  
  let initialDesign: Design = {
    body: {
      rows: [
        {
          cells: [
            {
              content: {
                blocks: [
                  {
                    type: "text",
                    data: {
                      text: "Hello World!",
                      textAlign: "center",
                      fontSize: "24px"
                    }
                  }
                ]
              }
            }
          ]
        }
      ]
    }
  };

  function handleExport(event: CustomEvent<{ html: string; design: Design }>) {
    const { html, design } = event.detail;
    console.log('HTML:', html);
    console.log('Design:', design);
  }
</script>

<UnlayerEditor
  design={initialDesign}
  on:export-html={handleExport}
/>

With Methods

<script lang="ts">
  import UnlayerEditor from 'unlayer-svelte-sdk';
  
  let editorRef: UnlayerEditor;
  
  async function exportHtml() {
    const result = await editorRef.exportHtml();
    console.log(result.html);
  }
  
  function loadDesign(design: Design) {
    editorRef.loadDesign(design);
  }
</script>

<UnlayerEditor bind:this={editorRef} />

๐Ÿ“š API Reference

Props

Prop Type Default Description
design Design undefined Initial design JSON to load into the editor
tools ToolConfig undefined Tool whitelist/blacklist configuration
options UnlayerOptions {} Unlayer initialization options
autoLoad boolean true Automatically load design on mount and prop changes
class string '' CSS class for styling

Events

Event Detail Description
loaded { editor: UnlayerEditor } Fired when the editor is ready
design-updated { design: Design } Fired when design changes
export-html { html: string; design: Design } Fired when HTML is exported
error { message: string } Fired when editor encounters an error

Methods

The component exposes the following methods via binding:

interface UnlayerEditorMethods {
  exportHtml(): Promise<{ html: string; design: Design }>;
  loadDesign(newDesign: Design): void;
  saveDesign(): Promise<Design>;
  retryLoad(): void;
}

โš™๏ธ Configuration

Basic Options

<UnlayerEditor
  options={{
    displayMode: 'email',
    locale: 'en-US',
    features: {
      preview: true,
      imageEditor: true,
      stockImages: true
    }
  }}
/>

Tool Configuration

<UnlayerEditor
  tools={{
    image: { enabled: true, position: 1 },
    text: { enabled: true, position: 2 },
    button: { enabled: true, position: 3 }
  }}
/>

Advanced Configuration

<UnlayerEditor
  options={{
    customCSS: ['.custom-class { color: red; }'],
    customJS: ['console.log("Editor loaded!");'],
    features: {
      textEditor: {
        spellChecker: true,
        thesaurus: true
      }
    }
  }}
/>

๐Ÿ—๏ธ Architecture

Component Structure

UnlayerEditor.svelte
โ”œโ”€โ”€ Props Interface (design, tools, options, autoLoad, class)
โ”œโ”€โ”€ Event Dispatcher (loaded, design-updated, export-html, error)
โ”œโ”€โ”€ Internal State Management
โ”œโ”€โ”€ Unlayer Script Loading (CDN with fallbacks)
โ”œโ”€โ”€ Editor Initialization
โ”œโ”€โ”€ Method Exports (exportHtml, loadDesign, saveDesign, retryLoad)
โ””โ”€โ”€ Lifecycle Management (onMount, onDestroy)

Key Features

  1. Dynamic Script Loading: Automatically loads Unlayer from CDN with fallbacks
  2. Type Safety: Full TypeScript support with proper interfaces
  3. Event System: Comprehensive event handling for all editor interactions
  4. Method Exposure: Parent components can call editor methods
  5. Memory Management: Proper cleanup to prevent memory leaks
  6. Error Handling: Graceful fallbacks for network issues

๐Ÿงช Development

Prerequisites

  • Node.js 18+
  • npm or yarn

Setup

git clone <repository-url>
cd unlayer-svelte-sdk
npm install

Scripts

  • npm run dev - Start demo development server
  • npm run build - Build library for distribution
  • npm run build:demo - Build demo application
  • npm run check - Type checking and validation
  • npm run package - Create NPM package

Project Structure

unlayer-svelte-sdk/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ lib/
โ”‚   โ”‚   โ””โ”€โ”€ UnlayerEditor.svelte    # Main component
โ”‚   โ”œโ”€โ”€ types/
โ”‚   โ”‚   โ””โ”€โ”€ unlayer.d.ts            # Type definitions
โ”‚   โ”œโ”€โ”€ assets/
โ”‚   โ”‚   โ””โ”€โ”€ welcome.json            # Sample design
โ”‚   โ”œโ”€โ”€ App.svelte                  # Demo application
โ”‚   โ”œโ”€โ”€ index.ts                    # Package exports
โ”‚   โ””โ”€โ”€ main.ts                     # Demo entry point
โ”œโ”€โ”€ dist/                           # Library build output
โ”œโ”€โ”€ dist-demo/                      # Demo build output
โ”œโ”€โ”€ package.json                    # Package configuration
โ”œโ”€โ”€ vite.config.ts                  # Library build config
โ”œโ”€โ”€ vite.demo.config.ts             # Demo build config
โ”œโ”€โ”€ tsconfig.build.json             # Library TypeScript config
โ””โ”€โ”€ README.md                       # Documentation

๐ŸŒŸ Best Practices

1. Error Handling

<script lang="ts">
  function handleError(event: CustomEvent<{ message: string }>) {
    console.error('Editor error:', event.detail.message);
    // Show user-friendly error message
  }
</script>

<UnlayerEditor on:error={handleError} />

2. Loading States

<script lang="ts">
  let isEditorReady = false;
  
  function handleLoaded(event: CustomEvent<{ editor: any }>) {
    isEditorReady = true;
  }
</script>

{#if isEditorReady}
  <UnlayerEditor on:loaded={handleLoaded} />
{:else}
  <div>Loading editor...</div>
{/if}

3. Design Management

<script lang="ts">
  let currentDesign: Design;
  
  function handleDesignUpdate(event: CustomEvent<{ design: Design }>) {
    currentDesign = event.detail.design;
    // Save to localStorage or send to server
  }
</script>

<UnlayerEditor
  design={currentDesign}
  on:design-updated={handleDesignUpdate}
/>

๐Ÿš€ Deployment

Demo Application

The repository includes a complete demo application showcasing all features:

  1. Editor Integration: Full Unlayer editor with sample design
  2. Design Management: Load, save, and export designs
  3. HTML Export: Export and preview generated HTML
  4. Responsive Layout: Mobile-friendly interface
  5. Error Handling: Graceful error handling and user feedback

Build for Production

# Build the library
npm run build

# Build the demo
npm run build:demo

# Create NPM package
npm run package

๐Ÿ“Š Performance

Bundle Sizes

  • Library: ~33KB (gzipped: ~10KB)
  • Demo: ~28KB (gzipped: ~11KB)
  • Total package: ~21KB

Build Times

  • Library build: ~320ms
  • Demo build: ~259ms
  • Package creation: ~1s

๐ŸŒ Browser Support

  • Chrome 80+
  • Firefox 75+
  • Safari 13+
  • Edge 80+

๐Ÿ”’ Security

  • CSP compliant CDN loading
  • No external dependencies bundled
  • Secure event handling
  • Memory leak prevention

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow TypeScript best practices
  • Use Svelte 5 syntax and patterns
  • Write clear, documented code
  • Include tests for new features
  • Update documentation as needed

๐Ÿ†˜ Support

Getting Help

Common Issues

  • CDN Loading Failed: Check network connectivity and try refreshing
  • TypeScript Errors: Ensure you're using Svelte 5+ and TypeScript 5+
  • Build Issues: Clear node_modules and reinstall dependencies

๐Ÿ™ Acknowledgments

  • Unlayer for the amazing email editor
  • Svelte team for the incredible framework
  • Vite for the fast build tooling

Built with โค๏ธ using Svelte 5 and Unlayer Email Editor

This project follows the Conventional Commits specification for commit messages.

Top categories

Loading Svelte Themes