CssComposer

This module contains and manage CSS rules for the template inside the canvas. You can customize the initial state of the module from the editor initialization, by passing the following Configuration Object

const editor = grapesjs.init({
 cssComposer: {
   // options
 }
})

Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance

const cssComposer = editor.CssComposer;

load

Load data from the passed object, if the object is empty will try to fetch them autonomously from the storage manager. The fetched data will be added to the collection

Parameters

  • dataObject Object of data to load

Returns Object Loaded rules

store

Store data to the selected storage

Parameters

  • noStoreBoolean If true, won't store

Returns Object Data to store

add

Add new rule to the collection, if not yet exists with the same selectors

Parameters

  • selectorsArray<Selector> Array of selectors
  • stateString Css rule state
  • widthString For which device this style is oriented
  • optsObject Other options for the rule (optional, default {})

Examples

var sm = editor.SelectorManager;
var sel1 = sm.add('myClass1');
var sel2 = sm.add('myClass2');
var rule = cssComposer.add([sel1, sel2], 'hover');
rule.set('style', {
  width: '100px',
  color: '#fff',
});

Returns Model

get

Get the rule

Parameters

  • selectorsArray<Selector> Array of selectors
  • stateString Css rule state
  • widthString For which device this style is oriented
  • rulePropsObject Other rule props

Examples

var sm = editor.SelectorManager;
var sel1 = sm.add('myClass1');
var sel2 = sm.add('myClass2');
var rule = cssComposer.get([sel1, sel2], 'hover');
// Update the style
rule.set('style', {
  width: '300px',
  color: '#000',
});

Returns (Model | null)

getAll

Get the collection of rules

Returns Collection

clear

Remove all rules

Returns this

setRule

Add/update the CSS rule with a generic selector

Parameters

  • selectorsstring Selector, eg. '.myclass'
  • styleObject Style properties and values
  • optsObject Additional properties (optional, default {})
    • opts.atRuleTypeString At-rule type, eg. 'media' (optional, default '')
    • opts.atRuleParamsString At-rule parameters, eg. '(min-width: 500px)' (optional, default '')

Examples

// Simple class-based rule
const rule = cc.setRule('.class1.class2', { color: 'red' });
console.log(rule.toCSS()) // output: .class1.class2 { color: red }
// With state and other mixed selector
const rule = cc.setRule('.class1.class2:hover, div#myid', { color: 'red' });
// output: .class1.class2:hover, div#myid { color: red }
// With media
const rule = cc.setRule('.class1:hover', { color: 'red' }, {
 atRuleType: 'media',
 atRuleParams: '(min-width: 500px)',
});
// output: @media (min-width: 500px) { .class1:hover { color: red } }

Returns CssRule The new/updated rule

getRule

Get the CSS rule by a generic selector

Parameters

  • selectorsstring Selector, eg. '.myclass:hover'
  • opts (optional, default {})

Examples

const rule = cc.getRule('.myclass1:hover');
const rule2 = cc.getRule('.myclass1:hover, div#myid');
const rule3 = cc.getRule('.myclass1', {
 atRuleType: 'media',
 atRuleParams: '(min-width: 500px)',
});

Returns CssRule

Last Updated: 1/28/2019, 7:45:05 PM