Skip to content
Merged
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
3 changes: 3 additions & 0 deletions backport-changelog/7.1/11966.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/11966

* https://github.com/WordPress/gutenberg/pull/78543
629 changes: 426 additions & 203 deletions lib/block-supports/layout.php

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion lib/block-supports/states.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ function gutenberg_normalize_state_preset_vars( $value ) {
* @return array Normalized state style object.
*/
function gutenberg_normalize_state_style_for_css_output( $style ) {
return gutenberg_normalize_state_preset_vars( $style );
// Layout is processed separately by gutenberg_render_layout_support_flag(), so we remove it before declaration generation.
unset( $style['layout'] );

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.

Could probably swap lines 46 & 47, so that gutenberg_normalize_state_preset_vars doesn't have to loop over the layout property.

$style = gutenberg_normalize_state_preset_vars( $style );
return $style;
}

/**
Expand Down Expand Up @@ -421,6 +424,10 @@ function gutenberg_render_block_states_support( $block_content, $block ) {
*
* State declarations need !important to apply reliably over inline styles and
* preset utility classes such as .has-accent-3-background-color.
*
* Layout-driven state styles (responsive layout, blockGap, child layout) are
* handled by gutenberg_render_layout_support_flag() so they share a selector
* with the base layout and target the correct (inner) wrapper element.
*/
$style_rules = array();
foreach ( $css_rules as $rule ) {
Expand Down
22 changes: 19 additions & 3 deletions packages/block-editor/src/components/block-inspector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ import { BlockStateBadges, BlockStatesControl } from '../../hooks/states';
import ContentTab from '../inspector-controls-tabs/content-tab';
import ViewportVisibilityInfo from '../block-visibility/viewport-visibility-info';
import { unlock } from '../../lock-unlock';
import { isDefaultBlockStyleState } from '../../hooks/block-style-state';
import {
hasPseudoBlockStyleState,
hasViewportBlockStyleState,
isDefaultBlockStyleState,
} from '../../hooks/block-style-state';
import { onViewportStateChangeKey } from '../../store/private-keys';

function StyleInspectorSlots( {
Expand Down Expand Up @@ -88,8 +92,11 @@ function StyleInspectorSlots( {
);
}

function StyleStateInspectorSlots( { blockName } ) {
function StyleStateInspectorSlots( { blockName, selectedBlockStyleState } ) {
const borderPanelLabel = useBorderPanelLabel( { blockName } );
const showLayoutControls =
hasViewportBlockStyleState( selectedBlockStyleState ) &&
! hasPseudoBlockStyleState( selectedBlockStyleState );
return (
<>
<InspectorControls.Slot
Expand All @@ -106,6 +113,12 @@ function StyleStateInspectorSlots( { blockName } ) {
group="typography"
label={ __( 'Typography' ) }
/>
{ showLayoutControls && (
<InspectorControls.Slot
group="layout"
label={ __( 'Layout' ) }
/>
) }
<InspectorControls.Slot
group="dimensions"
label={ __( 'Dimensions' ) }
Expand Down Expand Up @@ -473,7 +486,10 @@ const BlockInspectorSingleBlock = ( {
<BlockVariationTransforms blockClientId={ renderedBlockClientId } />
<BlockInspectorPreTabsSlot />
{ isBlockStyleStateSelected && ! isSectionBlock && (
<StyleStateInspectorSlots blockName={ blockName } />
<StyleStateInspectorSlots
blockName={ blockName }
selectedBlockStyleState={ selectedBlockStyleState }
/>
) }
{ ! isBlockStyleStateSelected && hasMultipleTabs && (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { cleanEmptyObject } from '../../hooks/utils';
import { setImmutably } from '../../utils/object';
import {
DEFAULT_BLOCK_STYLE_STATE,
isDefaultBlockStyleState,
hasPseudoBlockStyleState,
} from '../../hooks/block-style-state';

const AXIAL_SIDES = [ 'horizontal', 'vertical' ];
Expand Down Expand Up @@ -97,7 +97,7 @@ function hasAspectRatio( settings ) {
}

function hasChildLayout( settings, styleState = DEFAULT_BLOCK_STYLE_STATE ) {
if ( ! isDefaultBlockStyleState( styleState ) ) {
if ( hasPseudoBlockStyleState( styleState ) ) {
return false;
}

Expand Down Expand Up @@ -509,6 +509,7 @@ export default function DimensionsPanel( {
onChange( {
...value,
layout: {
...value?.layout,
...newChildLayout,
},
} );
Expand Down
241 changes: 179 additions & 62 deletions packages/block-editor/src/hooks/layout-child.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,79 +22,96 @@ import { BLOCK_VISIBILITY_VIEWPORTS } from '../components/block-visibility/const

// Used for generating the instance ID
const LAYOUT_CHILD_BLOCK_PROPS_REFERENCE = {};
// Keep in sync with WP_Theme_JSON_Gutenberg::RESPONSIVE_BREAKPOINTS.
const RESPONSIVE_BREAKPOINTS = {
mobile: '@media (width <= 480px)',
tablet: '@media (480px < width <= 782px)',
};

function useBlockPropsChildLayoutStyles( { style } ) {
const shouldRenderChildLayoutStyles = useSelect( ( select ) => {
return ! select( blockEditorStore ).getSettings().disableLayoutStyles;
} );
const layout = style?.layout ?? {};
function serializeRule( { selector, declarations } ) {
return `${ selector } {
${ Object.entries( declarations )
.map( ( [ property, value ] ) => `${ property }: ${ value }` )
.join( '; ' ) };
}`;
}

export function getChildLayoutStyleRules( {
selector,
layout = {},
viewportOverrides,
parentLayout = {},
includeContainerQuery = true,
} ) {
const hasViewportOverrides = viewportOverrides !== undefined;
const effectiveLayout = hasViewportOverrides
? {
...layout,
...viewportOverrides,
}
: layout;
const hasViewportOverride = ( key ) =>
Object.hasOwn( viewportOverrides || {}, key );
const {
selfStretch,
flexSize,
columnStart,
rowStart,
columnSpan,
rowSpan,
} = layout;
const parentLayout = useLayout() || {};
} = effectiveLayout;
const { columnCount, minimumColumnWidth } = parentLayout;
const id = useInstanceId( LAYOUT_CHILD_BLOCK_PROPS_REFERENCE );
const selector = `.wp-container-content-${ id }`;
const rules = [];

// Check that the grid layout attributes are of the correct type, so that we don't accidentally
// write code that stores a string attribute instead of a number.
if ( process.env.NODE_ENV === 'development' ) {
if ( columnStart && typeof columnStart !== 'number' ) {
throw new Error( 'columnStart must be a number' );
}
if ( rowStart && typeof rowStart !== 'number' ) {
throw new Error( 'rowStart must be a number' );
}
if ( columnSpan && typeof columnSpan !== 'number' ) {
throw new Error( 'columnSpan must be a number' );
}
if ( rowSpan && typeof rowSpan !== 'number' ) {
throw new Error( 'rowSpan must be a number' );
const declarations = {};
if (
! hasViewportOverrides ||
hasViewportOverride( 'selfStretch' ) ||
hasViewportOverride( 'flexSize' )
) {
if ( selfStretch === 'fixed' && flexSize ) {
declarations[ 'flex-basis' ] = flexSize;
declarations[ 'box-sizing' ] = 'border-box';
} else if ( selfStretch === 'fill' ) {
declarations[ 'flex-grow' ] = '1';
}
}

let css = '';
if ( shouldRenderChildLayoutStyles ) {
if ( selfStretch === 'fixed' && flexSize ) {
css = `${ selector } {
flex-basis: ${ flexSize };
box-sizing: border-box;
}`;
} else if ( selfStretch === 'fill' ) {
css = `${ selector } {
flex-grow: 1;
}`;
} else if ( columnStart && columnSpan ) {
css = `${ selector } {
grid-column: ${ columnStart } / span ${ columnSpan };
}`;
if (
! hasViewportOverrides ||
hasViewportOverride( 'columnStart' ) ||
hasViewportOverride( 'columnSpan' )
) {
if ( columnStart && columnSpan ) {
declarations[
'grid-column'
] = `${ columnStart } / span ${ columnSpan }`;
} else if ( columnStart ) {
css = `${ selector } {
grid-column: ${ columnStart };
}`;
declarations[ 'grid-column' ] = `${ columnStart }`;
} else if ( columnSpan ) {
css = `${ selector } {
grid-column: span ${ columnSpan };
}`;
declarations[ 'grid-column' ] = `span ${ columnSpan }`;
}
}

if (
! hasViewportOverrides ||
hasViewportOverride( 'rowStart' ) ||
hasViewportOverride( 'rowSpan' )
) {
if ( rowStart && rowSpan ) {
css += `${ selector } {
grid-row: ${ rowStart } / span ${ rowSpan };
}`;
declarations[ 'grid-row' ] = `${ rowStart } / span ${ rowSpan }`;
} else if ( rowStart ) {
css += `${ selector } {
grid-row: ${ rowStart };
}`;
declarations[ 'grid-row' ] = `${ rowStart }`;
} else if ( rowSpan ) {
css += `${ selector } {
grid-row: span ${ rowSpan };
}`;
declarations[ 'grid-row' ] = `span ${ rowSpan }`;
}
}

if ( Object.keys( declarations ).length ) {
rules.push( { selector, declarations } );
}

if ( includeContainerQuery && ! hasViewportOverrides ) {
/**
* If minimumColumnWidth is set on the parent, or if no
* columnCount is set, the grid is responsive so a
Expand Down Expand Up @@ -147,18 +164,118 @@ function useBlockPropsChildLayoutStyles( { style } ) {
const gridColumnValue =
columnSpan && columnSpan > 1 ? '1/-1' : 'auto';

css += `@container (max-width: ${ Math.max(
containerQueryValue,
minimumContainerQueryValue
) }${ parentColumnUnit }) {
${ selector } {
grid-column: ${ gridColumnValue };
grid-row: auto;
}
}`;
rules.push( {
rulesGroup: `@container (max-width: ${ Math.max(
containerQueryValue,
minimumContainerQueryValue
) }${ parentColumnUnit })`,
selector,
declarations: {
'grid-column': gridColumnValue,
'grid-row': 'auto',
},
} );
}
}

return rules;
}

export function getChildLayoutStyles( {
selector,
layout = {},
parentLayout = {},
includeContainerQuery = true,
} ) {
return getChildLayoutStyleRules( {
selector,
layout,
parentLayout,
includeContainerQuery,
} )
.map( ( rule ) => {
const serializedRule = serializeRule( rule );
return rule.rulesGroup
? `${ rule.rulesGroup } {
${ serializedRule }
}`
: serializedRule;
} )
.join( '' );
}

export function getResponsiveChildLayoutStyles( {
style = {},
selector,
parentLayout = {},
} ) {
const baseLayout = style?.layout ?? {};

return Object.entries( RESPONSIVE_BREAKPOINTS )
.map( ( [ viewport, mediaQuery ] ) => {
const viewportLayout = style?.[ viewport ]?.layout;
if ( ! viewportLayout || ! Object.keys( viewportLayout ).length ) {
return '';
}

const viewportRules = getChildLayoutStyleRules( {
selector,
layout: baseLayout,
viewportOverrides: viewportLayout,
parentLayout,
includeContainerQuery: false,
} );
const css = viewportRules.map( serializeRule ).join( '' );

return css ? `${ mediaQuery }{${ css }}` : '';
} )
.filter( Boolean )
.join( '' );
}

function useBlockPropsChildLayoutStyles( { style } ) {
const shouldRenderChildLayoutStyles = useSelect( ( select ) => {
return ! select( blockEditorStore ).getSettings().disableLayoutStyles;
} );
const layout = style?.layout ?? {};
const { columnStart, rowStart, columnSpan, rowSpan } = layout;
const parentLayout = useLayout() || {};
const id = useInstanceId( LAYOUT_CHILD_BLOCK_PROPS_REFERENCE );
const selector = `.wp-container-content-${ id }`;

// Check that the grid layout attributes are of the correct type, so that we don't accidentally
// write code that stores a string attribute instead of a number.
if ( process.env.NODE_ENV === 'development' ) {
if ( columnStart && typeof columnStart !== 'number' ) {
throw new Error( 'columnStart must be a number' );
}
if ( rowStart && typeof rowStart !== 'number' ) {
throw new Error( 'rowStart must be a number' );
}
if ( columnSpan && typeof columnSpan !== 'number' ) {
throw new Error( 'columnSpan must be a number' );
}
if ( rowSpan && typeof rowSpan !== 'number' ) {
throw new Error( 'rowSpan must be a number' );
}
}

let css = '';
if ( shouldRenderChildLayoutStyles ) {
css = [
getChildLayoutStyles( {
selector,
layout,
parentLayout,
} ),
getResponsiveChildLayoutStyles( {
style,
selector,
parentLayout,
} ),
].join( '' );
}

useStyleOverride( { css } );

// Only attach a container class if there is generated CSS to be attached.
Expand Down
Loading
Loading