--- title: Component Manager --- # Component Manager The Component is a base element of the template. It might be something simple and atomic like an image or a text box, but also complex structures, more probably composed by other components, like sections or pages. The concept of the component was made to allow the developer to bind different behaviors to different elements. For example, opening the Asset Manager on double click of the image is a custom behavior bound to that particular type of element. ::: warning This guide is referring to GrapesJS v0.15.8 or higher ::: ::: tip Skip the boilerplate—use a refined component editor out of the box. [Checkout the Grapes Studio SDK!](https://app.grapesjs.com/docs-sdk/configuration/components/overview?utm_source=grapesjs-docs&utm_medium=tip) ::: [[toc]] ## How Components work? Let's see in detail how components work by looking at all the steps from adding an HTML string to the editor. ::: tip All the following snippets can be run directly in console from the [main demo](https://grapesjs.com/demo.html) ::: This is how we can add new components to the canvas: ```js // Append components directly to the canvas editor.addComponents(`
Hello world!!!
`); // or into some, already defined, component. // For instance, appending to a selected component would be: editor.getSelected().append(`
...`); // Actually, editor.addComponents is an alias of... editor.getWrapper().append(`
...`); ``` ::: tip If you need to append a component at a specific position, you can use `at` option. So, to add a component on top of all others (in the same collection) you would use ```js component.append('
...', { at: 0 }); ``` or in the middle ```js const { length } = component.components(); component.append('
...', { at: parseInt(length / 2, 10) }); ``` ::: ### Component Definition In the first step, the HTML string is parsed and transformed to what is called **Component Definition**, so the result of the input above would be: ```js { tagName: 'div', components: [ { type: 'image', attributes: { src: 'https://path/image' }, }, { tagName: 'span', type: 'text', attributes: { title: 'foo' }, components: [{ type: 'textnode', content: 'Hello world!!!' }] } ] } ``` The real **Component Definition** would be a little bit bigger so we've reduced the JSON for the sake of simplicity. You might notice the result is similar to what is generally called a **Virtual DOM**, a lightweight representation of the DOM element. This actually helps the editor to keep track of the state of our elements and make performance-friendly changes/updates. The meaning of properties like `tagName`, `attributes` and `components` are quite obvious, but what about `type`?! This particular property specifies the **Component Type** of our **Component Definition** (you check the list of default components [below](#built-in-component-types)) and if it's omitted, the default one will be used `type: 'default'`. At this point, a good question would be, how the editor assigns those types by starting from a simple HTML string? This step is identified as **Component Recognition** and it's explained in detail in the next paragraph. ### Component Recognition and Component Type Stack As we mentioned before, when you pass an HTML string as a component to the editor, that string is parsed and compiled to the [Component Definition] with a new `type` property. To understand what `type` should be assigned, for each parsed HTML Element, the editor iterates over all the defined components, called **Component Type Stack**, and checks via `isComponent` method (we will see it later) if that component type is appropriate for that element. The Component Type Stack is just a simple array of component types but what matters is the order of those types. Any new added custom **Component Type** (we'll see later how to create them) goes on top of the Component Type Stack and each element returned from the parser iterates the stack from top to bottom (the last element of the stack is the `default` one), the iteration stops once one of the component returns a truthy value from the `isComponent` method. ::: tip If you're importing big string chunks of HTML code you might want to improve the performances by skipping the parsing and the component recognition steps by passing directly Component Definition objects or using the JSX syntax. Read [here](#setup-jsx-syntax) about how to setup JSX syntax parser ::: ### Component instance Once the **Component Definition** is ready and the type is assigned, the [Component] instance can be created (known also as the **Model**). Let's step back to our previous example with the HTML string, the result of the `append` method is an array of added components. ```js const component = editor.addComponents(`
Hello world!!!
`)[0]; ``` The Component instance contains properties and methods which allows you to obtain its data and change them. You can read properties with the `get` method, like, for example, the `type` ```js const componentType = component.get('type'); // eg. 'image' ``` and to update properties you'd use `set`, which might change the way a component behaves in the canvas. ```js // Make the component not draggable component.set('draggable', false); ``` You can also use methods like `getAttributes`, `setAttributes`, `components`, etc. ```js const innerComponents = component.components(); innerComponents.forEach((comp) => console.log(comp.toHTML())); // Update component content component.components(`
Component 1
Component 2
`); ``` Each component can define its own properties and methods but all of them will always extend, at least, the `default` one (then you will see how to create new custom components and how to extend the already defined) so it's good to check the [Component API] to see all available properties and methods. The **main purpose of the Component** is to keep track of its data and to return them when necessary. One common thing you might need to ask from the component is to show its current HTML ```js const componentHTML = component.toHTML(); ``` This will return a string containing the HTML of the component and all of its children. The component implements also `toJSON` methods so you can get its JSON structure in this way ```js JSON.stringify(component); ``` ::: tip For storing/loading all the components you should rely on the [Storage Manager](/modules/storage.html) ::: So, the **Component instance** is responsible for the **final data** (eg. HTML, JSON) of your templates. If you need, for example, to update/add some attribute in the HTML you need to update its component (eg. `component.addAttributes({ title: 'Title added' })`), so the Component/Model is your **Source of Truth**. ### Component rendering Another important part of components is how they are rendered in the **canvas**, this aspect is handled by its **View**. It has nothing to do with the **final HTML data**, you can return a big `
...
` string as HTML of your component but render it as a simple image in the canvas (think about placeholders for complex/dynamic data). By default, the view of components is automatically synced with the data of its models (you can't have a View without a Model). If you update the attribute of the component or append a new one as a child, the view will render it in the canvas. Unfortunately, sometimes, you might need some additional logic to handle better the component result. Think about allowing a user build its `` element, for this specific case you might want to add custom buttons in the canvas, so it'd be easier adding/removing columns/rows. To handle those cases you can rely on the View, where you can add additional DOM component, attach events, etc. All of this will be completely unrelated with the final HTML of the `
` (the result the user would expect) as it handled by the Model. Once the component is rendered you can always access its View and the DOM element. ```js const component = editor.getSelected(); // Get the View const view = component.getView(); // Get the DOM element const el = component.getEl(); ``` Generally, the View is something you wouldn't need to change as the default one handles already the sync with the Model but in case you'd need more control over elements (eg. custom UI in canvas) you'll probably need to create a custom component type and extend the default View with your logic. We'll see later how to create custom Component Types. So far we have seen the core concept behind Components and how they work. The **Model/Component** is the **source of truth** for the final code of templates (eg. the HTML export relies on it) and the **View/ComponentView** is what is used by the editor to **preview our components** to users in the canvas. ## Built-in Component Types Here below you can see the list of built-in component types, ordered by their position in the **Component Type Stack** - [`cell`](https://github.com/GrapesJS/grapesjs/blob/dev/packages/core/src/dom_components/model/ComponentTableCell.ts) - Component for handle `` elements - [`table`](https://github.com/GrapesJS/grapesjs/blob/dev/packages/core/src/dom_components/model/ComponentTable.ts) - Component for handle `
` and `` elements - [`row`](https://github.com/GrapesJS/grapesjs/blob/dev/packages/core/src/dom_components/model/ComponentTableRow.ts) - Component for handle `
` elements - [`thead`](https://github.com/GrapesJS/grapesjs/blob/dev/packages/core/src/dom_components/model/ComponentTableHead.ts) - Component for handle `` elements - [`tbody`](https://github.com/GrapesJS/grapesjs/blob/dev/packages/core/src/dom_components/model/ComponentTableBody.ts) - Component for handle `` elements - [`tfoot`](https://github.com/GrapesJS/grapesjs/blob/dev/packages/core/src/dom_components/model/ComponentTableFoot.ts) - Component for handle `` elements - [`map`](https://github.com/GrapesJS/grapesjs/blob/dev/packages/core/src/dom_components/model/ComponentMap.ts) - Component for handle `` elements - [`link`](https://github.com/GrapesJS/grapesjs/blob/dev/packages/core/src/dom_components/model/ComponentLink.ts) - Component for handle `` elements - [`label`](https://github.com/GrapesJS/grapesjs/blob/dev/packages/core/src/dom_components/model/ComponentLabel.ts) - Component for handle properly `