Cannot add spaces to extended button component based on text component.
Seems like space triggers the click event in Chrome. The solution seems to be to add the preventDefault on keypress https://stackoverflow.com/questions/22104860/chrome-firing-onclick-when-space-typed-in-contenteditable-button ps. avoid extending the view in that way. By default, when you use extend of some type, it's...
Read full answer below ↓Question
Hello guys,
First of all i would like to express my gratitudes for this amazing tool. I have extended button component based on text component so as to can edit the content of button manually without using traits.
The code for the extended button component is this:
var comps = editor.DomComponents;
var defaultType4 = comps.getType('text');
editor.DomComponents.addType('button', {
model:{
defaults: {
tagName: 'button',
classes: ['.pointer'], //Add click pointer on hover,
},
},
extend: 'text',
isComponent: function (el) {
if (el.tagName === 'BUTTON')
return {type: 'button'};
},
view: defaultType4.view.extend({
events: {
dblclick: 'onActive',
input: 'onInput',
dragstart: 'handleDragStart',
click: function() {
/*Click function */
}
},
})
})
At this point i am capable of edit content of button components manually. Now the problem that i am dealing with is that i can not add space characters inside content of buttons.Can anyone help me ?Answers (2)
Seems like space triggers the click event in Chrome. The solution seems to be to add the preventDefault on keypress https://stackoverflow.com/questions/22104860/chrome-firing-onclick-when-space-typed-in-contenteditable-button
ps. avoid extending the view in that way. By default, when you use extend of some type, it's also extending its view (if you need to extend the view of another component you can use extendView: 'another-component-type',)
Thanks for reporting this, @christosapos.
Great question about Cannot add spaces to extended button component based on text 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 #3287
Wrapper styling
First of all, congrats @artf for this amazing project 💯 I am currently working on a project and I wanted to ask if I can edit the wrapper...
Issue #3170
Add class name from traits
Hello, what i am trying to achieve is to have the text component extended by assigning an extra trait named Alignment and deciding through...
Issue #3328
How can I update the nodeValue (text value) of components by inputs forms
Hello. Grapesjs is a great tool. Thanks for it. I want to update the text value of a component by inputs, how can I do that?? if a user fil...
Issue #3512
Child elements not draggable ,droppable highlightable. also the CSS of all child elements not pushed in style section while develop a banner component in as plugin
Hello @artf , I am trying to develop a simple banner plugin which will be include a heading , subheading and link. and every element can be...
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.