Set Toolbar on every Component
Question
Hi @artf
Is there any standard way to add a new toolbar on every component
I've tried this way
editor.Components.getTypes().map(type => {
editor.Components.addType(type.id, {
model: {
defaults: {
toolbar,
traits:[
...editor.Components.getType(type.id).model.prototype.defaults.traits,
...[
]
]
},
}
})
});
const toolbar = [{
attributes: {
class: 'fa fa-arrow-up'
},
command: e => e.runCommand('core:component-exit', {
force: 1
})
}, {
attributes: {
class: 'fa fa-arrows gjs-no-touch-actions',
draggable: true
},
command: 'tlb-move'
}, {
attributes: {
class: 'fa fa-clone'
},
command: 'tlb-clone'
}, {
attributes: {
class: 'fa fa-trash-o'
},
command: 'tlb-delete'
},{
attributes: {
class: 'fa fa-gear'
},
command: e => {
alert("hi")
}
}]
When I run this code on the browser console it's working as expected but when I placed this code In the component file this is not working when we define a new trait for all the components it's working fine.
For Toolbar it's not working and I also tried to place a code in editor. on('load') but no luck.
Any suggestion?
Thanks
FYI @david-polak
@arthuralmeidap
Answers (2)
Follow the issue template (version, live demo, etc.)
Thanks for reporting this, @Abhisheknanda1344463.
Great question about Set Toolbar on every Component. 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 #3228
Component Value is not changing
Hi @artf I've added a new component here is the code on selecting the h1 and h2 tag it's changing the value in the HTML section as you can...
Issue #3185
[QUESTION] How to get the models trait of a component?
Hi there, I added a new type "array" to the TraitManager. So I can add several sub traits. On one point I have to check the traits of the m...
Issue #3174
Creating a new component type does not inherit the functions of it's parent type
Hello, I am trying to create a new common trait shared among all components by creating the following plugin on another plugin file I am al...
Issue #3388
FEAT: adding indication as a Trait without any input.
I want to add an indication for a trait. For my component, I have the traits: and for the type 'dateformat-indication', I created the type:...
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.