Skip to content

Fix: Ensure consistent margin application for blocks with dynamic block appender - #69595

Open
Sourav61 wants to merge 3 commits into
WordPress:trunkfrom
Sourav61:fix/block-appender-margin-shift
Open

Sourav61 wants to merge 3 commits into
WordPress:trunkfrom
Sourav61:fix/block-appender-margin-shift

Conversation

@Sourav61

Copy link
Copy Markdown
Contributor

Ensured consistent margin application for blocks when block list appender appears

What?

Closes #61846

Fixes an issue where the .block-list-appender dynamically modifies the block structure, causing inconsistent margin application for blocks inside .is-layout-constrained containers. This ensures that margins are correctly applied even when the block appender is added or removed dynamically.

Why?

Previously, when a block (e.g., Cover block) was inside a Group block with .is-layout-constrained, the following CSS rules were applied:

:root :where(.is-layout-constrained) > :first-child {
    margin-block-start: 0;
}

:root :where(.is-layout-constrained) > :last-child {
    margin-block-end: 0;
}

However, when selecting the Group block in the editor, the .block-list-appender gets appended to the group, making it the new :last-child, which removes the intended margin adjustments from the actual content block, causing a layout shift.

How?

  • Used :has(> .block-list-appender) to detect when a .block-list-appender is present inside .is-layout-constrained.
  • Applied :nth-last-child(2) to target the second-to-last element, ensuring the last actual content block (not the appender) retains the correct margin styles.
  • This prevents the .block-list-appender from disrupting the layout when dynamically added or removed

Testing Instructions

  1. Add a Cover block inside a Group block in the editor.
  2. Apply margin styles via theme.json or global styles.
  3. Observe the margins before and after selecting the Group block.

…ck appender

Ensured consistent margin application for blocks with dynamic block appender
…ck appender

Ensured consistent margin application for blocks with dynamic block appender
Revert the unwanted changes
@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Feature] Layout Layout block support, its UI controls, and style output. labels Apr 9, 2025

@aaronrobertshaw aaronrobertshaw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for picking this up @Sourav61 👍

I've only had a chance to quickly test this with TT5, a 48px global style margin on Cover blocks. The margin still shows when the group is selected, in both the block and site editors.

The block appender only shows in the editor so the rule in the PHP layout config won't ever match anything.

Also, the appender margin issue impacts flow layouts which have the same :last-child rule.

I think all we'll need here is to define the rule within the JS side of layouts.

Something like:

selector: ' > :has(+ :where(.block-list-appender):last-child)',

The :where() should keep the same specificity as > :last-child. A quick local test seemed to fix both editors.

Image
This PR JS engine rule
Where the rule lands Front end only Editor only
Layouts Constrained Flow and constrained
Specificity 0-3-0 0-2-0, same as > :last-child
Editor margin when selected Still 48px 0, matches front end

I can throw up an alternative PR if this PR is too stale.

@aaronrobertshaw

Copy link
Copy Markdown
Contributor

Looks like I was wrong in my earlier review.

A PHP stylesheet does reach the editor for hybrid themes, i.e. classic themes that have a theme.json file and set blockGap. So the PHP rule fixes constrained layouts for those themes, and flow still needs it. Block themes still need the rule in the JS engine.

I think the final solution should be the rule in the JS engine and a PHP rule that goes into the editor styles only so the unnecessary style doesn't reach the frontend.

@github-actions

Copy link
Copy Markdown

🤖 PR meta 🤖

🎉 Props

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: Sourav61 <sourav08@git.wordpress.org>
Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>

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

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

This branch has not been deployed

No deployments
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. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Layout Styles: Block list appender causes inconsistent application of layout margin styles

3 participants