I'm trying to figure out how to add a button to a block that, when clicked, will add a new child block to a parent block.
I see I can use wp.data.dispatch('core/editor').insertBlocks(block)
to add a block to the editor, but I'm not sure how to specify that it should go _inside_ a nest-able block.
I see the documentation here, but I'm unsure if the root client ID is what I need to get it to insert into a parent block, and if so, how I would go about getting that.
Thank in advance for any help!
Hi @raquelmsmith —
Great question! I happen to have an example you can use in our custom columns block.
Quick code tour:
clientId
is a prop available on your block. This is the client ID of the block you want to insert into.onAddColumn
callback when clicked. (Could just be generic onClick
or addBlock
or whatever too.)Let me know if it helps and I can close this up!
Yes, that helped a bunch! Don't know how I didn't realize that clientId
was a prop available on the block... 😆 Thanks so much for the help!
@raquelmsmith Woot! Glad it helped!
@chrisvanpatten
I want to test your columns block but I'm having trouble setting it up.
I build the javascript file, enqueued it but blocks don't show in Gutenberg and I get no errors.
@2one2 Sorry about that; had some small issues in the code. Just pushed up an improved version.
Most helpful comment
Hi @raquelmsmith —
Great question! I happen to have an example you can use in our custom columns block.
Quick code tour:
clientId
is a prop available on your block. This is the client ID of the block you want to insert into.onAddColumn
callback when clicked. (Could just be genericonClick
oraddBlock
or whatever too.)Let me know if it helps and I can close this up!