-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Block Styles: Add extended version of register block style functions #61029
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate the efforts of splitting this PR into smaller ones. It makes reviewing way easier.
How is this function different than the one in Core?
In core, register_block_style only allows registering a style for a single block. The PR allows registering the same style for multiple blocks. |
Ah makes sense, maybe let's add a very small unit test (to be brought to Core later) |
I see that the |
This version allows you to pass an array of block types to register a single block style for, instead of having to call Originally, this version also explicitly handled when As the block style variations feature evolved, that approach didn't work for applying variations within blocks that have other variations already applied. In #57908, the theme.json resolver takes any So long story, short, what we're left with is the requirement that theme authors could register a style across multiple blocks at once e.g. #56234 (comment). |
I was a bit slow with my reply 🐢 Hopefully, the extra context helps for posterity as well as explaining the new Edit: For the record, if this is merged before the related changes for #57908, the |
Will this also be added to the Javascript version of the API? If so, this will close #50256 |
I've had a go at updating the JS side of the block style registration in ba8c647. It would be great to get a sanity check there and make sure there isn't a better approach. I did look at batching the dispatched actions in Happy to change tack if needed 🙂 |
Size Change: +40 B (0%) Total Size: 1.75 MB
ℹ️ View Unchanged
|
What's blocking this PR. Anything you need help with? |
d628645
to
dba1d19
Compare
const original = deepFreeze( {} ); | ||
|
||
let state = blockStyles( original, { | ||
type: 'ADD_BLOCK_STYLES', | ||
blockName, | ||
blockNames: [ blockName ], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blockNames: [ blockName ], | |
blockNames: blockName, |
I think we should test this with a string, to check it works both ways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well this breaks the tests, so I guess we need to be sure to cast it to an array!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking a look at this part of the PR 👍
I'm not sure we need to cover the possibility of someone dispatching their own action object for this. If they use addBlockStyles
to create the action that will handle the casting any string given to an array.
Maybe I could actually use addBlockStyles
within the tests here to sort of cover your suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've switched the tests to use addBlockStyles
which illustrates the use case with a single block name as a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor nit, but LGTM
This reverts commit 88deb8b.
cf7ac41
to
9fac655
Compare
Thanks for the reviews and testing everyone 👍 I'll get this merged and address anything further in follow-ups. |
Backport for these changes can be found in: WordPress/wordpress-develop#6609 |
Related:
Note: The changes in this PR are being split out from #57908 to make that more manageable
What?
The
WP_Block_Styles_Registry
class was markedfinal
so it can't be extended in Gutenberg to allow block styles to be registered across multiple block types or with a style data object.This PR adds a global function,
gutenberg_register_block_style
, that will allow Gutenberg to support the extended block style registration until it is in Core and that version of WordPress is the minimum supported by Gutenberg.Additionally, the JS
registerBlockStyle
function has been updated to match i.e. you can pass it an array of block types to register the block style for.Why?
The planned section styling feature requires the ability to share block style variations across block types.
It is desired to allow registration of such block styles through both theme.json and the existing
register_block_style
PHP function. The proposed function here helps make this possible untilregister_block_style
can be updated in core.Testing Instructions
string
block name orarray
of block namesScreenshot