Block API
gziolo opened this issue ยท 28 comments
This overview issue collects an actionable list of impactful tasks that capture possible enhancements to the various Block APIs that exist to power the WordPress block editor.
Block API is intentionally a vague term reflected in the list of action items grouped into categories like block registration, block attributes, block interactions, block modification, etc. Whatโs in common is that the tasks included aim to resolve the most common friction points or increase the potential of the APIs offered for WordPress block, plugin, and theme authors. In effect, it makes it possible to fulfill the needs of developers wanting to adopt the block editor in their projects.
๐ ๏ธ = in development
๐ค = needs to be unblocked
โTop Things
High-impact projects in active development:
๐ Backlog
Planned tasks that are being actively researched, waiting for developers to pick them up or on hold until they get unblocked.
Block Registration
- ๐คExplore a mechanism improving developer experience to use one API (example:
useBlockProps
,InnerBlocks
) that run different code depending on the context: edit, save, or frontend (#48590). - Add SlotFill for the block's description in the sidebar to allow custom code (#49887, #49819). At the moment we use block's description which is a simple string. However, it would be sometimes useful to include some custom HTML code like
<a />
. - Start using REST API for block types in WordPress core (#22812).
- Register multiple blocks from several
block.json
files. At the moment developers have to register every block separately. It could help to updateregister_block_type
or add newregister_block_types
on the server to support auto-discovery of multiple blocks throughblock.json
in a given target folder. - "Universal blocks" that could be written once and run in both PHP and JS with the help of a compiler (#38224).
- Allow custom path for languages to be defined in
block.json
for translations used with JavaScript (Trac #54797). - Explore advanced options on the intersection of
ancestor
andparent
to support allow/deny lists based on the editor, post-type, and template type context (#46900, #41062, related discussion starts also in #37181 (comment)). - Add a warning for block developers if they use reserved words for attribute names (#48112).
- Validate each arg before setting
WP_Block_Type
properties (#48039). - #35223
Block Assets
Block assets are JavaScript (editorScript
, script
, viewScript
) and CSS (editorStyle
, style
) defined in block.json
file.
- ๐ค #2768
- ๐ ๏ธ Allow customizations for assets in
block.json
as explained in Trac #54018 and to fix #40447. Tracked in #46954. - Add new utility function to add script / viewScript to an already registered block (#48382).
- Iterate on absorbing block styles (currently
__experimentalStyle
inblock.json
) as part of the global styles mechanism (#45198). - #56838
Block Attributes
- ๐ค Solving some semi-related shortcomings with block attributes (#49466).
- ๐ ๏ธ Add support for sourced attributes on the server (WordPress/wordpress-develop#6388).
- Optional support that optimized blocks for usage as structured data (#49437).
- Explore how to connect an individual block attribute with a given UI control and the save flow. In effect, have a simple way to allow editing block attributes without manually writing all code for block controls and inspector controls (#48245).
- Exploring how Patterns can be connected to Schemas (#41606).
- #65186
- #57942
- #59532
Block Supports
- ๐คAllow controlling behaviors/interactions by blocks and themes (#50029).
- Ensure that the block editor respects
parent
,ancestor
, andsupports.multiple
when using drag & drop, or copy & paste (#53471). - Add the ability to limit the number of items within an Inner Blocks area (#42342).
- #53989
Block Variations
- Add handling for block type name aliases used with block variations (#43743). They should fall back if necessary to the original block type name, e.g.
woocommerce/product-list
that can get resolved to theproduct-list
variation added to thecore/query
block. - Add support for the
ancestor
andparent
settings for the block variation (#48424). - Allow users to select multiple block style variations (#14598).
- Allow block styles scoped to variations (#40621).
- Add PHP version of
wp.blocks.registerBlockVariation
(#47170). - #65851
Content Modelling
- ๐ ๏ธ #60397
- ๐ ๏ธ #60954
- Offer the ability to inject and manipulate inline dynamic tokens for blocks using Rich Text (#18490 and #21932).
- Dynamic replacement of server-provided content in blocks and in HTML attributes (#39831).
- #64756
- #64870
Various
- Offer a way to define the list of disallowed blocks to mitigate the limitation of the currently supported allowed list (#49592, #38500).
- #58233
- #62439
- #38767
- ๐คIntroduce classification of block validation types to make block deprecation handling more flexible (#21703).
- Introducing more robust permission handling across the various capabilities (block registration, locking, etc) so administrators can define what blocks are available for different user roles (or even what capabilities of individual blocks are to be exposed). See more in Phase 3: Collaboration, Block Library.
- Proper server-side APIs for block modification (#6494)
- Expose parsed block data directly through REST API endpoints (Trac#53603, POC #2649)
- Expand
get_posts
$filter parameter to allow a blocks as a return shape (Trac#53602) - Correctly pass parent block context in REST API call for Server-side Render Component (#40714).
- #56278
- Find a way to cover multiblock plugins where you have all of them installed but they wouldnโt get activated until you insert them for the first time. Itโs mostly a UX challenge because you could show them in the inserter like blocks from the Block Directory, but they could get higher priority.
- Better UX to prevent nesting some blocks into self that crashes the editor (the same Reusable Block, Template Part, Post Content, etc).
- "Undo trap": Avoid getting stuck in an editing state (#8119).
- Blocks with different versions like Log In/Out block, or different state like Navigation blocks. Explore how to build UI that allows full page control over global states like the user session, or viewport. Feed that information into the block context so users can edit and preview blocks in different contexts (#57719, see also related discussion #38108).
- Refactor the block parser to work asynchronously (#15674).
Developer Experience
Develop strategies for loading JavaScript on the frontend - head vs footer, but also frontend hydration techniques explored in Implement different hydration techniques block-hydration-experiments#12.
This can be a good first step, but acknowledging that we need more than that in the near future: probably asycn/defer
(something like what @adamsilverstein proposed in WordPress/performance#168) or even do the jump to ESM.
Apart from that, I think we should explore a mechanism to export different code depending on the context (Edit, Save, or Frontend): WordPress/block-interactivity-experiments#11
Thank you @luisherranz for your feedback. I included your suggestions together with references.
I also added two more items that I missed initially but have been following closely for a long time:
โ๏ธ Completed in WordPress 6.1 cycle
Block Registration
- Reference PHP file necessary for the block from
block.json
. It could be a new settingrender
inblock.json
(Trac#53148, WordPress/wordpress-develop#3222, #42430). - Better imports for core blocks in the Gutenberg repository and
@wordpress/block-library
to make it easier to consume individual blocks (#42258). - Combine block metadata during the build process into one file to avoid loading too many
block.json
files for core blocks (see WordPress/wordpress-develop#2252).
Block Assets
Some of the details were tracked in #33542.
- Add multiple assets per type (
editorScript
,script
) to bring complete consistency to what we already have for styles (WordPress/wordpress-develop#3108). - Change the default behavior for
viewScript
when therender_callback
is present in the block definition (Trac#56470). - Allow plugins to modify a theme.json file in a similar way themes do (#41707).
Documentation
โ๏ธ Completed in WordPress 6.2 cycle
Block Modification
- WordPress 6.2: Injecting dynamic data to block HTML markup in PHP (#42485, https://make.wordpress.org/core/2022/08/19/a-new-system-for-simply-and-reliably-updating-html-attributes/).
Mentioning this in case you think it'd be good to include it:
It's related to this item, but I wasn't sure it was talking about the same use case:
Explore how to build UI that allows full page control over global state like the user session and feed it into the block context so users can preview blocks in different contexts.
Mentioning this in case you think it'd be good to include it:
It's related to this item, but I wasn't sure it was talking about the same use case:
Yes, it was the same item so I linked it and rephrased as:
Blocks with different versions like Log In/Out block, or different states like Navigation block. Explore how to build UI that allows full page control over global states like the user session, or viewport. Feed that information into the block context so users can edit and preview blocks in different contexts.
Unrelated to that I added three action items based on recently triaged issues:
Another set of updates applied:
- Added to Block Variations: Allow users to select multiple block style variations (#14598).
- Added to Block Variations: Block Variations: Add PHP version of
wp.blocks.registerBlockVariation
(#47170). - Replaced in Documentation
Refresh README for @wordpress/blocks, which is outdated.with: Address confusing experiences when searching resources related to learning about block development (#48638). - Added to Documentation:
spacing.blockGap
explanation is inadequate for custom blocks (#43921). - Removed as it seems to be not general enough and useful only for a few core blocks: Allow setting role HTML attributes on blocks (#46002).
Sharing in case it relates to this effort and want it tracked here: one of the big tasks related to styles is being able to absorb block styles (CSS as well as the __experimentalStyle
from block.json
) as part of the global styles merge algorithm #45198
This is: by providing block styles in a structured way, they can be merged with core, theme, and user styles into a single stylesheet. Then, decide what of those should be shipped to the front-end.
Sharing in case it relates to this effort and want it tracked here: one of the big tasks related to styles is being able to absorb block styles (CSS as well as the
__experimentalStyle
fromblock.json
) as part of the global styles merge algorithm #45198
That's a great one. I included it for now in the "Block Assets" section as it sort of removes the need to write specific regular CSS. This is also going to be another helpful performance optimization.
Added new item in the Block Attributes section:
- Optional support that optimizes handling blocks for usage as structured data (#49437).
Consider adding SlotFill for the block's description in the sidebar to allow custom code. At the moment we use block's description which is a simple string. However, it would be sometimes useful to include some custom HTML code like
<a />
.
Now tracked in #49887
Consider adding SlotFill for the block's description in the sidebar to allow custom code. At the moment we use block's description which is a simple string. However, it would be sometimes useful to include some custom HTML code like
<a />
.Now tracked in #49887
Thank you so much @jeherve. I linked the issue in the description.
I also removed the following item from the Block Attributes section:
- Check whether we still need a special way or if it's only a documentation effort to expose attributes for the client in the frontend context with Interactive API in place (related to WordPress/block-interactivity-experiments#13).
There is an alternative approach that allows exposing attributes to the client covered in the Proposal: The Interactivity API โ A better developer experience in building interactive blocks.
I added a new item in the Block Attributes section:
- Provide a general solution to customize the list of allowed blocks for inner blocks with block attributes (#15682).
I added a new item to the Backlog in the Block Interactions section, it's in progress:
- Offer a way to ungroup blocks with a new type of transform (#50385)
In #51005, @Mamaduka is championing the proposal to create a formal API for registering new Block Supports options for blocks.
I found myself wanting for blocks to be able to export data they have. Case in point: trying to implement a feature which shows in the site editor sidebar the navigations available in a template part, when that template part is visible in the frame. I can get the client ids of the navigation blocks, but then I have to reimplement the whole logic of getting navigation details from the API although the blocks rendered already did it. I want to ask client id X give me data :)
Something that's been on my mind lately is how we have a lot of different APIs to do with restricting blocks:
- The
templateLock
prop onInnerBlocks
andtemplateLock
block editor setting - The
lock
block attribute - The
allowedBlocks
prop onInnerBlocks
andallowedBlocks
block editor setting - The
parent
attribute inregisterBlockType
- The
useBlockEditingMode
hook
I wonder if we can consolidate some of these APIs or otherwise make them more coherent. Perhaps something to think about in the context of Block API improvements.
Update
Top Things
Completed during WP 6.3 & 6.4
- #48286
- #39439
- #49868
- #50866
- #51056
- #44410
- Stabilize experimental selectors API as top-level
selectors
field (Trac #57585, #46496). - Develop strategies for loading JavaScript on the frontend - head vs footer, async/defer (related proposal WordPress/performance#168). It could also cover frontend hydration techniques explored in WordPress/block-interactivity-experiments#12.
- No simple way to enqueue styles and scripts for a group of blocks (#41821).
- Offer a way to ungroup blocks with a new type of transform (#50385).
- Docs: Address confusing experiences when searching resources related to learning about block development (#48638).
- Docs: How to work with the templates defined with
render
(#47826).
On hold
Added
- Introducing more robust permission handling across the various capabilities (block registration, locking, etc) so administrators can define what blocks are available for different user roles (or even what capabilities of individual blocks are to be exposed). See Phase 3: Collaboration, Block Library.
- Offer a way to define the list of disallowed blocks to mitigate the limitation of the currently supported allowed list (#49592, #38500).
- #50229
- #54112
- #53847
Regarding viewStyle
: The initial issue was closed, therefore I'm posting this here:
I actually think this would be great to have. Not having this assumes, frontend styles can be used 1:1 in the editor. I've encountered many situations where that's just not true - it has gotten a lot better with the apiVersion: 2
blocks, but there are still many cases where I want to enqueue a style only in the frontend. Examples:
- Very complex blocks - where I will develop a very specific block editing experience in the editor with components and editor styles and want to load the "real" styles for the (dynamically) rendered block only in the frontend.
- JavaScript-heavy frontend - the most basic example being slider. I will have a lot of CSS which only styles the JS-initialized version of the block and they just don't need to be loaded in the editor.
- There are cases where the frontend styles are just not used in the editor (and I know that while developing), so it's a performance slow-down if you have many such blocks. But there are also cases where the frontend styles may just "destroy" the editing experience of a block because they just don't work in that context.
Since we already have script
, viewScript
and editorScript
I think it would keep clarity by also introducing viewStyle
. Even if there are not many use cases where it's used, I don't see a real disadvantage.
It would also mean, that all assets of a block in all contexts could be managed through block.json
- which would be great.
@gaambo, thank you for feedback.
Regarding viewStyle: The initial issue was closed, therefore I'm posting this here:
Can you share the link to the issue? I would like to learn why it was closed and potentially reopen. There is still an item listed in the Backlog that signals that we have this API on the roadmap but it doesn't link to the issue you referred to:
"Introduce viewStyle
for styles used only on the front end to align with scripts and viewScript
."
Can you share the link to the issue? I would like to learn why it was closed and potentially reopen. There is still an item listed in the Backlog that signals that we have this API on the roadmap but it doesn't link to the issue you referred to:
@gziolo
I've found #33542 where viewStyle
was mentioned. I can open a new issue if required and we can link it in this overview issue.
I've found #33542 where
viewStyle
was mentioned. I can open a new issue if required and we can link it in this overview issue.
It was me who closed the issue with the note I moved the item to this issue - the one I quoted in #41236 (comment) ๐
If you could open a new issue that would be great, thank you! I'm happy to see this feature implemented if it's going to be useful for custom blocks.
Update
Completed during WP 6.4
Added
Needs review
- Introduce
viewStyle
for styles used only on the front end to align with scripts andviewScript
(#55492, WordPress/wordpress-develop#5531).
Update
Completed during WordPress 6.5 cycle
- #53300
- #56803
- #54904
- #53705
- #54579
- Create a system for inserting site/post metadata into generic blocks (#37753.
- Provide a general solution to customize the list of allowed blocks for inner blocks with block attributes (#15682, #16560, #14515).
- Start using JavaScript Modules and Import Maps (#36716).
- Support for deferred block variation initialization on the server has been added, which improves performance (https://core.trac.wordpress.org/ticket/59969).
- Introduce view style for styles used only on the front end to align with scripts and view script (#54491, https://core.trac.wordpress.org/ticket/59673).
- Asset registration file .asset.php has been made optional for blocks (https://core.trac.wordpress.org/ticket/60460).
- #56305