Miscellaneous block editor API additions in WordPress 5.8

WordPress 5.8 brings several additions and tweaks to the blockBlock Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience. editor APIAPI An API or Application Programming Interface is a software intermediary that allows programs to interact with each other and share data in limited, clearly defined ways..

Contextual patterns for easier creation and block transformations

We’ve all been there. Staring at a blank page sometimes with an idea of what you want to create; often with a mind as blank as the page. To make the creation process easier, there is now a way to suggest patterns based on the block being used. This is now implemented for the Query block and includes some coreCore Core is the set of software required to run WordPress. The Core Development Team builds WordPress. patterns to start with.

In addition, there is an API to suggest pattern transformations that are contextual to the currently selected blocks. So how this is different to the patterns current behaviour? Previously, patterns insert demo content that must be updated after insertion. With this feature, it’s possible to use some patterns and retain existing attributes or content.

So it’s for existing blocks!

An important thing to note here is that a pattern transform can result to adding more blocks than the ones currently selected. You can see this with an example like the below where we have a Quote block but the pattern consist of more blocks:

This is the first iteration of the feature that covers most simple blocks (without innerBlocks). A new experimental API has been created where we can mark what block attributes count as content attributes. You can see more details in the PR.

In the long run as this work continues and spreads to more blocks, it will be easier to create content and get inspired without leaving the editor.

Pattern Registration API

if you’re creating your own custom block patterns, there’s a new blockTypes property that will allow your patterns to show up in other contexts like the transform menu. blockTypes property is an array containing the block names.

         'title'         => __( 'Black heading' ),
         'categories'    => array( 'text' ),
         'blockTypes'    => array( 'core/heading' ),
         'viewportWidth' => 500,
         'content'       => ' <!-- wp:heading {"level":3,"backgroundColor":"black","textColor":"white"} -->
    <h3 class="has-white-color has-black-background-color has-text-color has-background">Demo heading</h3>
<!-- /wp:heading -->',

To learn more about block patterns, see this WordPress News article: So you want to make block patterns.

BlockControls group prop

In WordPress 5.8, core blocks toolbars have been updated and made more consistent across blocks by splitting them into 4 areas like shown in the following screenshot.

To do so a new group prop has been added to the wp.blockEditor.BlockControls component. Third-party block authors are encourage to use this prop in their block code to follow the core blocks design pattern.

<BlockControls group="block">
    <ToolbarButton onClick={ doSomething }>{ __( 'My button' ) }</ToolbarButton>

#5-8, #core-editor, #dev-notes