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 Options for the add of new rule (optional, default{})addOpts(optional, default{})propsObject Other props for the rule
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
Parameters
opts(optional, default{})
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{})
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
getRules
Find rules, in different states (eg. like :hover) and media queries, matching the selector.
Parameters
selectorstring Selector, eg. '.myclass'
Examples
// Common scenario, take all the component specific rules
const id = someComponent.getId();
const rules = cc.getRules(`#${id}`);
console.log(rules.map(rule => rule.toCSS()))
Returns Array<CssRule>