Gutenberg: Is there a way to know if current block is inside InnerBlocks?

getBlockParents will work accurately. You can use getBlockParents with the clientId of the block, getBlockParents will return the all parent blocks id if the current block is under any Blocks. It will return blank if current block is not under any Block

here is a method that you can use:

const innerBlock = "namespace/block-name";
const parentBlocks = wp.data.select( 'core/block-editor' ).getBlockParents(props.clientId); 
const parentAttributes = wp.data.select('core/block-editor').getBlocksByClientId(parentBlocks);

var is_under_inner = false;
for (i = 0; i < parentAttributes.length; i++) {
    if ( parentAttributes[i].name == innerBlock ) {
        is_under_inner = true;
    }
}

//console.log(is_under_inner);

Leave a Comment