@@ -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' ;
1010import { useSelect } from '@wordpress/data' ;
1111import { __ , _x } from '@wordpress/i18n' ;
1212import {
@@ -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 } >
0 commit comments