10 KiB
StyleManager
With Style Manager you build categories (called sectors) of CSS properties which could be used to customize the style of components. You can customize the initial state of the module from the editor initialization, by passing the following Configuration Object
const editor = grapesjs.init({
styleManager: {
// options
}
})
Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance.
const styleManager = editor.StyleManager;
Available Events
style:sector:addSector added. The Sector is passed as an argument to the callback.
editor.on('style:sector:add', (sector) => { ... });
style:sector:removeSector removed. The Sector is passed as an argument to the callback.
editor.on('style:sector:remove', (sector) => { ... });
style:sector:updateSector updated. The Sector and the object containing changes are passed as arguments to the callback.
editor.on('style:sector:update', (sector, changes) => { ... });
style:property:addProperty added. The Property is passed as an argument to the callback.
editor.on('style:property:add', (property) => { ... });
style:property:removeProperty removed. The Property is passed as an argument to the callback.
editor.on('style:property:remove', (property) => { ... });
style:property:updateProperty updated. The Property and the object containing changes are passed as arguments to the callback.
editor.on('style:property:update', (property, changes) => { ... });
style:targetTarget selection changed. The target (or null in case the target is deselected) is passed as an argument to the callback.
editor.on('style:target', (target) => { ... });
style:layer:selectLayer selected. Object containing layer data is passed as an argument.
editor.on('style:layer:select', (data) => { ... });
style:customCustom style event. Object containing all custom data is passed as an argument.
editor.on('style:custom', ({ container }) => { ... });
styleCatch-all event for all the events mentioned above. An object containing all the available data about the triggered event is passed as an argument to the callback.
editor.on('style', ({ event, sector, property, ... }) => { ... });
Methods
- getConfig
- addSector
- getSector
- getSectors
- removeSector
- addProperty
- getProperty
- getProperties
- removeProperty
- select
- getSelected
- getSelectedAll
- getSelectedParents
- addStyleTargets
- getBuiltIn
- getBuiltInAll
- addBuiltIn
- addType
- getType
- getTypes
Sectors
getConfig
Get configuration object
Returns Object
addSector
Add new sector. If the sector with the same id already exists, that one will be returned.
Parameters
-
idString Sector id -
sectorObject Sector definition. Check the available properties -
optionsObject Options (optional, default{})options.atNumber? Position index (by default, will be appended at the end).
Examples
const sector = styleManager.addSector('mySector',{
name: 'My sector',
open: true,
properties: [{ name: 'My property'}]
}, { at: 0 });
// With `at: 0` we place the new sector at the beginning of the list
Returns Sector Added Sector
getSector
Get sector by id.
Parameters
Examples
const sector = styleManager.getSector('mySector');
Returns (Sector | null)
getSectors
Get all sectors.
Parameters
Examples
const sectors = styleManager.getSectors();
removeSector
Remove sector by id.
Parameters
idString Sector id
Examples
const removed = styleManager.removeSector('mySector');
Returns Sector Removed sector
addProperty
Add new property to the sector.
Parameters
-
sectorIdString Sector id. -
propertyObject Property definition. Check the base available properties + others based on thetypeof your property. -
optsObject Options (optional, default{})opts.atNumber? Position index (by default, will be appended at the end).
Examples
const property = styleManager.addProperty('mySector', {
label: 'Minimum height',
property: 'min-height',
type: 'select',
default: '100px',
options: [
{ id: '100px', label: '100' },
{ id: '200px', label: '200' },
],
}, { at: 0 });
Returns (Property | null) Added property or null in case the sector doesn't exist.
getProperty
Get the property.
Parameters
Examples
const property = styleManager.getProperty('mySector', 'min-height');
Returns (Property | undefined)
getProperties
Get all properties of the sector.
Parameters
sectorIdString Sector id.
Examples
const properties = styleManager.getProperties('mySector');
Returns (Collection<Property> | undefined) Collection of properties
removeProperty
Remove the property.
Parameters
Examples
const property = styleManager.removeProperty('mySector', 'min-height');
Returns (Property | null) Removed property
select
Select new target. The target could be a Component, CSSRule, or a CSS selector string.
Parameters
target(Component | CSSRule | String)opts{stylable: boolean?, component: Component?} (optional, default{})
Examples
// Select the first button in the current page
const wrapperCmp = editor.Pages.getSelected().getMainComponent();
const btnCmp = wrapperCmp.find('button')[0];
btnCmp && styleManager.select(btnCmp);
// Set as a target the CSS selector
styleManager.select('.btn > span');
Returns Array<(Component | CSSRule)> Array containing selected Components or CSSRules
getSelected
Get the last selected target. By default, the Style Manager shows styles of the last selected target.
Returns (Component | CSSRule | null)
getSelectedAll
Get the array of selected targets.
Returns Array<(Component | CSSRule)>
getSelectedParents
Get parent rules of the last selected target.
addStyleTargets
Update selected targets with a custom style.
Parameters
Examples
styleManager.addStyleTargets({ color: 'red' });
getBuiltIn
Return built-in property definition
Parameters
propString Property name.
Examples
const widthPropDefinition = styleManager.getBuiltIn('width');
Returns (Object | null) Property definition.
getBuiltInAll
Get all the available built-in property definitions.
Returns Object
addBuiltIn
Add built-in property definition. If the property exists already, it will extend it.
Parameters
Examples
const sector = styleManager.addBuiltIn('new-property', {
type: 'select',
default: 'value1',
options: [{ id: 'value1', label: 'Some label' }, ...],
})
Returns Object Added property definition.
addType
Add new property type
Parameters
Examples
styleManager.addType('my-custom-prop', {
// Create UI
create({ props, change }) {
const el = document.createElement('div');
el.innerHTML = '<input type="range" class="my-input" min="10" max="50"/>';
const inputEl = el.querySelector('.my-input');
inputEl.addEventListener('change', event => change({ event }));
inputEl.addEventListener('input', event => change({ event, partial: true }));
return el;
},
// Propagate UI changes up to the targets
emit({ props, updateStyle }, { event, partial }) {
const { value } = event.target;
updateStyle(`${value}px`, { partial });
},
// Update UI (eg. when the target is changed)
update({ value, el }) {
el.querySelector('.my-input').value = parseInt(value, 10);
},
// Clean the memory from side effects if necessary (eg. global event listeners, etc.)
destroy() {}
})
getType
Get type
Parameters
idstring Type ID
Returns Object Type definition
getTypes
Get all types
Returns Array