# Layer Manager

GrapesJS - Layer Manager

The Layer Manager module is responsible for managing and displaying your Components as a tree.

WARNING

This guide applies to GrapesJS v0.19.5 or higher.

# Configuration

To change the default configuration, pass the layerManager option in the main configuration object.

const editor = grapesjs.init({
  ...
  layerManager: {
    ...
  }
});

You can check here the full list of available configuration options: Layer Manager Config (opens new window)

Layers directly represent your components, so they are only available once your components are loaded in the editor (e.g., after loading project data from a remote endpoint).

In your configuration, you can change the global behavior of layers (e.g., make all layers unsortable) and decide which component layer to use as the root.

const editor = grapesjs.init({
  ...
  layerManager: {
    // If the `root` is not specified or the component element is not found,
    // the main wrapper component will be used.
    root: '#my-custom-root',
    sortable: false,
    hidable: false,
  }
});

The configuration options mainly target the default UI provided by GrapesJS core. If you need more control over the layer tree, read the Customization section below.

# Programmatic usage

To manage layers programmatically, use the Layers API.

# Customization

Using the Layers API, you can replace the default UI with your own implementation.

Tell the editor to use a custom UI, then subscribe to the events that let you update it.

const editor = grapesjs.init({
  // ...
  layerManager: {
    custom: true,
    // ...
  },
});

// Use this event to append your UI in the default container provided by GrapesJS.
// You can skip this event if you don't rely on the core panels and decide to
// place the UI in some other place.
editor.on('layer:custom', (props) => {
  // props.container (HTMLElement) - The default element where you can append your UI
});

// Triggered when the root layer is changed.
editor.on('layer:root', (root) => {
  // Update the root of your UI
});

// Triggered when a component is updated, allowing you to update specific layers.
editor.on('layer:component', (component) => {
  // Update the specific layer of your UI
});

In the example below, we'll replicate most of the default functionality with our own implementation.

# Events

For a complete list of available events, check here.