Issue #3076Opened October 16, 2020by Joshmamroud3 reactions

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: 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)

JoshmamroudOctober 23, 20202 reactions

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

artfOctober 22, 20201 reactions

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, 20200 reactions

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...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.