Install grapesjs-mjml via npm
masterTo use MJML components within GrapesJS, install the plugin using npm. Note that this plugin requires GrapesJS v0.15.9 or higher.
npm i grapesjs-mjmlrepository·master·Indexed 20 days ago
https://github.com/grapesjs/mjmlA GrapesJS plugin that integrates MJML (Mailjet Markup Language) components, providing a real-time visual editor for building responsive email templates. It supports the official v4 compiler, custom MJML parsers, and a set of specialized components including mj-body, mj-button, mj-column, mj-divider, mj-navbar-link, and mj-text. The plugin includes commands for retrieving MJML code, converting MJML to HTML, and switching between desktop, tablet, and mobile device views.
To use MJML components within GrapesJS, install the plugin using npm. Note that this plugin requires GrapesJS v0.15.9 or higher.
npm i grapesjs-mjmlYou can integrate the plugin using either standard script tags or ESM imports. The plugin enables real-time rendering of MJML components using the official v4 compiler.
import 'grapesjs/dist/css/grapes.min.css'
import grapesJS from 'grapesjs'
import grapesJSMJML from 'grapesjs-mjml'
grapesJS.init({
fromElement: true,
container: '#gjs',
plugins: [grapesJSMJML],
pluginsOpts: {
[grapesJSMJML]: {/* ...options */}
},
});In the grapesjs-mjml plugin, MJML components are built using a dual-layer approach consisting of a coreMjmlModel and a coreMjmlView.
The model manages the component's state, specifically its MJML attributes and styles. It synchronizes GrapesJS style and attributes so that changes to one reflect in the other. It also handles the conversion of component data into valid MJML XML attributes via getMjmlAttributes() and provides a toHTML() method for direct HTML generation.
Because MJML must be compiled to produce valid HTML, the view is responsible for the compilation lifecycle:
getMjmlTemplate() (for the full document wrapper) and getInnerMjmlTemplate() (for the specific component's XML tag and attributes).getTemplateFromMjml() to wrap the component in an <mjml> document, runs it through the mjmlParser, and extracts the resulting HTML.render() method updates the element's inner HTML with the compiled output, renders child components, and applies styles.When creating custom MJML components, you can override these helpers to change how your component's XML is structured or how it is compiled.
// Conceptual overview of the model/view relationship
// The model handles: attributes, styles, and MJML XML generation
// The view handles: MJML compilation, template wrapping, and DOM renderingThe grapesjs-mjml plugin accepts several configuration options to customize the MJML experience. Key options include:
blocks: Which blocks to add (default: all).block: Function to add custom block options based on block ID: (blockId) => ({}).mjmlParser: Custom mjml-browser instance to extend MJML functionality or add custom components.customComponents: List of components to be added to the default set.useXmlParser: Experimental option to use an XML parser instead of HTML, allowing void MJML elements like <mj-image/>.columnsPadding: Sets column padding to make selection easier (default: '10px 0').overwriteExport: Whether to overwrite the default export command (default: true).resetBlocks, resetDevices, resetStyleManager: Boolean flags to clean/reset specific parts of the editor (default: true).The grapesjs-mjml plugin integrates MJML support into the GrapesJS editor. It provides MJML-specific blocks, components, commands, and panels. You can use it by passing the plugin function to the GrapesJS editor initialization.
import grapesjs from 'grapesjs';
import mjmlPlugin from 'grapesjs-mjml';
const editor = grapesjs.init({
container: '#gjs',
plugins: [mjmlPlugin],
pluginsOpts: {
[mjmlPlugin]: {
// Your plugin options here
}
}
});To localize the MJML plugin, pass an i18n object within the pluginsOpts for grapesjs-mjml.
import 'grapesjs/dist/css/grapes.min.css'
import grapesJS from 'grapesjs'
import nl from 'grapesjs/locale/nl'
import grapesJSMJML from 'grapesjs-mjml'
import mjmlNL from 'grapesjs-mjml/locale/nl'
grapesJS.init({
fromElement: true,
container: '#gjs',
i18n: {
messages: { nl: nl },
},
plugins: [grapesJSMJML],
pluginsOpts: {
[grapesJSMJML]: {
i18n: { nl: mjmlNL }
}
},
});If you have a custom version of MJML with extended components, you can override the default mjmlParser and provide a list of customComponents via the plugin options.
import 'grapesjs/dist/css/grapes.min.css'
import grapesJS from 'grapesjs'
import grapesJSMJML from 'grapesjs-mjml'
import customMjmlParser from 'custom-mjml-parser';
import customImage from 'custom/components/path'
grapesJS.init({
fromElement: true,
container: '#gjs',
plugins: [grapesJSMJML],
pluginsOpts: {
[grapesJSMJML]: {
mjmlParser: customMjmlParser,
customComponents: [
customImage,
]
}
},
});When initializing the GrapesJS MJML plugin, you can pass a PluginOptions object to customize its behavior. This includes controlling which blocks are loaded, adding custom MJML components, configuring the parser, and managing how the editor handles MJML-specific elements like fonts and themes.
Key configuration areas include:
blocks to specify a subset of blocks or block to provide a function that returns custom options for specific block IDs.customComponents to register your own MJML components with the editor.preMjml and postMjml, or set an importPlaceholder for the import modal.mjmlParser instance if you are not using the default mjml-browser build.fonts for the exported HTML header and columnsPadding to improve column selection in the editor.// Example configuration for the MJML plugin
{
blocks: ['mj-hero', 'mj-text'],
block: (blockId) => (blockId === 'mj-hero' ? { attributes: { custom: 'value' } } : {}),
customComponents: [(editor, componentOptions) => {
// Logic to add custom MJML components
}],
fonts: {
Montserrat: 'https://fonts.googleapis.com/css?family=Montserrat',
'Open Sans': 'https://fonts.googleapis.com/css?family=Open+Sans'
},
useXmlParser: true // Experimental: allows importing void elements like <mj-image/>
}When initializing the plugin, you can pass a PluginOptions object to customize its behavior.
Key configuration options include:
blocks: An array of MJML block identifiers to include (e.g., 'mj-text', 'mj-image').customComponents: An array of custom MJML components to register.mjmlParser: The function used to parse MJML into HTML (defaults to mjml2html).useXmlParser: If true, configures the editor's parser to use text/xml.hideSelector: If true, hides the default selector manager.useCustomTheme: If true (and running in a browser), injects a default CSS theme for the plugin UI.i18n: An object containing translation messages to extend or override the default English locale.resetBlocks, resetStyleManager, resetDevices: Boolean flags to determine if the plugin should reset these editor features.const pluginOptions = {
blocks: ['mj-text', 'mj-button'], // Only include specific blocks
useXmlParser: true,
hideSelector: true,
i18n: {
// Custom translations
}
};The mj-navbar-link component is designed to be used within an mj-navbar context. It renders as an <a> tag with the following default inline styles:
float: none;display: inline-table;It can contain children such as <a> or <p> elements.
The mj-navbar-link component is an MJML component used for navigation links within an mj-navbar.
You can apply the following styles to this component:
font-style, font-size, font-weight, font-family, color, text-decoration, text-transform.padding, padding-top, padding-left, padding-right, padding-bottom.Default Styles:
font-size: 13pxpadding-top: 25pxpadding-bottom: 25pxpadding-left: 10pxpadding-right: 10pxtext-transform: uppercasehref: Used to define the link destination.When calling the command to convert MJML to HTML, you can pass CommandOptionsMjmlToHtml to specify the MJML source and other parsing options inherited from MJMLParsingOptions.
// Example usage of command options
// Note: This is a type definition for the options object passed to the command
const options: CommandOptionsMjmlToHtml = {
mjml: '<mjml>...</mjml>',
// ... other MJMLParsingOptions
};