How to customize a component
Do this isComponent: el => (el.tagName || '').toLowerCase() === 'mj-body',
Read full answer below ↓Question
I read this document https://grapesjs.com/docs/modules/Components.html#define-custom-component-type and tried to write the following code. I want to make mj-body impossible to delete:
const myNewComponentTypes = editor => {
editor.DomComponents.addType("my-body-type",{
isComponent: el => el.tagName === 'mj-body',
model:{
default:{
removable:false,
tagName:'mj-body',
}
}
});
};
var editor = grapesjs.init({
container: '#gjs',
// Get the content for the canvas directly from the element
// As an alternative we could use: `components: '<h1>Hello World Component!</h1>'`,
fromElement: true,
// Size of the editor
height:600,
width: 'auto',
// Disable the storage manager for the moment
storageManager: false,
// Avoid any default panel
panels: { defaults: [] },
plugins: ['grapesjs-mjml','gjs-plugin-ckeditor', myNewComponentTypes],
pluginsOpts: {
'grapesjs-mjml': {},
'gjs-plugin-ckeditor': {/* ...options */}
},
})
<div id="gjs" style="height: 0px; overflow: hidden">
<mjml>
<mj-head>
<mj-font
name="Barlow"
href="https://fonts.googleapis.com/css?family=Barlow"
/>
<mj-style> .slogan { background: #000; } </mj-style>
</mj-head>
<mj-body>
But I don't see the effect, please help me
Answers (2)
Do this isComponent: el => (el.tagName || '').toLowerCase() === 'mj-body',
Thanks for reporting this, @mingxin-yang.
Great question about How to customize a component. The recommended approach with StyleManager is to use the event-driven API.
Start here:
- Check the GrapesJS documentation for your specific module
- Look for the
on()event listener method - Most operations can be achieved by listening to editor and component events
Common patterns:
// Listen for changes
editor.on('change', () => console.log('something changed'));
// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));
If you're still stuck:
- Share a minimal CodeSandbox reproduction
- Include what you've already tried
- Mention your GrapesJS version
- The community is here to help!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3631
How to load dynamic content in the final HTML file?
I looked everywhere, including the issues section and tried a few things. But really unsure how to load dynamic content in my components al...
Issue #3347
How can I get Uncompressed JS code for me to edit?
Hi, thank you for reviewing my problem. I want to use my own JSEditor to edit html、css and js. 1、First, I use getHtml, getCss, getJs to dis...
Issue #6152
CSS added via custom code persists after custom code component is removed
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? any Reproducible demo link https://grapesj...
Issue #3574
adding traits are not picked up by trait manager.
Following example from docs . Is there further configurtion to make new traitsa show for elements? The example using the input does not run...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Related tutorials
In-depth guides on the same topic.
Tutorial
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Tutorial
Embed GrapesJS in Your SaaS: A Weekend Guide
Embed GrapesJS in your SaaS and ship a white-label page builder over a weekend. Honest tradeoffs, real code, and the plugins that close the UX gap.
Tutorial
Big Updates: TinyMCE 8 and Placeholder 2.0 for GrapesJS
In May we shipped major updates to two of our most popular GrapesJS plugins — TinyMCE Inline Text Editor and Placeholder.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.