Custom tools
ExtendingCustom tools
Register your own blocks. A custom tool appears in the Content panel like a built-in, renders on the canvas, is configured through inspector fields, and exports to email-safe HTML.
Register a tool (Unlayer's call)#
Call registerTool on the editor, the same call Unlayer uses. It works in react-email-craft (ref.current.editor), vue-email-craft (this.$refs.editor.editor), angular-email-craft (component.editor) and vanilla-email-craft (mailkaro.registerTool, also before mailkaro.init). A tool written for Unlayer needs no change:
const onReady = (editor) => {
editor.registerTool({
name: "product_card",
label: "Product",
icon: "https://cdn.example.com/icons/product.png", // a URL or an emoji; Font Awesome names get the default icon
supportedDisplayModes: ["web", "email"],
options: {
content: {
title: "Content",
position: 1,
options: {
title: { label: "Title", defaultValue: "Trail Runners", widget: "text" },
price: { label: "Price", defaultValue: "$120", widget: "text" },
color: { label: "Accent", defaultValue: "#0f766e", widget: "color_picker" },
photo: { label: "Photo", defaultValue: { url: "https://cdn.example.com/shoe.jpg" }, widget: "image" },
link: { label: "Link", defaultValue: { name: "web", values: { href: "https://shop.example.com", target: "_blank" } }, widget: "link" },
},
},
},
values: {},
renderer: {
// What the canvas shows (HTML).
Viewer: editor.createViewer({
render: (v) => `<div style="border:2px solid ${v.color};padding:12px"><img src="${v.photo.url}" width="120"> ${v.title} ${v.price}</div>`,
}),
// What the export and the sent email get (keep it table-based with inline styles).
exporters: {
email: (v) => `<table role="presentation" width="100%"><tr><td style="color:${v.color}">
<a href="${v.link.values.href}"><img src="${v.photo.url}" width="100%" alt=""></a>
<strong>${v.title}</strong> ${v.price}</td></tr></table>`,
web: (v) => `<div>${v.title}</div>`,
},
head: { css: () => "", js: () => "" },
},
});
};How it runs#
The editor runs in a frame on another origin, and functions cannot cross into it. So your tool's code stays on your page: the editor gets the plain part (name, label, icon, fields, default values) and, each time a block's values change, asks your page for its HTML. It keeps that HTML in the block (values._html), so exportHtml, saveDesign and a server render never wait for your page, and a design that uses the tool still renders where the tool is not registered.
- The canvas shows
renderer.Viewer.render(values)(or the export when there is no Viewer). HTML from your page is cleaned like an HTML block. - The exporter for the current display mode is used (
exporters.email,exporters.web, ...), falling back toemail. - Exporters may return a promise.
head.css(values)is put in a<style>in front of the block's HTML. - If an exporter throws, the block keeps its last good HTML.
- Registering the same
nameagain replaces the tool.registerToolreturns a promise that settles once the editor has the tool, and rejects for a config without aname. - The tool's code (your
customJSin Unlayer) does not run inside the frame: register it from your page.
Widgets#
Unlayer's property widgets map to the editor's fields. The exporter gets values in Unlayer's shape:
| Property | Controls | Default |
|---|---|---|
text | Single-line input | string |
rich_text, html, textarea | Multi-line input | string |
color_picker | Color picker | string |
toggle | Switch | boolean |
counter | Number input (data.min / data.max) | number |
dropdown | Dropdown (data.options) | string |
alignment | Left / Center / Right | string |
image | Upload field with a link box | { url, ... } |
link | Address input | { name, values: { href, target } } |
font_family | Text input | { label, value } |
Others with an object value (padding, border, ...) | Not shown; the default value is kept | as given |
MailKaro's own shape#
registerTool also takes MailKaro's tool object: id, label, icon, values, inspector fields and exportHtml(values). An optional render(values) that returns an HTML string is used for the canvas.
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | - | Unique id, stored on the block as values._tool. |
label | string | - | Name in the Content panel, on the block and in the inspector. |
icon | string | - | An image URL, or an emoji. |
description | string | - | Optional help text. |
values | Record<string, unknown> | - | Default values for a new block. |
inspector | ToolField[] | - | Fields (below). Omit for a view-only block. |
exportHtml | (values) => string | Promise<string> | - | Email HTML for export and the sent email. |
render | (values) => string | - | Optional canvas HTML. |
Each field binds a control to a key in the block's values:
| Property | Controls | Default |
|---|---|---|
type: text / url | Single-line input | , |
type: textarea | Multi-line input | , |
type: number | Numeric input (min / max) | , |
type: color | Color picker | , |
type: file | Upload button with a progress bar and cancel question, a drop area and a link box. The value is the URL; the name, type and size are saved as <key>_name, <key>_mime and <key>_size | , |
type: image | The same upload field, for images | , |
type: select | Dropdown (options: [{label, value}]) | , |
type: toggle | Boolean switch | , |
values._html. If you render on your own server and want fresh HTML, pass the same exporters to renderDesign via its tools option.