Skip to content

Commit 0c1456a

Browse files
try back compat with a new attribute flag
1 parent 79d78e2 commit 0c1456a

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

docs/reference-guides/core-blocks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ Contains the block elements used to render a post, like the title, date, feature
754754
- **Category:** theme
755755
- **Ancestor:** core/query
756756
- **Supports:** align (full, wide), anchor, color (background, gradients, link, text), interactivity (clientNavigation), layout, spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~, ~~reusable~~
757+
- **Attributes:** hasOptionalResponsiveGrid
757758

758759
## Post Terms
759760

packages/block-library/src/post-template/block.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
"enhancedPagination",
1717
"postType"
1818
],
19+
"attributes": {
20+
"hasOptionalResponsiveGrid": {
21+
"type": "boolean"
22+
}
23+
},
1924
"supports": {
2025
"anchor": true,
2126
"reusable": false,

packages/block-library/src/post-template/edit.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import clsx from 'clsx';
66
/**
77
* WordPress dependencies
88
*/
9-
import { memo, useMemo, useState } from '@wordpress/element';
9+
import { memo, useEffect, useMemo, useRef, useState } from '@wordpress/element';
1010
import { useSelect } from '@wordpress/data';
1111
import { __, _x } from '@wordpress/i18n';
1212
import {
@@ -113,10 +113,12 @@ export default function PostTemplateEdit( {
113113
templateSlug,
114114
previewPostType,
115115
},
116-
attributes: { layout },
116+
attributes: { layout, hasOptionalResponsiveGrid },
117117
__unstableLayoutClassNames,
118118
} ) {
119119
const { type: layoutType, columnCount = 3 } = layout || {};
120+
const previousLayoutRef = useRef( layout );
121+
const isFirstRenderRef = useRef( true );
120122
const [ activeBlockContextId, setActiveBlockContextId ] = useState();
121123
const { posts, blocks } = useSelect(
122124
( select ) => {
@@ -281,9 +283,41 @@ export default function PostTemplateEdit( {
281283
className: clsx( __unstableLayoutClassNames, {
282284
[ `columns-${ columnCount }` ]:
283285
layoutType === 'grid' && columnCount, // Ensure column count is flagged via classname for backwards compatibility.
286+
'has-optional-responsive-grid':
287+
layoutType === 'grid' && hasOptionalResponsiveGrid,
284288
} ),
285289
} );
286290

291+
useEffect( () => {
292+
if ( isFirstRenderRef.current ) {
293+
isFirstRenderRef.current = false;
294+
previousLayoutRef.current = layout;
295+
296+
return;
297+
}
298+
299+
const hasLayoutChanged =
300+
JSON.stringify( previousLayoutRef.current ) !==
301+
JSON.stringify( layout );
302+
303+
if (
304+
hasLayoutChanged &&
305+
layoutType === 'grid' &&
306+
layout?.columnCount &&
307+
! hasOptionalResponsiveGrid
308+
) {
309+
setAttributes( { hasOptionalResponsiveGrid: true } );
310+
} else if (
311+
hasLayoutChanged &&
312+
( layoutType !== 'grid' || ! layout?.columnCount ) &&
313+
hasOptionalResponsiveGrid
314+
) {
315+
setAttributes( { hasOptionalResponsiveGrid: undefined } );
316+
}
317+
318+
previousLayoutRef.current = layout;
319+
}, [ hasOptionalResponsiveGrid, layout, layoutType, setAttributes ] );
320+
287321
if ( ! posts ) {
288322
return (
289323
<p { ...blockProps }>

packages/block-library/src/post-template/index.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ function render_block_core_post_template( $attributes, $content, $block ) {
9393
// Ensure backwards compatibility by flagging the number of columns via classname when using grid layout.
9494
if ( isset( $attributes['layout']['type'] ) && 'grid' === $attributes['layout']['type'] && ! empty( $attributes['layout']['columnCount'] ) ) {
9595
$classnames .= ' ' . sanitize_title( 'columns-' . $attributes['layout']['columnCount'] );
96+
97+
if ( ! empty( $attributes['hasOptionalResponsiveGrid'] ) ) {
98+
$classnames .= ' has-optional-responsive-grid';
99+
}
96100
}
97101

98102
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => trim( $classnames ) ) );

packages/block-library/src/post-template/style.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
}
3333
}
3434

35+
// Legacy fallback for older grid layouts before responsive behaviour was implemented.
36+
@media (max-width: $break-small) {
37+
.wp-block-post-template-is-layout-grid[class*="columns-"]:not(.has-optional-responsive-grid) {
38+
grid-template-columns: 1fr;
39+
}
40+
}
41+
3542
.wp-block-post-template-is-layout-constrained > li > .alignright,
3643
.wp-block-post-template-is-layout-flow > li > .alignright {
3744
float: right;
@@ -51,3 +58,4 @@
5158
margin-inline-start: auto;
5259
margin-inline-end: auto;
5360
}
61+

0 commit comments

Comments
 (0)