Prevent/Disable Custom Blocks from being dropped inside other Custom Blocks
You can use :not() selector, eg. :not(.except-this-class)
Read full answer below āQuestion
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 Section Blocks. Is that possible?
Ref: https://grapesjs.com/docs/modules/Components.html#define-custom-component-type
I am aware of the draggable/droppable options that I can define on new component types. My understanding of these options is that they are inclusive.
...
draggable: 'form, form *', // Can be dropped only inside `form` elements
...
Is there a way to make draggable exclusive, where I say, "this block can be dropped anywhere EXCEPT [[component type]]"?
Answers (4)
You can use :not() selector, eg. :not(.except-this-class)
Thanks @artf . This solution works, mostly. Now I am encountering a new problem where some placeholder text is disappearing.
On some elements inside my blocks, I'll have an HTML element with a text node, then some other nested html element. An example would be a link tag containing an icon.
<a href="#">Click Me <i class="fa fa-chevron-right"></i></a>
When I add the draggable option to the model of my component, HTML elements inside the block similar to the A tag above will lose their text node. If I wrap these text nodes in something like a span tag, the text will stay. Examples of elements that don''t change:
<h1>I'm a Lonely textnode</h1>
<a href="#><span>this textnode is safe</span><i class="fa fa-chevron-right"></i></a>
Do you know of anything that will cause text nodes to disappear if they appear inside a container with other elements?
Here is how I am creating my custom component:
editor.DomComponents.addType('MySectionBlock', {
model: editor.DomComponents.getType('default').model.extend({
draggable: ':not(.my-section-block)'
}),
view: editor.DomComponents.getType('default').view.extend({
init() {
this.listenTo(this.model, 'active', this.act());
},
act() {
// do stuff to manipulate the view
},
})
});
My apologies if this new issue is vague. I am still troubleshooting, and very confused how this draggable config option can affect my component's view. Do you think I am somehow deleting these text nodes after I initiate my component's view?
Thanks again.
You're defining the component with the old API, which requires other stuff to work properly and that might be the cause of the issue (I've tried on my side and everything works as expected). Please check here for the correct API https://grapesjs.com/docs/modules/Components.html#define-custom-component-type
Thanks for reporting this, @stljeff1.
Great question about Prevent/Disable Custom Blocks from being dropped inside other Custom Blocks. 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 #3688
How to customize a component
I read this document https://grapesjs.com/docs/modules/Components.html#define-custom-component-type and tried to write the following code....
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...
Issue #5768
Wrong Type for "content" property in BlockProperties
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v122 Reproducible demo link not app...
Issue #4384
index.d.ts says that components must be Backbone.Collection<Component> but the docs say Component[] is also accepted
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? AnyReproducible demo link https://grapesjs.c...
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.