A lightweight Svelte wrapper for the Unlayer email editor - a powerful drag-and-drop email template builder.
# Via CDN (recommended for this implementation)
# The editor loads via CDN automatically
# For local development
git clone https://github.com/thealitahir/unlayer-svelte-sdk.git
cd unlayer-svelte-sdk
npm install
npm run dev
<script lang="ts">
import UnlayerEditor from './lib/UnlayerEditor.svelte';
import type { UnlayerDesign, HtmlExport } from './lib/types';
let editorRef: any;
let isReady = false;
function handleReady() {
isReady = true;
}
function handleExport(event: CustomEvent<HtmlExport>) {
const { html, design } = event.detail;
// Use the exported HTML and design data
}
async function exportEmail() {
if (editorRef && isReady) {
const result = await editorRef.exportHtml();
// Handle the exported result
}
}
</script>
<UnlayerEditor
bind:this={editorRef}
options={{ displayMode: 'email' }}
projectId={1234}
on:loaded={handleReady}
on:export-html={handleExport}
/>
<button on:click={exportEmail} disabled={!isReady}>
Export HTML
</button>
Prop | Type | Default | Description |
---|---|---|---|
design |
UnlayerDesign | null |
null |
Initial design JSON to load |
tools |
Record<string, any> | null |
null |
Custom tools configuration |
options |
UnlayerOptions |
{} |
Unlayer editor options |
projectId |
number |
1234 |
Unlayer project ID |
Event | Payload | Description |
---|---|---|
loaded |
void |
Fired when editor is ready |
design-updated |
UnlayerDesign |
Fired when design changes |
export-html |
HtmlExport |
Fired when HTML is exported |
Method | Parameters | Returns | Description |
---|---|---|---|
exportHtml() |
- | Promise<HtmlExport> |
Export current design as HTML |
loadDesign() |
design: UnlayerDesign |
void |
Load a design into editor |
saveDesign() |
- | Promise<UnlayerDesign> |
Save current design |
The demo includes:
npm run dev
Visit http://localhost:5173
to see the demo in action.
UnlayerEditor.svelte
component for simplicityProblem: Unlayer script needs to be loaded before initialization Solution: Implemented async script loading with polling mechanism
Problem: Default Vite CSS was constraining editor dimensions Solution: Identified and removed conflicting CSS rules in global styles
Problem: Bridging Unlayer callbacks with Svelte events
Solution: Used Svelte's createEventDispatcher
for clean event propagation
MIT License - feel free to use this in your projects!
Contributions welcome! Please feel free to submit issues and pull requests.
For questions about this implementation, please create an issue in the repository. For Unlayer-specific questions, visit Unlayer Documentation.