Blocks: Add initial API for Block Variations (formerly Patterns)#18270
Conversation
| * | ||
| * @return {Object} Updated state. | ||
| */ | ||
| export function blockPatterns( state = {}, action ) { |
There was a problem hiding this comment.
This is nearly identical to blockStyles. In theory, we could extract duplicated code and abstract it away. However, I'm not convinced that this will make it easier to read in the long run. If you think this is a better way to go, I'm happy to refactor.
There was a problem hiding this comment.
This is nearly identical to
blockStyles. In theory, we could extract duplicated code and abstract it away. However, I'm not convinced that this will make it easier to read in the long run. If you think this is a better way to go, I'm happy to refactor.
Since we're inheriting the logic, I'm not overly concerned with addressing it here, but a point for both this and the blockStyles upon which it's based:
- Do we really need to track a value for each registered block type? Can't this be something where we just manage the normalization to an empty array from the selector? Seems like it may make the implementation a little simpler, and avoid the (albeit minimal) memory usage involved with tracking a bunch of key/values for blocks without patterns.
There was a problem hiding this comment.
That's a good point, indeed. I guess this would force us to use memoization for the selector, but it seems like a good trade-off given that the implementation would get much simpler in the reducer. I'll open a follow-up shortly after I merge this one which is going to explore this idea for both reducers.
66f451f to
2cc172b
Compare
| * @param {string} blockName Name of the block (example: “core/columns”). | ||
| * @param {WPBlockPattern} pattern Object describing a block pattern. | ||
| */ | ||
| export const __experimentalRegisterBlockPattern = ( blockName, pattern ) => { |
There was a problem hiding this comment.
@youknowriad - do we even need those APIs in the first place? I followed registerBlockStyle and unregisterBlockStyle but we could also use data API directly. What do you think?
There was a problem hiding this comment.
@youknowriad - do we even need those APIs in the first place? I followed
registerBlockStyleandunregisterBlockStylebut we could also use data API directly. What do you think?
In my opinion, I think we should have them:
- It's slightly more convenient / obvious to use, than via the equivalent data actions
- Consistency for consistency's sake
There was a problem hiding this comment.
I used this wp.blocks.* API in the examples presenting how you can tweak the Columns block with custom patterns so maybe it's indeed more convenient. The benefit is also that you don't have to worry whether the core/blocks store is initialized as it's embedded in the module. Wheres using the data module directly is prone to error in that regard.
Based on #18283, is there any concern with this just being the block templates array syntax? I'm not really sure what's hinted at here as requiring further consideration. |
I don't have any concerns about this part. It's super solid, well-documented and extensively tested on production. I have been thinking about the name which is less of an issue but I noticed that we use |
It's a good point. I'm not sure what the best option is here, since Another option, if we standardize on "template" as the name for this format, is to include a prefix describing it for a pattern as applying to its inner blocks, i.e. |
aduth
left a comment
There was a problem hiding this comment.
Looks pretty flawless to me! A few minor points, but nothing blocking.
| * @param {string} blockName Name of the block (example: “core/columns”). | ||
| * @param {WPBlockPattern} pattern Object describing a block pattern. | ||
| */ | ||
| export const __experimentalRegisterBlockPattern = ( blockName, pattern ) => { |
There was a problem hiding this comment.
@youknowriad - do we even need those APIs in the first place? I followed
registerBlockStyleandunregisterBlockStylebut we could also use data API directly. What do you think?
In my opinion, I think we should have them:
- It's slightly more convenient / obvious to use, than via the equivalent data actions
- Consistency for consistency's sake
| * | ||
| * @return {Object} Updated state. | ||
| */ | ||
| export function blockPatterns( state = {}, action ) { |
There was a problem hiding this comment.
This is nearly identical to
blockStyles. In theory, we could extract duplicated code and abstract it away. However, I'm not convinced that this will make it easier to read in the long run. If you think this is a better way to go, I'm happy to refactor.
Since we're inheriting the logic, I'm not overly concerned with addressing it here, but a point for both this and the blockStyles upon which it's based:
- Do we really need to track a value for each registered block type? Can't this be something where we just manage the normalization to an empty array from the selector? Seems like it may make the implementation a little simpler, and avoid the (albeit minimal) memory usage involved with tracking a bunch of key/values for blocks without patterns.
Co-Authored-By: Andrew Duthie <andrew@andrewduthie.com>
Yes, this one is longer but it probably fits better. @youknowriad what do you think? I know that you used gutenberg/packages/block-library/src/columns/index.js Lines 27 to 31 in e1fb88e |
| return uniqBy( [ | ||
| ...get( blockType, [ 'patterns' ], [] ), | ||
| ...get( state, [ blockType.name ], [] ), | ||
| ], ( style ) => style.name ); |
There was a problem hiding this comment.
I think this should read ( pattern ) => pattern.name :)
There was a problem hiding this comment.
Refactor planned in #18398 is going to fix it.
|
nice work here 👍 |
Description
Part 1 of #16283.
This is my proposal for the initial API:
innerBlocksneeds more thoughts. I would prefer to defer this decision until the very first block is converted to use this new API. See more detailed explanation in #18270 (comment).New APIs proposed in
@wordpress/blockswp.data.*Actions
Selectors
How has this been tested?
npm run testAll added unit tests should pass.