Issue #3076✓ SolvedOpened October 16, 2020by Joshmamroud3 reactions

Set component's empty state

Quick answerby Joshmamroud2

@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: image

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)

👍 Most helpfulJoshmamroudOctober 23, 2020

@artf You are THE MAN! I didn't even know the :empty selector existed. So simple, so awesome! Mind blown. Thank you!

artfOctober 22, 2020

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>
     `);
    } 
  }
 }
})
hellocaioOctober 17, 2020

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.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

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.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.