QUESTION: Set component's empty state
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 #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 #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...
Issue #1846
[Question]: Using ngModel on component that contains groups of tagName
I have tried this solution https://github.com/artf/grapesjs/issues/1819#event-2158016066 But on custom components Let's say, i have a mixtu...
Issue #1426
Make some element unediatble by default.
@artf how can we set some configuration so that while importing html in editor it will not consider some element as text component and it s...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.