From df1463991236e5bd7f646b6e38038177e75b49e5 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Thu, 9 Jul 2026 13:58:42 +1000 Subject: [PATCH 1/2] Fix global gap styles for Gallery block --- packages/block-library/package.json | 1 + .../block-library/src/gallery/gap-styles.js | 44 +++++++++++++++++-- packages/block-library/src/gallery/index.php | 33 ++++++++++++-- .../global-styles-engine/src/core/render.tsx | 8 ++++ .../src/test/render.test.ts | 41 +++++++++++++++++ 5 files changed, 119 insertions(+), 8 deletions(-) diff --git a/packages/block-library/package.json b/packages/block-library/package.json index fa560e8e14de56..378b7109a6ba9b 100644 --- a/packages/block-library/package.json +++ b/packages/block-library/package.json @@ -73,6 +73,7 @@ "wp_enqueue_block_support_styles", "wp_get_typography_font_size_value", "wp_style_engine_get_styles", + "wp_get_global_styles", "wp_get_global_settings" ], "suffixClasses": [ diff --git a/packages/block-library/src/gallery/gap-styles.js b/packages/block-library/src/gallery/gap-styles.js index b56987139e9050..72261606b1d2ac 100644 --- a/packages/block-library/src/gallery/gap-styles.js +++ b/packages/block-library/src/gallery/gap-styles.js @@ -3,9 +3,12 @@ */ import { __experimentalGetGapCSSValue as getGapCSSValue, + privateApis as blockEditorPrivateApis, + store as blockEditorStore, useStyleOverride, useSettings, } from '@wordpress/block-editor'; +import { useSelect } from '@wordpress/data'; import { privateApis as globalStylesEnginePrivateApis } from '@wordpress/global-styles-engine'; /** @@ -14,6 +17,8 @@ import { privateApis as globalStylesEnginePrivateApis } from '@wordpress/global- import { unlock } from '../lock-unlock'; const { getResponsiveMediaQueries } = unlock( globalStylesEnginePrivateApis ); +const { globalStylesDataKey } = unlock( blockEditorPrivateApis ); +const GALLERY_BLOCK_NAME = 'core/gallery'; // --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default // gap on the gallery. @@ -34,17 +39,48 @@ function getGalleryGapCustomPropertyStyle( selector, blockGap ) { }`; } +function getBlockGapValue( style ) { + if ( ! Object.hasOwn( style?.spacing || {}, 'blockGap' ) ) { + return undefined; + } + + return style.spacing.blockGap; +} + export default function GalleryGapCustomProperties( { style, clientId } ) { const selector = `#block-${ clientId }`; const [ viewportSettings ] = useSettings( 'viewport' ); - let gap = getGalleryGapCustomPropertyStyle( - selector, - style?.spacing?.blockGap + const globalStyles = useSelect( + ( select ) => + select( blockEditorStore ).getSettings()?.[ globalStylesDataKey ], + [] ); + const globalGalleryStyles = + globalStyles?.blocks?.[ GALLERY_BLOCK_NAME ] || {}; + const styleBlockGap = getBlockGapValue( style ); + const globalBlockGap = + globalGalleryStyles?.spacing?.blockGap ?? + globalStyles?.spacing?.blockGap; + // Prefer the block's own gap value, then Gallery global styles, then root global styles. + const blockGap = + styleBlockGap === undefined ? globalBlockGap : styleBlockGap; + let gap = getGalleryGapCustomPropertyStyle( selector, blockGap ); Object.entries( getResponsiveMediaQueries( viewportSettings ) ).forEach( ( [ viewport, mediaQuery ] ) => { - const viewportBlockGap = style?.[ viewport ]?.spacing?.blockGap; + const styleViewportBlockGap = getBlockGapValue( + style?.[ viewport ] + ); + // Viewport-specific block values win. Global viewport values only apply + // when the block has no base gap, so they do not override an instance value. + const globalViewportBlockGap = + styleBlockGap === undefined + ? globalGalleryStyles?.[ viewport ]?.spacing?.blockGap + : undefined; + const viewportBlockGap = + styleViewportBlockGap === undefined + ? globalViewportBlockGap + : styleViewportBlockGap; if ( viewportBlockGap === undefined || viewportBlockGap === null ) { return; } diff --git a/packages/block-library/src/gallery/index.php b/packages/block-library/src/gallery/index.php index fac0c115640928..80b7d846498bcc 100644 --- a/packages/block-library/src/gallery/index.php +++ b/packages/block-library/src/gallery/index.php @@ -399,7 +399,16 @@ function_exists( 'gutenberg_resolve_style_state_aliases' ) // --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default // gap on the gallery. $fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )'; - $gap_column = block_core_gallery_get_column_gap_value( $style_attr['spacing']['blockGap'] ?? null, $fallback_gap ); + + $global_styles = function_exists( 'wp_get_global_styles' ) ? wp_get_global_styles() : array(); + $global_gallery_styles = $global_styles['blocks']['core/gallery'] ?? array(); + $global_block_gap = $global_gallery_styles['spacing']['blockGap'] ?? $global_styles['spacing']['blockGap'] ?? null; + $has_block_gap = is_array( $style_attr['spacing'] ?? null ) && array_key_exists( 'blockGap', $style_attr['spacing'] ); + // Prefer the block's own gap value, then Gallery global styles, then root global styles. + $block_gap = $has_block_gap + ? $style_attr['spacing']['blockGap'] + : $global_block_gap; + $gap_column = block_core_gallery_get_column_gap_value( $block_gap, $fallback_gap ); // Set the CSS variable to the column value for Gallery's flex width calculations. $gallery_styles = array( @@ -422,8 +431,24 @@ function_exists( 'gutenberg_resolve_style_state_aliases' ) } foreach ( $responsive_media_queries as $breakpoint => $media_query ) { - $viewport_style = $style_attr[ $breakpoint ] ?? null; - if ( ! is_array( $viewport_style ) || ! isset( $viewport_style['spacing']['blockGap'] ) ) { + $viewport_style = $style_attr[ $breakpoint ] ?? null; + $has_viewport_block_gap = is_array( $viewport_style ) && + is_array( $viewport_style['spacing'] ?? null ) && + array_key_exists( 'blockGap', $viewport_style['spacing'] ); + $has_global_viewport_block_gap = is_array( $global_gallery_styles[ $breakpoint ]['spacing'] ?? null ) && + array_key_exists( 'blockGap', $global_gallery_styles[ $breakpoint ]['spacing'] ); + + // Viewport-specific block values win. Global viewport values only apply + // when the block has no base gap, so they do not override an instance value. + if ( $has_viewport_block_gap ) { + $viewport_gap = $viewport_style['spacing']['blockGap']; + } elseif ( ! $has_block_gap && $has_global_viewport_block_gap ) { + $viewport_gap = $global_gallery_styles[ $breakpoint ]['spacing']['blockGap']; + } else { + continue; + } + + if ( null === $viewport_gap ) { continue; } @@ -431,7 +456,7 @@ function_exists( 'gutenberg_resolve_style_state_aliases' ) 'selector' => ".wp-block-gallery.{$unique_gallery_classname}", 'declarations' => array( '--wp--style--unstable-gallery-gap' => block_core_gallery_get_column_gap_value( - $viewport_style['spacing']['blockGap'], + $viewport_gap, $fallback_gap ), ), diff --git a/packages/global-styles-engine/src/core/render.tsx b/packages/global-styles-engine/src/core/render.tsx index f2851e068dcf40..4c232f5382e143 100644 --- a/packages/global-styles-engine/src/core/render.tsx +++ b/packages/global-styles-engine/src/core/render.tsx @@ -1012,7 +1012,11 @@ function getResponsiveStyleNodes( const { styles, selector, + fallbackGapValue, featureSelectors, + hasLayoutSupport, + layoutHasBlockGapSupport, + layoutSelector, name, elementName, isStyleVariation, @@ -1039,10 +1043,14 @@ function getResponsiveStyleNodes( featureSelectors && typeof featureSelectors !== 'string' ? featureSelectors : undefined, + fallbackGapValue, + hasLayoutSupport, name, elementName, isStyleVariation, variationName, + layoutSelector, + layoutHasBlockGapSupport, }, ]; } diff --git a/packages/global-styles-engine/src/test/render.test.ts b/packages/global-styles-engine/src/test/render.test.ts index 1673a68290d69e..97ae2dde69dac4 100644 --- a/packages/global-styles-engine/src/test/render.test.ts +++ b/packages/global-styles-engine/src/test/render.test.ts @@ -1040,6 +1040,47 @@ describe( 'global styles renderer', () => { ); } ); + it( 'handles responsive block gap styles', () => { + const tree = { + styles: { + blocks: { + 'core/group': { + '@mobile': { + spacing: { + blockGap: '24px', + }, + }, + }, + }, + }, + } as unknown as GlobalStylesConfig; + + const blockSelectors = { + 'core/group': { + selector: '.wp-block-group', + hasLayoutSupport: true, + }, + }; + + const result = transformToStyles( + Object.freeze( tree ), + blockSelectors, + true, + false, + false, + true, + minimalStyleOptions + ); + + expect( result ).toContain( '@media (width <= 480px)' ); + expect( result ).toContain( + ':root :where(.wp-block-group-is-layout-flow) > * { margin-block-start: 24px; margin-block-end: 0; }' + ); + expect( result ).toContain( + ':root :where(.wp-block-group-is-layout-flex) { gap: 24px; }' + ); + } ); + it( 'handles legacy responsive block styles', () => { const tree = { styles: { From f0c2b7e05dbd4356915f4fb2153201e340ce1c21 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Fri, 10 Jul 2026 10:50:40 +1000 Subject: [PATCH 2/2] use Gallery's own fallback --- .../block-library/src/gallery/gap-styles.js | 14 +++++++------- packages/block-library/src/gallery/index.php | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/gallery/gap-styles.js b/packages/block-library/src/gallery/gap-styles.js index 72261606b1d2ac..e8b1bb009579ca 100644 --- a/packages/block-library/src/gallery/gap-styles.js +++ b/packages/block-library/src/gallery/gap-styles.js @@ -58,12 +58,12 @@ export default function GalleryGapCustomProperties( { style, clientId } ) { const globalGalleryStyles = globalStyles?.blocks?.[ GALLERY_BLOCK_NAME ] || {}; const styleBlockGap = getBlockGapValue( style ); - const globalBlockGap = - globalGalleryStyles?.spacing?.blockGap ?? - globalStyles?.spacing?.blockGap; - // Prefer the block's own gap value, then Gallery global styles, then root global styles. + const globalGalleryBlockGap = + globalGalleryStyles?.spacing?.blockGap ?? FALLBACK_VALUE; + // Prefer the block's own gap value, then Gallery global styles. Missing + // values fall back to the Gallery blockGap default. const blockGap = - styleBlockGap === undefined ? globalBlockGap : styleBlockGap; + styleBlockGap === undefined ? globalGalleryBlockGap : styleBlockGap; let gap = getGalleryGapCustomPropertyStyle( selector, blockGap ); Object.entries( getResponsiveMediaQueries( viewportSettings ) ).forEach( @@ -71,8 +71,8 @@ export default function GalleryGapCustomProperties( { style, clientId } ) { const styleViewportBlockGap = getBlockGapValue( style?.[ viewport ] ); - // Viewport-specific block values win. Global viewport values only apply - // when the block has no base gap, so they do not override an instance value. + // Viewport-specific block values win. Gallery global viewport values + // only apply when the block has no base gap, so they do not override an instance value. const globalViewportBlockGap = styleBlockGap === undefined ? globalGalleryStyles?.[ viewport ]?.spacing?.blockGap diff --git a/packages/block-library/src/gallery/index.php b/packages/block-library/src/gallery/index.php index 80b7d846498bcc..7823921975d0c3 100644 --- a/packages/block-library/src/gallery/index.php +++ b/packages/block-library/src/gallery/index.php @@ -307,6 +307,8 @@ function block_core_gallery_render_dynamic_image( $attachment_id, $attributes, $ * @return string The content of the block being rendered. */ function block_core_gallery_render( $attributes, $content, $block ) { + static $global_styles = null; + // In dynamic mode the gallery's images are resolved at render time instead of // being authored as inner blocks, so `save.js` persists at most the // gallery-level caption — a bare `
`, or nothing when there is no @@ -400,14 +402,18 @@ function_exists( 'gutenberg_resolve_style_state_aliases' ) // gap on the gallery. $fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )'; - $global_styles = function_exists( 'wp_get_global_styles' ) ? wp_get_global_styles() : array(); + if ( null === $global_styles ) { + $global_styles = function_exists( 'wp_get_global_styles' ) ? wp_get_global_styles() : array(); + } + $global_gallery_styles = $global_styles['blocks']['core/gallery'] ?? array(); - $global_block_gap = $global_gallery_styles['spacing']['blockGap'] ?? $global_styles['spacing']['blockGap'] ?? null; + $global_gallery_gap = $global_gallery_styles['spacing']['blockGap'] ?? $fallback_gap; $has_block_gap = is_array( $style_attr['spacing'] ?? null ) && array_key_exists( 'blockGap', $style_attr['spacing'] ); - // Prefer the block's own gap value, then Gallery global styles, then root global styles. + // Prefer the block's own gap value, then Gallery global styles. Missing + // values fall back to the Gallery blockGap default. $block_gap = $has_block_gap ? $style_attr['spacing']['blockGap'] - : $global_block_gap; + : $global_gallery_gap; $gap_column = block_core_gallery_get_column_gap_value( $block_gap, $fallback_gap ); // Set the CSS variable to the column value for Gallery's flex width calculations. @@ -438,8 +444,8 @@ function_exists( 'gutenberg_resolve_style_state_aliases' ) $has_global_viewport_block_gap = is_array( $global_gallery_styles[ $breakpoint ]['spacing'] ?? null ) && array_key_exists( 'blockGap', $global_gallery_styles[ $breakpoint ]['spacing'] ); - // Viewport-specific block values win. Global viewport values only apply - // when the block has no base gap, so they do not override an instance value. + // Viewport-specific block values win. Gallery global viewport values + // only apply when the block has no base gap, so they do not override an instance value. if ( $has_viewport_block_gap ) { $viewport_gap = $viewport_style['spacing']['blockGap']; } elseif ( ! $has_block_gap && $has_global_viewport_block_gap ) {