4.9 KiB
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 selectorsstateString Css rule statewidthString For which device this style is orientedoptsObject 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 selectorsstateString Css rule statewidthString For which device this style is orientedrulePropsObject 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
setIdRule
Add/update the CSS rule with id selector
Parameters
namestring Id selector name, eg. 'my-id'styleObject Style properties and values (optional, default{})optsObject Custom options, likestateandmediaText(optional, default{})
Examples
const rule = cc.setIdRule('myid', { color: 'red' });
const ruleHover = cc.setIdRule('myid', { color: 'blue' }, { state: 'hover' });
// This will add current CSS:
// #myid { color: red }
// #myid:hover { color: blue }
Returns CssRule The new/updated rule
getIdRule
Get the CSS rule by id selector
Parameters
namestring Id selector name, eg. 'my-id'optsObject Custom options, likestateandmediaText(optional, default{})
Examples
const rule = cc.getIdRule('myid');
const ruleHover = cc.setIdRule('myid', { state: 'hover' });
Returns CssRule
setClassRule
Add/update the CSS rule with class selector
Parameters
namestring Class selector name, eg. 'my-class'styleObject Style properties and values (optional, default{})optsObject Custom options, likestateandmediaText(optional, default{})
Examples
const rule = cc.setClassRule('myclass', { color: 'red' });
const ruleHover = cc.setClassRule('myclass', { color: 'blue' }, { state: 'hover' });
// This will add current CSS:
// .myclass { color: red }
// .myclass:hover { color: blue }
Returns CssRule The new/updated rule
getClassRule
Get the CSS rule by class selector
Parameters
namestring Class selector name, eg. 'my-class'optsObject Custom options, likestateandmediaText(optional, default{})
Examples
const rule = cc.getClassRule('myclass');
const ruleHover = cc.getClassRule('myclass', { state: 'hover' });
Returns CssRule