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: use root padding for full-width margin offset #41042

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ function gutenberg_register_layout_support( $block_type ) {
* @return string CSS style.
*/
function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false ) {
$layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default';
$layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default';
$full_width_should_stretch = isset( $layout['fullWidthShouldStretch'] ) && true === $layout['fullWidthShouldStretch'];

$style = '';
if ( 'default' === $layout_type ) {
Expand Down Expand Up @@ -140,7 +141,9 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support

$style .= "$selector > * { margin: 0; }";
}

if ( $full_width_should_stretch ) {
$style .= "$selector.alignfull { margin-right: calc(var(--wp--style--root--padding-right) * -1) !important; margin-left: calc(var(--wp--style--root--padding-left) * -1) !important; width: unset; }";
}
return $style;
}

Expand All @@ -164,15 +167,18 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
$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;

if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) {
if ( ! $default_layout ) {
return $block_content;
}
$used_layout = $default_layout;
}
$used_layout['fullWidthShouldStretch'] = isset( $default_layout['fullWidthShouldStretch'] ) && true === $default_layout['fullWidthShouldStretch'] && isset( $block['attrs']['align'] ) && 'full' === $block['attrs']['align'];

$class_name = wp_unique_id( 'wp-container-' );
$gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) );

$gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) );
// Skip if gap value contains unsupported characters.
// Regex for CSS value borrowed from `safecss_filter_attr`, and used here
// because we only want to match against the value, not the CSS attribute.
Expand Down
165 changes: 165 additions & 0 deletions lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,171 @@
* @access private
*/
class WP_Theme_JSON_6_1 extends WP_Theme_JSON_6_0 {
/**
* Metadata for style properties.
*
* Each element is a direct mapping from the CSS property name to the
* path to the value in theme.json & block attributes.
*/
const PROPERTIES_METADATA = array(
'--wp--style--block-gap' => array( 'spacing', 'blockGap' ),
'background' => array( 'color', 'gradient' ),
'background-color' => array( 'color', 'background' ),
'border-radius' => array( 'border', 'radius' ),
'border-top-left-radius' => array( 'border', 'radius', 'topLeft' ),
'border-top-right-radius' => array( 'border', 'radius', 'topRight' ),
'border-bottom-left-radius' => array( 'border', 'radius', 'bottomLeft' ),
'border-bottom-right-radius' => array( 'border', 'radius', 'bottomRight' ),
'border-color' => array( 'border', 'color' ),
'border-width' => array( 'border', 'width' ),
'border-style' => array( 'border', 'style' ),
'border-top-color' => array( 'border', 'top', 'color' ),
'border-top-width' => array( 'border', 'top', 'width' ),
'border-top-style' => array( 'border', 'top', 'style' ),
'border-right-color' => array( 'border', 'right', 'color' ),
'border-right-width' => array( 'border', 'right', 'width' ),
'border-right-style' => array( 'border', 'right', 'style' ),
'border-bottom-color' => array( 'border', 'bottom', 'color' ),
'border-bottom-width' => array( 'border', 'bottom', 'width' ),
'border-bottom-style' => array( 'border', 'bottom', 'style' ),
'border-left-color' => array( 'border', 'left', 'color' ),
'border-left-width' => array( 'border', 'left', 'width' ),
'border-left-style' => array( 'border', 'left', 'style' ),
'color' => array( 'color', 'text' ),
'font-family' => array( 'typography', 'fontFamily' ),
'font-size' => array( 'typography', 'fontSize' ),
'font-style' => array( 'typography', 'fontStyle' ),
'font-weight' => array( 'typography', 'fontWeight' ),
'letter-spacing' => array( 'typography', 'letterSpacing' ),
'line-height' => array( 'typography', 'lineHeight' ),
'margin' => array( 'spacing', 'margin' ),
'margin-top' => array( 'spacing', 'margin', 'top' ),
'margin-right' => array( 'spacing', 'margin', 'right' ),
'margin-bottom' => array( 'spacing', 'margin', 'bottom' ),
'margin-left' => array( 'spacing', 'margin', 'left' ),
'padding' => array( 'spacing', 'padding' ),
'padding-top' => array( 'spacing', 'padding', 'top' ),
'padding-right' => array( 'spacing', 'padding', 'right' ),
'padding-bottom' => array( 'spacing', 'padding', 'bottom' ),
'padding-left' => array( 'spacing', 'padding', 'left' ),
'text-decoration' => array( 'typography', 'textDecoration' ),
'text-transform' => array( 'typography', 'textTransform' ),
'filter' => array( 'filter', 'duotone' ),
);

/**
* Metadata for style properties that should only be printed to the roo selector.
*
* Each element is a direct mapping from the CSS property name to the corresponding root CSS var.
*/
const ROOT_PROPERTIES_METADATA = array(
'padding' => '--wp--style--root--padding',
'padding-top' => '--wp--style--root--padding-top',
'padding-right' => '--wp--style--root--padding-right',
'padding-bottom' => '--wp--style--root--padding-bottom',
'padding-left' => '--wp--style--root--padding-left',
);

/**
* The valid properties under the settings key.
*
* @var array
*/
const VALID_SETTINGS = array(
'appearanceTools' => null,
'border' => array(
'color' => null,
'radius' => null,
'style' => null,
'width' => null,
),
'color' => array(
'background' => null,
'custom' => null,
'customDuotone' => null,
'customGradient' => null,
'defaultDuotone' => null,
'defaultGradients' => null,
'defaultPalette' => null,
'duotone' => null,
'gradients' => null,
'link' => null,
'palette' => null,
'text' => null,
),
'custom' => null,
'layout' => array(
'contentSize' => null,
'wideSize' => null,
'fullWidthShouldStretch' => null,
),
'spacing' => array(
'blockGap' => null,
'margin' => null,
'padding' => null,
'units' => null,
),
'typography' => array(
'customFontSize' => null,
'dropCap' => null,
'fontFamilies' => null,
'fontSizes' => null,
'fontStyle' => null,
'fontWeight' => null,
'letterSpacing' => null,
'lineHeight' => null,
'textDecoration' => null,
'textTransform' => null,
),
);

/**
* Given a selector and a declaration list,
* creates the corresponding ruleset.
*
* To help debugging, will add some space
* if SCRIPT_DEBUG is defined and true.
*
* @param string $selector CSS selector.
* @param array $declarations List of declarations.
*
* @return string CSS ruleset.
*/
protected static function to_ruleset( $selector, $declarations ) {
if ( empty( $declarations ) ) {
return '';
}
$ruleset = '';

if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
$declaration_block = array_reduce(
$declarations,
function ( $carry, $element ) use ( $selector ) {
if ( static::ROOT_BLOCK_SELECTOR === $selector && isset( static::ROOT_PROPERTIES_METADATA[ $element['name'] ] ) ) {
$carry .= "\t" . static::ROOT_PROPERTIES_METADATA[ $element['name'] ] . ': ' . $element['value'] . ";\n";
return $carry .= "\t" . $element['name'] . ': var(' . static::ROOT_PROPERTIES_METADATA[ $element['name'] ] . ");\n";
}
return $carry .= "\t" . $element['name'] . ': ' . $element['value'] . ";\n"; },
''
);
$ruleset .= $selector . " {\n" . $declaration_block . "}\n";
} else {
$declaration_block = array_reduce(
$declarations,
function ( $carry, $element ) use ( $selector ) {
if ( static::ROOT_BLOCK_SELECTOR === $selector && isset( static::ROOT_PROPERTIES_METADATA[ $element['name'] ] ) ) {
$carry .= static::ROOT_PROPERTIES_METADATA[ $element['name'] ] . ': ' . $element['value'] . ';';
return $carry .= $element['name'] . ': var(' . static::ROOT_PROPERTIES_METADATA[ $element['name'] ] . ');';
}
return $carry .= $element['name'] . ': ' . $element['value'] . ';'; },
''
);
$ruleset .= $selector . '{' . $declaration_block . '}';
}

return $ruleset;
}

/**
* Returns the metadata for each block.
*
Expand Down
3 changes: 2 additions & 1 deletion test/emptytheme/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"appearanceTools": true,
"layout": {
"contentSize": "840px",
"wideSize": "1100px"
"wideSize": "1100px",
"fullWidthShouldStretch": true
}
},
"patterns": [ "short-text-surrounded-by-round-images", "partner-logos" ]
Expand Down