@mmuneebahmad/unlayer-svelte-wrapper
Version:
A Svelte wrapper component for the Unlayer email editor
254 lines (191 loc) โข 5.84 kB
Markdown
A modern, TypeScript-first Svelte wrapper for the [Unlayer](https://unlayer.com/) email editor.
- ๐ **Easy Integration** - Drop-in Svelte component
- ๐ **TypeScript Support** - Full type definitions included
- ๐จ **Customizable** - Support for all Unlayer configuration options
- ๐ฑ **Responsive** - Fills parent container width/height
- ๐ **Event-Driven** - Reactive design updates and exports
- ๐งช **Well Tested** - Comprehensive test coverage
- ๐ฆ **Tree Shakeable** - Import only what you need
```bash
npm install @mmuneebahmad/unlayer-svelte-wrapper
```
```svelte
<script>
import { UnlayerEditor } from '@mmuneebahmad/unlayer-svelte-wrapper';
let editor;
let isEditorReady = false;
function handleEditorLoaded(event) {
isEditorReady = true;
console.log('Editor loaded:', event.detail.editor);
}
async function exportDesign() {
if (editor && isEditorReady) {
const data = await editor.exportHtml();
console.log('Exported:', data);
}
}
</script>
<div style="height: 600px;">
<UnlayerEditor
bind:this={editor}
options={{ displayMode: 'email' }}
on:loaded={handleEditorLoaded}
/>
</div>
<button on:click={exportDesign} disabled={!isEditorReady}>
Export Design
</button>
```
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `design` | `UnlayerDesign \| null` | `null` | Initial design JSON to load |
| `tools` | `Record<string, any> \| null` | `null` | Tool whitelist/blacklist configuration |
| `options` | `UnlayerOptions` | `{}` | Unlayer initialization options |
| Event | Payload | Description |
|-------|---------|-------------|
| `loaded` | `{ editor: UnlayerInstance }` | Fired when editor is ready |
| `design-updated` | `UnlayerDesign` | Fired when design changes |
| `export-html` | `ExportData` | Fired when HTML is exported |
| Method | Parameters | Returns | Description |
|--------|------------|---------|-------------|
| `exportHtml()` | `dispatchEvent?: boolean` | `Promise<ExportData>` | Export current design as HTML |
| `loadDesign()` | `design: UnlayerDesign` | `void` | Load a design into the editor |
| `saveDesign()` | - | `Promise<UnlayerDesign>` | Get current design JSON |
| `isReady()` | - | `boolean` | Check if editor is ready |
| `getEditor()` | - | `UnlayerInstance \| null` | Get raw editor instance |
A pre-built modal for handling design exports.
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `show` | `boolean` | `false` | Whether modal is visible |
| `exportData` | `ExportData \| null` | `null` | Export data to display |
| Event | Payload | Description |
|-------|---------|-------------|
| `close` | `void` | Fired when modal is closed |
| `download` | `DownloadEvent` | Fired when download is triggered |
```svelte
<script>
import { UnlayerEditor } from '@mmuneebahmad/unlayer-svelte-wrapper';
const editorOptions = {
displayMode: 'email',
projectId: 1234,
locale: 'en-US',
appearance: {
theme: 'light',
panels: {
tools: {
dock: 'left'
}
}
},
tools: {
enabled: ['text', 'image', 'button'],
disabled: ['video']
},
mergeTags: [
{
name: 'First Name',
value: '{{first_name}}',
sample: 'John'
}
]
};
</script>
<UnlayerEditor options={editorOptions} />
```
```svelte
<script>
import { UnlayerEditor, ExportModal } from '@mmuneebahmad/unlayer-svelte-wrapper';
let editor;
let showExportModal = false;
let exportData = null;
async function handleExport() {
if (editor) {
exportData = await editor.exportHtml();
showExportModal = true;
}
}
</script>
<UnlayerEditor bind:this={editor} />
<button on:click={handleExport}>Export</button>
<ExportModal
bind:show={showExportModal}
{exportData}
on:download={(e) => console.log('Downloaded:', e.detail)}
/>
```
```svelte
<script>
import { UnlayerEditor, createSampleDesign } from '@mmuneebahmad/unlayer-svelte-wrapper';
const initialDesign = createSampleDesign();
</script>
<UnlayerEditor design={initialDesign} />
```
The package includes comprehensive TypeScript definitions:
```typescript
import type {
UnlayerOptions,
UnlayerDesign,
ExportData,
EditorLoadedEvent
} from '@mmuneebahmad/unlayer-svelte-wrapper';
const options: UnlayerOptions = {
displayMode: 'email',
projectId: 123
};
function handleLoaded(event: CustomEvent<EditorLoadedEvent>) {
const editor = event.detail.editor;
// editor is fully typed
}
```
The component fills its parent container. Ensure the parent has defined dimensions:
```svelte
<div class="editor-container">
<UnlayerEditor />
</div>
<style>
.editor-container {
width: 100%;
height: 600px; /* or 100vh for full height */
}
</style>
```
The component includes built-in error handling with retry functionality:
```svelte
<UnlayerEditor
on:loaded={handleLoaded}
on:error={handleError}
/>
```
- Modern browsers with ES2020+ support
- Svelte 4.0+ or 5.0+
- TypeScript 5.0+ (optional)
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
MIT ยฉ [Muneeb Ahmad]
## Changelog
See [CHANGELOG.md](CHANGELOG.md) for details.