Custom component property that is not a trait
Question
I would like to create a custom component with a property that will be updated by specific code, but I don't want this property to be visible and updatable as a trait; let say my property is called "ebTesto", I wrote a plugin function like that:
function addComponentX(editor, options){
const COMPONENT_TYPE = 'eb-componentx';
editor.DomComponents.addType(COMPONENT_TYPE, {
isComponent: function(el) {
if(el.getAttribute && el.getAttribute('data-gjs-type') ==COMPONENT_TYPE) {
return {type:COMPONENT_TYPE};
}
},
model: {
defaults: {
ebTesto: 'Ciao',
tagName: 'div',
components: [
{
type:'text',
content: 'Title component X',
removable: false
}
]
},
ebTesto: 'Ciao'
}
});
editor.BlockManager.add(COMPONENT_TYPE, {
label: 'Component X',
category: 'EB',
attributes: {class:'eb-componentx-block'},
content: `<div class="eb-componentx" data-gjs-type="${COMPONENT_TYPE}"></div>`
});
}
I expected to find my new property in the "gjs-components" part of the stored JSON, but this is what I got:
[
{
"type": "eb-componentx",
"classes": [
"eb-componentx"
],
"components": [
{
"type": "text",
"removable": false,
"content": "Title component X"
}
]
}
]
Maybe I'm trying the wrong way? Has anyone ever had a similar need? Thanks for your help!
Answers (4)
Thank you both!
I think it's not stored since the property hasn't been changed from the default. The editor should be able to recover the property from the component definition itself.
Try adding it inside defaults
Thanks for reporting this, @ltenti-eventboost.
Great question about [QUESTION] Custom component property that is not a trait. The recommended approach with Components 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 #3335
Creating trait with Style Manager property UI
I have a component using the background-url Style Manager property: I would like to reuse this UI that is shown in the Style Manager for a...
Issue #5362
draggable/droppable property functions not called while dragging
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Version 1.56.20 Chromium: 115.0.5790.171 (...
Issue #3258
Prevent/Disable Custom Blocks from being dropped inside other Custom Blocks
Hello, I have a custom component, let's call it a Section Block. I want to prevent the user from dropping Section Blocks inside other Secti...
Issue #3213
How to save the dynamically added traits
Hi, @artf, I have a custom component that has a button trait. When the button is pressed, I am adding a custom trait dynamically using comp...
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.