Set component's empty state
@artf You are THE MAN! I didn't even know the :empty selector existed. So simple, so awesome! Mind blown. Thank you!
Read full answer below ↓Question
Hi @artf ,
How would you recommend giving a component an empty state?
Say, for instance, I have a layout component (ie. a column) which should contain child elements (ie. text component or image). When that layout component is empty I'd like to display some help text inside such as "Add an element here" and when it has children it shouldn't be displayed.
Of course, this should only exist in the view and not render in the output HTML.
Here is an example of what I am looking to create:

If there is another issue that explains how to do this then I apologize, I searched and wasn't able to find anything on it.
Thank you in advance! Josh
Answers (3)
@artf You are THE MAN! I didn't even know the :empty selector existed. So simple, so awesome! Mind blown. Thank you!
I've done something similar in this way using only CSS
const doc = editor.Canvas.getDocument();
...addType('cc', {
model: {...},
view: {
init() {
...
const stl = doc.querySelector('style#cc-id');
if (!stl) {
doc.body.insertAdjacentHTML('beforeend', `
<style id="cc-id">
.cc-class:empty:after {
content: "Empty block";
text-align: center;
display: block;
}
</style>
`);
}
}
}
})
Hi Josh, I like your idea.
No expert here, but here are couple thoughts:
Basically I would add some kind of flag to the component itself.
idea 1) If the component is some kind of container (i.e. div), add a <p class="empty-message">Drag an element here</p> to it, then when a new component is added, you could use the editor event component:addand check for any siblings with the empty-message class and remove it.
idea 2) Add a empty-message class to the component itself, then use CSS :before/:after to add content to it. Then remove the componentś parent empty-message class using JS, similarly to idea 1.
Hope it helps.
Related Questions and Answers
Continue research with similar issue discussions.
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 #2170
[QUESTION] Custom component - how to set editable to true
Hi, I've created a custom component type, but I am having some trouble making it editable. The component is a "span" with component type "t...
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...
Issue #3030
Question: How to sync the order of sub children across all child elements ?
I have a <ul> in which I want to show Images and image description as <li> elements. I want to be able to move the image inside the <li> fo...
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.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.