diff --git a/docs/.vuepress/public/block-custom-render.jpg b/docs/.vuepress/public/block-custom-render.jpg new file mode 100644 index 000000000..1a883d749 Binary files /dev/null and b/docs/.vuepress/public/block-custom-render.jpg differ diff --git a/docs/.vuepress/public/block-custom-render2.jpg b/docs/.vuepress/public/block-custom-render2.jpg new file mode 100644 index 000000000..b9a303a3f Binary files /dev/null and b/docs/.vuepress/public/block-custom-render2.jpg differ diff --git a/docs/modules/Blocks.md b/docs/modules/Blocks.md index 9ad08dd08..bdac21ccf 100644 --- a/docs/modules/Blocks.md +++ b/docs/modules/Blocks.md @@ -6,6 +6,8 @@ title: Block Manager

GrapesJS - Block Manager

+[[toc]] + The Block is a group of [Components] and can be easily reused inside templates. The difference between components and blocks: The component is more atomic, so a single image, a text box or a map is a component. The block is what the end user will drag inside the canvas, so it could contain a single image (single Component) or the entire section like, for example, the footer with a lot of components inside (texts, images, inputs, etc). @@ -69,6 +71,57 @@ If you want to check the complete list of available Component's properties, chec [https://github.com/artf/grapesjs/blob/dev/src/dom_components/model/Component.js](https://github.com/artf/grapesjs/blob/dev/src/dom_components/model/Component.js) + + + + +## Custom render + +If you need to customize the aspect of each block you can pass a `render` callback function in the block definition. Let's see how it works. + +As a first option, you can return a simple HTML string, which will be used as a new inner content of the block. As an argument of the callback you will get an object containing the following properties: + +* `model` - Block's model (so you can use any passed property to it) +* `el` - Current rendered HTMLElement of the block +* `className` - The base class name used for blocks (useful if you follow BEM, so you can create classes like `${className}__elem`) + +```js +blockManager.add('some-block-id', { + label: `
+ +
Label block
+
`, + content: '
...
', + render: ({ model, className }) => `
+ Before label + ${model.get('label')} + After label +
`, +}); +``` + + + + +Another option would be to avoid returning from the callback (in that case nothing will be replaced) and edit only the current `el` block element + +```js +blockManager.add('some-block-id', { + // ... + render: ({ el }) => { + const btn = document.createElement('button'); + btn.innerHTML = 'Click me'; + btn.addEventListener('click', () => alert('Do something')) + el.appendChild(btn); + }, +}); +``` + + + + + + [Component]: [Components]: [Blocks API]: