How should I receive custom trait modifications in the script?
You'll need to add the trait to script-props, you can read more here https://grapesjs.com/docs/modules/Components-js.html#passing-properties-to-scripts
Read full answer below βQuestion
Hi, @artf, this is a great framework, thank you very much.
I am now using it to create components of the mobile BUI framework, and I ran into a problem, I added a custom trait textarea, how can I make this When the data changes, it is triggered in the output script.
editor.TraitManager.addType('textarea', {
createInput({
trait,
component
}) {
// Here we can decide to use properties from the trait
const el=document.createElement('div');
el.innerHTML = `
<textarea class="buiv-textarea" name="${options[0].name}" rows="${options[0].rows}">${options[0].value}</textarea>
`;
return el;
},
onEvent({
elInput,
component,
event
}) {
// The data change will reach this callback, you can operate the component, but how can we trigger the parameters received in the script?
},
onUpdate({
elInput,
component,
trait
}) {
},
});
// use trait textarea
editor.DomComponents.addType("slide", {
model: defaultModel.extend({
defaults: {
...defaultModel.prototype.defaults,
name: "slide",
content: `<div class="bui-slide bui-slide-skin01"></div>`,
datas: '[{image:""}]',
traits: [{
type: 'textarea',
name: 'datas',
label: "data",
changeProp: 1,
}],
script() {
let parentId=this.id;
// first time it is [{image:""}], I want to listen 'datas' change trigger here.
console.log('{[ datas ]}')
},
},
}
}),
view: defaultView.extend({
init() {
const props=['loop', 'fullscreen', 'height', 'direction', 'visibleNum', 'scrollNum', 'datas', 'autoplay'];
const reactTo = props.map(prop => `change:${prop}`).join(' ');
this.listenTo(this.model, reactTo, this.render);
}
}),
});
Answers (4)
You'll need to add the trait to script-props, you can read more here https://grapesjs.com/docs/modules/Components-js.html#passing-properties-to-scripts
Thanks @Ju99ernaut, If it is some built-in features, when modifying the script can receive the value of the parameter, I am currently experiencing problems: custom feature textarea, can trigger changes such as dom, but the exported script can not receive the value of this parameter, even if the script-props are set: "datas".
You can try forcing the script to update:
editor.DomComponents.addType('slide', {
model: {
// ...
init() {
this.on('change:datas', () => this.trigger('change:script'));
},
},
// ...
});
Thanks for reporting this, @imouou.
Great question about How should I receive custom trait modifications in the script?. The recommended approach with Canvas 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 #6409
Cannot drag to move components on mobile
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v133 Reproducible demo link https:/...
Issue #5379
Class modifications of components are not automatically detected
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome Version 116Reproducible demo link htt...
Issue #5245
Contents of component with editable:false are still editable
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome 114.0.5735.198 Reproducible demo li...
Issue #4906
Grapesjs fails to recognize component types when setting innerHTML through script.
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? chrome v108.0.5359.98 Reproducible demo li...
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
GrapesJS vs Webflow vs Builder.io vs Puck: Which Visual Builder to Choose in 2026
A practitioner's 2026 comparison of GrapesJS, Webflow, Builder.io, and Puck β pricing, AI features, lock-in, and a one-line rule for picking the right one
Tutorial
Find the Right GrapesJS Plugin in Seconds: Smarter Discovery Is Live
We're shipping a set of discovery upgrades. New label filters, a proper compatibility switch for GrapesJS vs Studio, one-click and a smarter sort bar.
Tutorial
Why use grapesjs shadcn for your template builder
Skip months of editor plumbing and start with a fully working visual builder β polished UI included.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.