How to get style for header & footer components only
I don't see a problem with media queries. The rules are detected by selector and everything works fine. I add class and set style only for mobile and it works.
Read full answer below ↓Question
Hi @artf, can you please tell me what is the fastest and easies way to extract component styles.
I have template and in this template you can edit part of header, footer and add content between them. I don't store the whole template just the content in the middle, header & footer. component structure is easy I loop the JSON and get the components.
I want to separate the style of the header & footer from the style of the page. How to do that?
Regards, Alex
Answers (3)
I don't see a problem with media queries. The rules are detected by selector and everything works fine. I add class and set style only for mobile and it works.
Yeah, styles are in a different container (editor.CssComposer.getAll()) so it's a bit harder and there is no API for extracting all matching styles.
What I'd suggest doing something like this
const matchedRules = (component, rules) {
const el = component.getEl();
let result = [];
rules.forEach(rule => {
try {
if (rule.selectorsToString().split(',').some(
selector => el.matches(selector)
)) {
result.push(rule);
}
} catch (err) {}
});
component.components().forEach(component => {
result = result.concat(matchedRules(component, rules))
})
return result;
};
const allRules = editor.CssComposer.getAll();
const componentRules = matchedRules(yourComponent, allRules);
// componentRules is now an array of css rules
// To get the code you'd do something like this
let cssCode = '';
componentRules.forEach(rule => cssCode += rule.toCSS());
BTW this is not a final solution, definitely, you'd need to handle also rules with media queries, so take it as a starting point
@artf, thanks!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3268
How to add javascript in the html without sanitization?
Hey @artf, I am taking javascript input from the user. I need to add this javascript inside the template HTML but it gets sanitized. Is the...
Issue #2404
Question: how to observe children changes in component
Is there an easy way to subscribe/listen to changes in the components/children of my custom component type? Perhaps an event I can subscrib...
Issue #913
[Question] Using Style tag instead of css
Hi, I've been taking a look inside of the dom_components, but cant seem to figure out how to manually override the styling aspect. I need a...
Issue #2076
[QUESTION] How make a new component to allow laravel blade directives?
Hi, thanks for the nice work. I'll like to know if there is a way to use blade directives inside the code. I already add a new functionalit...
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 →Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.