-
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
Refactor responsive logic for grid column spans. #59057
Changes from all commits
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 |
---|---|---|
|
@@ -596,13 +596,16 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) { | |
|
||
/** | ||
* If columnSpan is set, and the parent grid is responsive, i.e. if it has a minimumColumnWidth set, | ||
* the columnSpan should be removed on small grids. | ||
* the columnSpan should be removed on small grids. If there's a minimumColumnWidth, the grid is responsive. | ||
* But if the minimumColumnWidth value wasn't changed, it won't be set. In that case, if columnCount doesn't | ||
* exist, we can assume that the grid is responsive. | ||
*/ | ||
if ( isset( $block['attrs']['style']['layout']['columnSpan'] ) && isset( $block['attrs']['style']['layout']['parentColumnWidth'] ) ) { | ||
if ( isset( $block['attrs']['style']['layout']['columnSpan'] ) && ( isset( $block['parentLayout']['minimumColumnWidth'] ) || ! isset( $block['parentLayout']['columnCount'] ) ) ) { | ||
$column_span_number = floatval( $block['attrs']['style']['layout']['columnSpan'] ); | ||
$parent_column_width = $block['attrs']['style']['layout']['parentColumnWidth']; | ||
$parent_column_width = isset( $block['parentLayout']['minimumColumnWidth'] ) ? $block['parentLayout']['minimumColumnWidth'] : '12rem'; | ||
$parent_column_value = floatval( $parent_column_width ); | ||
$parent_column_unit = explode( $parent_column_value, $parent_column_width ); | ||
|
||
/** | ||
* If there is no unit, the width has somehow been mangled so we reset both unit and value | ||
* to defaults. | ||
|
@@ -887,6 +890,25 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) { | |
return $processor->get_updated_html(); | ||
} | ||
|
||
/** | ||
* Add a `render_block_data` filter to fetch the parent block layout data. | ||
*/ | ||
add_filter( | ||
'render_block_data', | ||
function ( $parsed_block, $source_block, $parent_block ) { | ||
/** | ||
* Check if the parent block exists and if it has a layout attribute. | ||
* If it does, add the parent layout to the parsed block. | ||
*/ | ||
if ( $parent_block && isset( $parent_block->parsed_block['attrs']['layout'] ) ) { | ||
$parsed_block['parentLayout'] = $parent_block->parsed_block['attrs']['layout']; | ||
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. Editing the Variables comparing function: I'm just writing this so I can follow it up on Monday, I need to work on a way to prevent SSR processing errors when someone filters a 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. For the record, we are comparing the parsed_block array ourselves because modifying it (adding a key like
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. Thanks for the heads up! Yeah the issue with elements block supports already surfaced (see comment above) and was fixed in #59535. 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. Awesome! So, we will be able to set something like 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. Yeah, that should work fine now! |
||
} | ||
return $parsed_block; | ||
}, | ||
10, | ||
3 | ||
); | ||
|
||
// Register the block support. (overrides core one). | ||
WP_Block_Supports::get_instance()->register( | ||
'layout', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,16 @@ import { useSelect } from '@wordpress/data'; | |
*/ | ||
import { store as blockEditorStore } from '../store'; | ||
import { useStyleOverride } from './utils'; | ||
import { useLayout } from '../components/block-list/layout'; | ||
|
||
function useBlockPropsChildLayoutStyles( { style } ) { | ||
const shouldRenderChildLayoutStyles = useSelect( ( select ) => { | ||
return ! select( blockEditorStore ).getSettings().disableLayoutStyles; | ||
} ); | ||
const layout = style?.layout ?? {}; | ||
const { selfStretch, flexSize, columnSpan, rowSpan, parentColumnWidth } = | ||
layout; | ||
const { selfStretch, flexSize, columnSpan, rowSpan } = layout; | ||
const parentLayout = useLayout() || {}; | ||
const { columnCount, minimumColumnWidth } = parentLayout; | ||
const id = useInstanceId( useBlockPropsChildLayoutStyles ); | ||
const selector = `.wp-container-content-${ id }`; | ||
|
||
|
@@ -37,13 +39,14 @@ function useBlockPropsChildLayoutStyles( { style } ) { | |
}`; | ||
} | ||
/** | ||
* If parentColumnWidth is set, the grid is responsive | ||
* so a container query is needed for the span to resize. | ||
* If minimumColumnWidth is set on the parent, or if no | ||
* columnCount is set, the grid is responsive so a | ||
* container query is needed for the span to resize. | ||
*/ | ||
if ( columnSpan && parentColumnWidth ) { | ||
if ( columnSpan && ( minimumColumnWidth || ! columnCount ) ) { | ||
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. Similar to the PHP side of things, do we also need to check that the parent layout type is 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. We could actually do this here because the parentLayout comes from the BlockList context and seems to always have the type, but not sure if it's worth having different logic here and in the PHP. 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. Yeah, I wouldn't worry about it for now. We can always follow up if need be! |
||
// Calculate the container query value. | ||
const columnSpanNumber = parseInt( columnSpan ); | ||
let parentColumnValue = parseFloat( parentColumnWidth ); | ||
let parentColumnValue = parseFloat( minimumColumnWidth ); | ||
/** | ||
* 12rem is the default minimumColumnWidth value. | ||
* If parentColumnValue is not a number, default to 12. | ||
|
@@ -52,7 +55,7 @@ function useBlockPropsChildLayoutStyles( { style } ) { | |
parentColumnValue = 12; | ||
} | ||
|
||
let parentColumnUnit = parentColumnWidth?.replace( | ||
let parentColumnUnit = minimumColumnWidth?.replace( | ||
parentColumnValue, | ||
'' | ||
); | ||
|
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.
Should we also check that
$block['parentLayout']['type']
is set and isgrid
? This is potentially edge casey, but if I take a block that is a child of a grid layout and then drag it to be within another group (i.e. within a Row block elsewhere on the page), then the block still hascolumnSpan
set, and theparentLayout
does not havecolumnCount
(because the parent layout isflex
), but this condition still passes, which results in rules being output that likely shouldn't be: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.
Good question! So I opted for not doing that because
$block['parentLayout']['type']
isn't always set. If grid is defined as the default layout type for a block, and no customisations are made,$block['parentLayout']
will be empty.I don't think it's a huge issue because the rules we're applying to children (of both flex and grid) are specific to that layout type, so there shouldn't be any weird side-effects.
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.
Sounds good! Yeah, these rules don't wind up affecting anything visually, so seems fine to leave it for now 👍