I've created a custom Gutenberg block and would like to know how to add more within the same module. Some suggested to add JS in MY_CustomModule.libraries.yml, but it does not work.
My custom H2 heading block in index.es6.js. This simple block is working fine.
const { blocks, data, element, components, editor } = wp;
const { registerBlockType } = blocks;
const { dispatch, select } = data;
const { Fragment } = element;
const { PanelBody, BaseControl, Icon, RangeControl, IconButton, Toolbar, SelectControl } = components;
const { InnerBlocks, RichText, InspectorControls, PanelColorSettings, MediaUpload, BlockControls } = editor;
const __ = Drupal.t;
const settings = {
title: __('Section Heading'),
description: __('H2 Heading'),
icon: 'heading',
attributes: {
title: {
type: 'array',
source: 'children',
selector: 'h2',
},
},
edit({ className, attributes, setAttributes, isSelected }) {
const { title } = attributes;
return (
<Fragment>
<RichText
identifier="title"
tagName="h2"
value={ title }
onChange={nextTitle => {
setAttributes({
title: nextTitle,
});
}}
placeholder={ __( 'Enter text...', 'custom-block' ) }
keepPlaceholderOnFocus={true}
/>
<InspectorControls>
<PanelBody title={ __('Block Settings') }>
{ attributes.content }
</PanelBody>
</InspectorControls>
</Fragment>
);
},
save({ className, attributes }) {
const { title } = attributes;
return (
<h2>{ title }</h2>
);
}
};
const category = {
slug: 'myblocks',
title: __('My Blocks'),
};
const currentCategories = select('core/blocks').getCategories().filter(item => item.slug !== category.slug);
dispatch('core/blocks').setCategories([ category, ...currentCategories ]);
registerBlockType(`${category.slug}/docs-blocks`, { category: category.slug, ...settings });