-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 variations: Add class name #6602
base: trunk
Are you sure you want to change the base?
Conversation
* If no `isActive` property is defined, all `attributes` specified by a variation | ||
* are compared to the given block's to determine if the variation is active. | ||
* | ||
* @param WP_Block_Type $block_type Block Type. |
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.
Should we change this to accept a block type name instead to make it consistent with the client-side getActiveBlockVariation()
? It will mostly mean some additional (and somewhat redundant) calls to WP_Block_Type_Registry::get_instance()->get_registered()
to get the WP_Block_Type
instance.
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.
Personally, I don't think so if the only reason is for that level of consistency. I think it's correct for it to accept the WP_Block_Type
instead.
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.
Yeah, I was leaning that way initially, too. Now I've seen how similar a lot of this code here is to the client side (e.g. or generated-classname.php
/generated-class-name.js
or getBlockDefaultClassName
/wp_get_block_default_classname
) that I'm starting to reconsider 😅
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 would rather update the client side code to be more flexible. Most of these API operate on the string or object passed to represent a block type. However internally, it often requires converting the string to the block type object.
I'm assuming we're going to need to open a GB counterpart to deal with the same logic for adding variation class names in the editor too? |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Oh, good point! I was originally just focused on dynamic blocks, but you're right, we'll probably need something somewhere around |
@tjcafferkey Mind giving that a stab? 😄 |
I'd love to! |
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 Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
continue; | ||
} | ||
|
||
foreach ( $attributes as $attribute ) { |
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 don't think a fallback like this can work well due to the nuances of object
attributes handling. An example is query
attribute of Query Loop block.
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.
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.
Removed the fallback in 9d54dd6.
5bdad7e
to
18eb15d
Compare
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.
That looks like a good improvement for block variations to automatically generate a unique class name for them 👍
* @return array Block CSS classes and inline styles. | ||
*/ | ||
function wp_apply_generated_classname_support( $block_type ) { | ||
function wp_apply_generated_classname_support( $block_type, $block_attributes ) { |
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.
It feels like there should be a default value provided for backward compatibility.
If any 3rd party code uses it, it passes only one argument today.
It looks like there are no tests as it would caught that.
* If no `isActive` property is defined, all `attributes` specified by a variation | ||
* are compared to the given block's to determine if the variation is active. | ||
* | ||
* @param WP_Block_Type $block_type Block Type. |
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 would rather update the client side code to be more flexible. Most of these API operate on the string or object passed to represent a block type. However internally, it often requires converting the string to the block type object.
if ( | ||
isset( $variation['isActive'] ) && | ||
is_array( $variation['isActive'] ) && | ||
! empty( $variation['isActive'] ) |
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.
This can be simplified because empty
also performs isset
check internally.
|
||
foreach ( $attributes as $attribute ) { | ||
if ( | ||
! isset( $block_attributes[ $attribute ] ) || |
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.
Two notes here:
- does it cover default values?
- can be the value equal to
null
?isset
can be tricky with that value
18eb15d
to
cf21ed0
Compare
For a given block
my/block
with a variationmyvariation
that's registered on the server side (viavariation_callback
orget_block_type_variations
), add a classwp-block-my-block-myvariation
to the block wrapper.Design choices
This PR introduces a new function called
get_active_block_variation()
, which is modelled after its client-side counterpart,getActiveBlockVariation()
. The signatures are almost identical, the major differences being:WP_Block_Type
object. Some discussion on that here.scope
argument, which is forwarded togetBlockVariations()
. It is used to specify if a variation should show up in theinserter
, intransforms
, or if it should only be used internally by theblock
. Since the server-side's counterpartWP_Block_Type::get_variations()
doesn't support ascope
argument, it is also not implemented inget_active_block_variation()
. (It should be possible to add it later on to both functions if we so wish.)No separate block-supports. This doesn't introduce a separate block-supports field to control the addition of a generated variation class name; instead, it relies on the existing
className
block-supports, as does the block's generated class name (i.e. thewp-block-my-block
thing). The reason for this is that the addition of an extra class name was considered innocuous and similar enough to the generated block class name that it didn't merit introducing a separate block-supports, which would otherwise increase the API surface.Inference. Note that whileget_active_block_variation
respects theisActive
field to determine the variation of a block given its attributes, it also has a fallback mechanism, which is to compare the variation's set of definingattributes
to the block's.I believe that this is a reasonable technique to determine a block variation, especially in the absence of anisActive
field (which on the server side is furthermore limited to an array of attributes that need to be considered in determining a variation, as opposed to the client side, where arbitrary callbacks are allowed forisActive
).If we agree with this fallback mechanism, I'm inclined to add it to the client side'sgetActiveBlockVariation
as well.Edit: Removed the fallback mechanism, see https://github.com/WordPress/wordpress-develop/pull/6602/files#r1616104632.
TODO
isActive
is an array of attribute names. SeeisActive
string array gutenberg#62088getActiveBlockVariation
return variation with highest specificity gutenberg#62031getActiveBlockVariation
unit tests.Testing Instructions
Test with a Block Theme (such as TT4) that uses Template Part block (and ideally also Navigation Link blocks), as those are among the few blocks with server-side registered variations. Inspect the page source and verify that the aforementioned blocks have indeed a variation-specific classname added, e.g.
wp-block-template-part-area_footer
orwp-block-navigation-link-page
.Screenshots
Trac ticket: https://core.trac.wordpress.org/ticket/61265
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.