Allow grid to use style variation blockGap values for columns calculation#75360
Allow grid to use style variation blockGap values for columns calculation#75360tellthemachines merged 5 commits intotrunkfrom
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. |
|
Size Change: +113 B (0%) Total Size: 3 MB
ℹ️ View Unchanged
|
ramonjd
left a comment
There was a problem hiding this comment.
LGTM and works a treat. 🌮 Approving pending the Core backport.
Kapture.2026-02-10.at.12.56.41.mp4
Up to you whether you want to reuse the getVariationNameFromClass style variation util, but if not, it could be worth a comment to say that the logic is intentionally mirrored.
Also, you probably have an extra PHP test in WP_Block_Supports_Layout_Test on your radar as part of the Core backport
| * | ||
| * @return {string|null} The name of the first registered variation, or null if none found. | ||
| */ | ||
| function getVariationNameFromClass( className, registeredStyles = [] ) { |
There was a problem hiding this comment.
Is there anyway to reuse getVariationNameFromClass, or is it preferable not to have a cross-hook dep?
There was a problem hiding this comment.
Oh well spotted, yes I think we can reuse it.
lib/block-supports/layout.php
Outdated
| foreach ( $classes as $name ) { | ||
| if ( str_starts_with( $name, 'is-style-' ) ) { | ||
| $match = substr( $name, strlen( 'is-style-' ) ); | ||
| if ( 'default' !== $match ) { | ||
| $matches[] = $match; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foreach ( $matches as $variation ) { | ||
| foreach ( $registered_styles as $style ) { | ||
| if ( isset( $style['name'] ) && $style['name'] === $variation ) { | ||
| return $variation; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Not a blocker and feel free to ignore, but I was trying to work out a way to save a loop or two here. Then I remembered that there are presumably a handful of classes and registered styles per block, so it's all negligible. Posting anyway so I can get if off my chest 😅
$registered_names = array_filter( array_column( $registered_styles, 'name' ) );
$prefix = 'is-style-';
$len = strlen( $prefix );
foreach ( explode( ' ', $class_name ) as $class ) {
if ( str_starts_with( $class, $prefix ) ) {
$variation = substr( $class, $len );
if ( 'default' !== $variation && in_array( $variation, $registered_names, true ) ) {
return $variation;
}
}
}There was a problem hiding this comment.
Nice, I can use that, thanks!
lib/block-supports/layout.php
Outdated
| $matches = array(); | ||
| $classes = explode( ' ', $class_name ); | ||
|
|
||
| foreach ( $classes as $name ) { |
There was a problem hiding this comment.
Shell we use here array_filter along with array_map?
There was a problem hiding this comment.
I updated this block to Ramon's suggestion above.
lib/block-supports/layout.php
Outdated
|
|
||
| foreach ( $classes as $name ) { | ||
| if ( str_starts_with( $name, 'is-style-' ) ) { | ||
| $match = substr( $name, strlen( 'is-style-' ) ); |
There was a problem hiding this comment.
| $match = substr( $name, strlen( 'is-style-' ) ); | |
| $match = substr( $name, 9 ); // 'is-style-' is 9 characters |
As we already know it's 9 why we calculate it?
There was a problem hiding this comment.
That's just to avoid a magic number; by using strlen the code is self-explanatory.
Co-authored-by: Mukesh Panchal <[email protected]>
|
Feedback addressed and test added! Merging this now. Sync PR in WordPress/wordpress-develop#10906. |
| /* | ||
| * Register a style variation with a custom blockGap value for testing. | ||
| */ |
Co-authored-by: tellthemachines <[email protected]> Co-authored-by: andrewserong <[email protected]> Co-authored-by: ramonjd <[email protected]>
What?
Follow-up to #74725 made possible by #74529.
Now that we output blockGap values for block style variations, we should ensure the grid block columns computation takes any active variation value into account.
Testing Instructions