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

Layout: merge CSS rule for block gap #43052

Merged
merged 2 commits into from
Aug 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,15 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
array_push(
$layout_styles,
array(
'selector' => "$selector > *",
'selector' => "$selector > *,$selector > * + *",
'declarations' => array(
'margin-block-start' => '0',
'margin-block-end' => '0',
'margin-block-end' => '0',
),
),
array(
'selector' => "$selector > * + *",
Copy link
Member

Choose a reason for hiding this comment

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

We could simply change this selector to be more specific... Since this needs to override the other one and they both have the same specificity, we should raise the specificity of this one

Suggested change
'selector' => "$selector > * + *",
'selector' => "$selector.$selector > * + *",

Copy link
Member Author

Choose a reason for hiding this comment

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

Works great! Committed (without the extra .)

I'm happy to go with either if we think we should merge this one.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice that this one fixes the current use case, and it feels a bit neater than combining the selectors in the first rule. Since this is only output when there is a real gap value, I don't at all object to upping the specificity here 👍

I'm wondering if it reveals a drawback or potential issue for the optimisation that we should look into separately, though. It's possibly going to be fairly common for themes to expect styles to override earlier styles with the same specificity due to the order in which they're added. In the case of this particular gap rule, I believe it might have been originally implemented based on the rules from the following article / generator: https://every-layout.dev/layouts/stack/#the-generator

Also, once we go to implement optimisation in global styles, we'll need to be careful about the specificity of the rules, as upping the specificity at one level of the style chain might make it harder for rules at other levels. (e.g. base layout gap, versus block-level in theme gap, versus block-level in block attributes gap).

'declarations' => array(
'margin-block-start' => $gap_value,
'margin-block-end' => '0',
),
)
);
Expand Down