Issue #2238Opened September 2, 2019by abozhinov1 reactions

[QUESTION] How to get style for header & footer components only

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)

abozhinovSeptember 7, 20191 reactions

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.

artfSeptember 5, 20190 reactions

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

abozhinovSeptember 7, 20190 reactions

@artf, thanks!

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.