-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Allow full width blocks to override parent or root container padding #39871
Changes from all commits
f93230f
e3a71f2
cc90ade
b7536bb
cd5b314
505f87f
92f57fb
5b706aa
fe0801b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,14 +30,15 @@ function gutenberg_register_layout_support( $block_type ) { | |
* | ||
* @param string $selector CSS selector. | ||
* @param array $layout Layout object. The one that is passed has already checked the existence of default block layout. | ||
* @param array|null $padding Padding applied to the current block. | ||
* @param boolean $has_block_gap_support Whether the theme has support for the block gap. | ||
* @param string $gap_value The block gap value to apply. | ||
* @param boolean $should_skip_gap_serialization Whether to skip applying the user-defined value set in the editor. | ||
* @param string $fallback_gap_value The block gap value to apply. | ||
* | ||
* @return string CSS style. | ||
*/ | ||
function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em' ) { | ||
function gutenberg_get_layout_style( $selector, $layout, $padding, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em' ) { | ||
$layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default'; | ||
|
||
$style = ''; | ||
|
@@ -54,14 +55,29 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support | |
$wide_max_width_value = wp_strip_all_tags( explode( ';', $wide_max_width_value )[0] ); | ||
|
||
if ( $content_size || $wide_size ) { | ||
$style = "$selector > :where(:not(.alignleft):not(.alignright)) {"; | ||
$style = "$selector {"; | ||
// Using important here to override the inline padding that could be potentially | ||
// applied using the custom padding control before the layout inheritance is applied. | ||
$style .= sprintf( | ||
'padding: %s %s %s %s !important', | ||
isset( $padding['top'] ) ? $padding['top'] : 0, | ||
isset( $padding['right'] ) ? $padding['right'] : 0, | ||
isset( $padding['bottom'] ) ? $padding['bottom'] : 0, | ||
isset( $padding['left'] ) ? $padding['left'] : 0 | ||
); | ||
$style .= '}'; | ||
$style .= "$selector > :where(:not(.alignleft):not(.alignright)) {"; | ||
$style .= 'max-width: ' . esc_html( $all_max_width_value ) . ';'; | ||
$style .= 'margin-left: auto !important;'; | ||
$style .= 'margin-right: auto !important;'; | ||
$style .= '}'; | ||
|
||
$style .= "$selector > .alignwide { max-width: " . esc_html( $wide_max_width_value ) . ';}'; | ||
$style .= "$selector .alignfull { max-width: none; }"; | ||
$style .= "$selector > .alignfull {"; | ||
$style .= 'max-width: none;'; | ||
$style .= isset( $padding['left'] ) ? sprintf( 'margin-left: calc( -1 * %s ) !important;', $padding['left'] ) : ''; | ||
$style .= isset( $padding['right'] ) ? sprintf( 'margin-right: calc( -1 * %s ) !important;', $padding['right'] ) : ''; | ||
$style .= '}'; | ||
} | ||
|
||
$style .= "$selector > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }"; | ||
|
@@ -162,6 +178,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) { | |
|
||
$block_gap = gutenberg_get_global_settings( array( 'spacing', 'blockGap' ) ); | ||
$default_layout = gutenberg_get_global_settings( array( 'layout' ) ); | ||
$padding = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'padding' ), null ); | ||
$has_block_gap_support = isset( $block_gap ) ? null !== $block_gap : false; | ||
$default_block_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() ); | ||
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout; | ||
|
@@ -170,6 +187,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) { | |
return $block_content; | ||
} | ||
$used_layout = $default_layout; | ||
$padding = isset( $default_layout['padding'] ) ? $default_layout['padding'] : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds padding to all default layout blocks, which is not the current behaviour in trunk, so it might break some stuff. |
||
} | ||
|
||
$class_names = array(); | ||
|
@@ -209,7 +227,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) { | |
// If a block's block.json skips serialization for spacing or spacing.blockGap, | ||
// don't apply the user-defined value to the styles. | ||
$should_skip_gap_serialization = gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' ); | ||
$style = gutenberg_get_layout_style( ".$container_class", $used_layout, $has_block_gap_support, $gap_value, $should_skip_gap_serialization, $fallback_gap_value ); | ||
$style = gutenberg_get_layout_style( ".$container_class", $used_layout, $padding, $has_block_gap_support, $gap_value, $should_skip_gap_serialization, $fallback_gap_value ); | ||
// This assumes the hook only applies to blocks with a single wrapper. | ||
// I think this is a reasonable limitation for that particular hook. | ||
$content = preg_replace( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,6 +227,7 @@ class WP_Theme_JSON_6_0 extends WP_Theme_JSON_5_9 { | |
'layout' => array( | ||
'contentSize' => null, | ||
'wideSize' => null, | ||
'padding' => null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why we're adding this when we can use the value at styles > spacing > padding. |
||
), | ||
'spacing' => array( | ||
'blockGap' => null, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,7 @@ export default { | |
layout = {}, | ||
style, | ||
blockName, | ||
padding, | ||
} ) { | ||
const { contentSize, wideSize } = layout; | ||
const blockGapSupport = useSetting( 'spacing.blockGap' ); | ||
|
@@ -126,10 +127,17 @@ export default { | |
! shouldSkipSerialization( blockName, 'spacing', 'blockGap' ) | ||
? blockGapStyleValue?.top | ||
: 'var( --wp--style--block-gap )'; | ||
// Using important here for the padding to override the inline padding that could be potentially | ||
// applied using the custom padding control before the layout inheritance is applied. | ||
|
||
let output = | ||
!! contentSize || !! wideSize | ||
? ` | ||
${ appendSelectors( selector ) } { | ||
padding: ${ padding?.top || 0 } ${ padding?.right || 0 } ${ | ||
padding?.bottom || 0 | ||
} ${ padding?.left || 0 } !important; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is the best way to go about it. If user sets a padding, we should respect that, and work with its value to add negative margins to full-width items. This is making it impossible to have a Group with both layout (needed for e.g. full width images) and padding, which is not ideal, especially if background color is set: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, I found that a bit challenging in testing, too, that toggling "Inherit default layout" hid the padding controls, which felt unexpected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we keep the padding visible when we inherit a layout, we'd create another unexpected behavior though:
Not sure if that's confusing enough or if we can explain it to the user. |
||
} | ||
${ appendSelectors( | ||
selector, | ||
'> :where(:not(.alignleft):not(.alignright))' | ||
|
@@ -143,6 +151,16 @@ export default { | |
} | ||
${ appendSelectors( selector, '> .alignfull' ) } { | ||
max-width: none; | ||
${ | ||
padding?.left | ||
? `margin-left: calc( -1 * ${ padding?.left } ) !important;` | ||
: '' | ||
} | ||
${ | ||
padding?.right | ||
? `margin-right: calc( -1 * ${ padding?.right } ) !important;` | ||
: '' | ||
} | ||
} | ||
` | ||
: ''; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I will test this manually, but ran out of time today 😄
So this is a note for myself more than anything.
Let's say the result is
padding: 10px 0 0 0;
, where onlypadding-top: 10px
was defined by the user in the editor.Are there any problems with right, bottom and left being now
0
?What if a CSS style higher up says otherwise, and is consequently overridden?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good point!
It sounds like that might be a similar challenge to what we've been discussing in the PR to add support for
blockGap
at the block level within global styles: #39870 (comment) — TL;DR: how do we reliably get the value that's cascaded down correctly at this point in the execution. E.g. do we need to look up global styles here as well, to get the right value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, wait, I think I might be getting confused here — because this is a differentNo: it's coming from thepadding
that comes from the layout object instead?style
object... 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might, yes. Though ideally, either the block attributes would know about global styles too, or that cascade would be resolved someplace else by the time we get to layout. Is that within the scope of the style engine work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too sure, to be honest, it's at least beyond the initial work of refactoring the existing block supports, but I think we might naturally bump into it when we go to consolidate style generation for global styles / theme.json.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just rounding back to this: global/theme.json block styles are overwritten for the reasons @andrewserong mentions above
I see the intention is for
!important
to override inline styles. Works as intended, but the inline styles are still there.Without promising anything 😄 I also wonder if the style engine will later dedupe this sort of stuff as well. Layout crosses over into spacing more and more it seems!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify why we need to render the padding here (I mean in layout)? Can't we assume the padding block support will just output the same thing (inline or using a style tag) later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that's for the "inherited padding", maybe we can only output these styles if the layout is inherited to avoid duplication and avoid important?