Last active
February 21, 2021 22:39
-
-
Save nico-martin/2592eba57775264f848a1876d82a74f2 to your computer and use it in GitHub Desktop.
Gutenberg InnerBlock with renderAppender
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {TextControl, IconButton} = wp.components; | |
const {InnerBlocks} = wp.editor; | |
const {__} = wp.i18n; | |
const {dispatch} = wp.data; | |
const {createBlock, registerBlockType} = wp.blocks; | |
registerBlockType('prefix/container', { | |
title: 'Container', | |
icon: 'category', | |
category: 'category', | |
edit(props): { | |
return ( | |
<div className="b-container"> | |
<InnerBlocks | |
allowedBlocks={['prefix/myblock']} | |
template={[['prefix/myblock']]} | |
renderAppender={() => ( | |
<IconButton icon="plus" className="button b-hellotab__append-tab" onClick={() => { | |
dispatch('core/block-editor').insertBlocks(createBlock('prefix/myblock'), 9999, this.props.clientId); | |
}}> | |
{__('Add Block')} | |
</IconButton> | |
)} | |
/> | |
</div> | |
); | |
}, | |
save(props) { | |
return ( | |
<div className="b-container"> | |
<InnerBlocks.Content/> | |
</div> | |
); | |
}, | |
}); |
Thanks🙏️, man you are such a lifesaver!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At the moment you have to use the Gutenberg Plugin instead of the Core Implementation because custom renderAppender is not yet in core.