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

Check child layout exists before generating classname. #61392

Merged
merged 2 commits into from
May 7, 2024

Conversation

tellthemachines
Copy link
Contributor

What?

In writing a test to accompany WordPress/wordpress-develop#6493, I realised the child layout classname was being generated for every block with layout, even if it doesn't have child layout. This PR adds the classname generation and remaining child layout logic inside a condition, so it only runs if there is child layout defined.

It also adds a test to check the child layout classname is generated.

I guess a further improvement could be to create a dedicated function for outputting child layout styles, similar to gutenberg_get_layout_style, which would allow us to test the actual styles output, though that may have limited value in that it will produce more test strings that will have to be updated when child layout rules change.

Testing Instructions

Run the PHP tests and make sure they pass: npm run test:php

Testing Instructions for Keyboard

Screenshots or screencast

@tellthemachines tellthemachines added [Type] Enhancement A suggestion for improvement. [Feature] Layout Layout block support, its UI controls, and style output. labels May 6, 2024
@tellthemachines tellthemachines self-assigned this May 6, 2024
Copy link

github-actions bot commented May 6, 2024

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: tellthemachines <isabel_brison@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment on lines 688 to 691
// Prep the processor for modifying the block output.
$processor = new WP_HTML_Tag_Processor( $block_content );

// Having no tags implies there are no tags onto which to add class names.
// Having no tags implies there are no tags onto which to add class names.
Copy link
Contributor

@andrewserong andrewserong May 6, 2024

Choose a reason for hiding this comment

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

The whitespace here and below / before if ( ! $block_supports_layout && ! empty( $outer_class_names ) ) { appears to be off (extra indentation).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for noticing! Not sure what happened here, the auto-formatter usually knows what it's doing 🤔

Comment on lines 672 to 674
* Add to the style engine store to enqueue and render layout styles.
* Return styles here just to check if any exist.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Tiny nit: the indentation here appears to be one character off.

Comment on lines 622 to 627
* If columnSpan or columnStart is set, and the parent grid is responsive, i.e. if it has a minimumColumnWidth set,
* the columnSpan should be removed once the grid is smaller than the span, and columnStart should be removed
* once the grid has less columns than the start.
* If there's a minimumColumnWidth, the grid is responsive. But if the minimumColumnWidth value wasn't changed, it won't be set.
* In that case, if columnCount doesn't exist, we can assume that the grid is responsive.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation is one character off.

@andrewserong
Copy link
Contributor

Thanks for the follow-up! Guarding behind a check for the child layout styles sounds like a good idea 👍

I haven't taken this for a spin yet, but plan to later on today. Just left a few little nits about whitespace indentation.

@ramonjd
Copy link
Member

ramonjd commented May 6, 2024

I'm not sure how to test this manually. I think it's just my ignorance of how it's supposed to work 😄

I tried to follow the example in the test.

Between trunk and this branch the frontend HTML looks the same to me:

Paragraph has a colspan

<div class="wp-block-group is-layout-grid wp-container-core-group-is-layout-1 wp-block-group-is-layout-grid">
<p class="wp-container-content-1">Test</p>
</div>

Paragraph has no colspan

<div class="wp-block-group is-layout-grid wp-container-core-group-is-layout-1 wp-block-group-is-layout-grid">
<p>Test</p>
</div>

@tellthemachines
Copy link
Contributor Author

I'm not sure how to test this manually.

Manually you could compare the generated classnames in trunk and on this branch, for a page that has both layout and child layout blocks. In trunk, the child layout classname is generated for all blocks with layout, whether they have child layout or not. On this branch, it's only generated for blocks with child layout. So for the same markup, your generated classname on trunk might be wp-container-content-12 whereas on this branch it'll be a lower number, e.g. wp-container-content-3. (The classname is still generated for all blocks with child layout, whether they output CSS or not, but it only gets added to the block if there is CSS output, so you may not see wp-container-content-0 appear in the markup.) Does that make sense?

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.

Thanks for the explanation, I can see the difference in the markup now:

Trunk: (the number of the content container is 3):

image

This PR: (the number for the content container is 7):

image

Layout and child layout rules appear to be working as on trunk as far as I can tell.

One of the other benefits of this PR IMO is that by nesting the child layout rules under the if statement, it's now clearer at a glance that all that code is just for the child layout rules, which improves readability to me 👍

LGTM!

@tellthemachines
Copy link
Contributor Author

it's now clearer at a glance that all that code is just for the child layout rules, which improves readability

Yeah I was considering splitting the style generation into a different function as a further improvement, but that can be for later 😅

Thanks for reviewing and testing!

@tellthemachines tellthemachines merged commit 7b221c0 into trunk May 7, 2024
62 checks passed
@tellthemachines tellthemachines deleted the add/check-child-layout branch May 7, 2024 05:13
@github-actions github-actions bot added this to the Gutenberg 18.4 milestone May 7, 2024
@tellthemachines tellthemachines added the Needs PHP backport Needs PHP backport to Core label May 7, 2024
@tellthemachines tellthemachines removed the Needs PHP backport Needs PHP backport to Core label May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Layout Layout block support, its UI controls, and style output. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants