Skip to content
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

Add new grid layout type #4625

Closed

Conversation

tellthemachines
Copy link
Contributor

@tellthemachines tellthemachines commented Jun 16, 2023

Trac ticket: https://core.trac.wordpress.org/ticket/58554

Introduces the PHP changes from WordPress/gutenberg#49018 (except for the kses filter which will be dealt with separately) and the changes to layout.php from WordPress/gutenberg#49050, because they build upon the changes from the first PR.

To test this change, check out this branch locally and update the following npm packages:

@wordpress/block-editor": "11.8.0"
"@wordpress/block-library": "8.8.0"
"@wordpress/blocks": "12.8.0"

Then run npm run postsync-gutenberg-packages followed by npm run build:dev (assuming you're building from src, otherwise run npm run build)

Then, in the post editor, paste the following block markup:

<!-- wp:group {"layout":{"type":"grid","minimumColumnWidth":"8rem"}} -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>grid</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>row</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>stack</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>group</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

The grid should look fine in the editor; on the front end it won't show the correct column number without the kses patch to support repeat.


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.

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing nicely for me with the changes applied along with repeat allowed via #4637:

✅ Grid layout test markup works as expected, using auto-fill rule in grid-template-columns.
✅ Updating the markup to use a columnCount attribute with an integer correctly outputs a grid-template-columns rule with a number based approach for the number of columns.
blockGap output works correctly, and parses spacing presets correctly.

Values are passed to style engine for output, which is responsible for filtering CSS values via safecss_filter_attr, so this all looks good to me!

Copy link
Contributor

@costdev costdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @tellthemachines! Looks good to me.

Just a couple of questions about some of the checks that exist in these changes and elsewhere in the file. Not a blocker for this PR, but possibly a follow-up improvement in this file depending on the answers?

}
$gap_value = trim( $combined_gap_value );

if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
Copy link
Contributor

@costdev costdev Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think $gap_value can ever be null by this point. Should this be '' !== $gap_value, or is the condition not needed?

Although I see this is how it's done elsewhere in the file. If it can't be null by this point, then this could maybe be updated elsewhere in the file in a separate issue to reduce checks and make things a bit cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, better look into cleaning all the checks up at once in a separate issue.

foreach ( $gap_sides as $gap_side ) {
$process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value );
// Get spacing CSS variable from preset value if provided.
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
Copy link
Contributor

@costdev costdev Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't $process_value always be a string after the line above? If not, what other type might it be, and is this safe when interpolated on the last line of the foreach()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like $gap_value could be an array, e.g.,

array(
   'top' => '12px', // column
   'left' => '11px', // row
)

So I think the extra string check makes sense if we're fetching the value of $gap_value['top'] for example?

Copy link
Contributor

@costdev costdev Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking if $gap_value is a string makes sense. I was referring to the is_string( $process_value ). This seems like it's always a string already - The fallback value is documented as a string, values always appear to have units like px or em (such as '12px' if getting top from your example), and $process_value is appended to a string without any conversion at the bottom of the foreach():

$combined_gap_value .= "$process_value "

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$process_value might possibly be null, but I don't think it could be anything other than string or null.

This logic is repeated for the flex layout type, so might also be worth looking into separately and fixing all instances together.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I didn't make it down to $combined_gap_value .= "$process_value ";... now I get it 🤦

I'm pretty sure the assumption is "yes", it's a string, so the extra check is probably redundant (?)

I guess folks could add anything as a style value in theme.json (and they probably do), even if it's contrary to the theme.json schema rules.

Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -287,6 +287,44 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
);
}
}
} elseif ( 'grid' === $layout_type ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need @since annotation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks! I forget about them half the time 😅

foreach ( $gap_sides as $gap_side ) {
$process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value );
// Get spacing CSS variable from preset value if provided.
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like $gap_value could be an array, e.g.,

array(
   'top' => '12px', // column
   'left' => '11px', // row
)

So I think the extra string check makes sense if we're fetching the value of $gap_value['top'] for example?

@tellthemachines
Copy link
Contributor Author

Finally got e2e to pass on this branch so will try committing again 🤞

@tellthemachines
Copy link
Contributor Author

Thanks for the reviews folks!

Committed in r55975 / 6188537.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

4 participants