Skip to content

Layouts: Update editor layout definitions to better select the first block #70003

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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

yogeshbhutkar
Copy link
Contributor

@yogeshbhutkar yogeshbhutkar commented Apr 28, 2025

What?

Closes #69989

This PR fixes the unintentional top margin (margin-block-start) on the post content block when placed within the template query. (Likely introduced in #66003)

Why?

This happens because the following line tries to apply a top margin of 0 px to the first child.

selector: ' > :first-child',

The problem occurs when there are multiple <style> and <SVG> elements before the first element within the editor. Refer to the following DOM structure:

DOM

These elements hold the preview styles related to the content block.

Ref.

return <div { ...blockPreviewProps }></div>;

How?

Since all the blocks within the editor have a .wp-block class, we can update the selection logic to be:
' > :nth-child(1 of .wp-block)'

Because the .wp-block class is not present in the front-end and since the problem doesn't occur in there (as the <style> and <svg> tags are not rendered within the parent block), I didn't feel the need to update the rules for the corresponding PHP file.

Testing Instructions

  1. Create a new post, paste the following markup in the code editor, and save it.
Click to toggle the code
<!-- wp:group {"className":"is-style-section-4","style":{"spacing":{"padding":{"right":"var:preset|spacing|40","left":"var:preset|spacing|40","top":"var:preset|spacing|40","bottom":"var:preset|spacing|40"}}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group is-style-section-4" style="padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)"><!-- wp:group {"style":{"layout":{"columnSpan":1,"rowSpan":1}},"layout":{"type":"constrained"}} -->
<div class="wp-block-group"><!-- wp:group {"layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group"><!-- wp:paragraph {"style":{"elements":{"link":{"color":{"text":"#29c8f8"}}}}} -->
<p class="has-link-color">Welcome to <a href="http://www.wordpress.org">WordPress</a>. This is your first post. Edit or delete it, then start writing! Welcome to WordPress. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"elements":{"link":{"color":{"text":"#3ee732"}}}}} -->
<p class="has-link-color">Welcome to WordPress. This is your first post. Edit or delete it, then start writing! Welcome to <a href="http://wordpress.org">WordPress</a>. </p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:buttons {"style":{"layout":{"columnSpan":2,"rowSpan":1}},"layout":{"type":"flex","justifyContent":"center"}} -->
<div class="wp-block-buttons"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">Button</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div>
<!-- /wp:group --></div>
<!-- /wp:group -->
  1. From wp-admin, navigate to Appearance > Editor > Templates > Blog Home.

  2. Confirm that the Post Content block has no extra margin on top and looks exactly like the front-end.

Testing Instructions for Keyboard

Same.

Screenshots

Before After
before after

@yogeshbhutkar yogeshbhutkar marked this pull request as ready for review April 28, 2025 11:12
Copy link

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: yogeshbhutkar <[email protected]>
Co-authored-by: biplob-g <[email protected]>
Co-authored-by: iamtakashi <[email protected]>

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

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Feature] Layout Layout block support, its UI controls, and style output. [Block] Post Content Affects the Post Content Block labels Apr 28, 2025
@yogeshbhutkar
Copy link
Contributor Author

Hi, @t-hamano. If possible, can you please review this PR and the approach proposed? Thanks!

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Based on this comment, I'm not sure if this is the right approach:

// If making changes or additions to layout definitions, be sure to update the corresponding PHP definitions in
// `block-supports/layout.php` so that the server-side and client-side definitions match.

However, the wp-block class is not used on the frontend, so we cannot have wp-block as part of the selector.

The problem occurs when there are multiple <style> and <SVG> elements before the first element within the editor.

First, it may be worth looking at why it is implemented this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Content Affects the Post Content Block [Feature] Layout Layout block support, its UI controls, and style output. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Content Block: Unwanted top margin on the first visible element in the block when it's inside a query block in the site editor.
2 participants