Default style for custom blocks
Question
Hi, I'm using GrapesJS to a page builder inside a Prestashop website. The idea is to set default template stored in DB with custom Blocks. Everything working fine about the store and the load of the template. My problem is that I want to use only custom blocks that i created and added to the BlockManager but with default CSS. I'm able to put CSS but how do I set media queries for those blocks by default ? The users of the page builder will only have to drag the blocks and those one should be responsive by default. This is my code :
editor.BlockManager.add('block-header', {
label: 'Bloc Header',
content:
`<style>
.mdc_block_header {
width:100%;
display:flex;
justify-content:center;
align-items:center;
height:400px;
background-image: url('http://placehold.it/350x250/78c5d6/fff/image1.jpg');
background-size: cover;
}
.mdc_block_header span {
font-size:22px;
}
</style>
`<div class="mdc_block_header"><span>Test</span></div>`,
attributes: {
title: 'Insert header_block'
}
});
The problem is that I have to use the <style></style> in order to access media queries but when I drag two times the blocks and change the background-image in the editor, it change for the two blocks and not only the one I wanted to..
Answers (3)
You can make that class private to avoid selecting them in Style Manager, in that way you will style the component and not the rule
const privateCls = ['.mdc_block_header'];
editor.on('selector:add', (selector) => {
if (privateCls.indexOf(selector.getFullName()) >= 0) {
selector.set({private: 1});
}
});
Nice one, working great ! Thank's a lot !
@artf The code works but I just saw that I can't see anymore the select button to see the Hover state of the element in the editor.. It come back when i remove the code you made me add..
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1611
Script tag create JSON error
Hi. I'm creating blocks in Grapesjs and I need to use JS on them for hover effect and more.. Everything works fine for the creation, store,...
Issue #1720
[QUESTION] How to add the button in custom traits
Hi, I try to add a button to my custom traits for my custom blocks but its not working fine as expected and also need to know how to call t...
Issue #543
Blocks Manager in GrapesJS Not Working For Columns
Hello, My installation of GJS ties in all of the default blocks. Every block drags fine onto the page with the exception of all the "column...
Issue #1123
[QUESTION] Onload event inside template
Hi! I have a problem with some templates. This template has some scripts running after the page is loaded. When this templates is loaded by...
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.