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
1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
44 changes: 40 additions & 4 deletions packages/block-library/src/gallery/gap-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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.
Expand All @@ -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 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 ? globalGalleryBlockGap : 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. 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
: undefined;
const viewportBlockGap =
styleViewportBlockGap === undefined
? globalViewportBlockGap
: styleViewportBlockGap;
if ( viewportBlockGap === undefined || viewportBlockGap === null ) {
return;
}
Expand Down
39 changes: 35 additions & 4 deletions packages/block-library/src/gallery/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<figcaption>`, or nothing when there is no
Expand Down Expand Up @@ -399,7 +401,20 @@ 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 );

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_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. Missing
// values fall back to the Gallery blockGap default.
$block_gap = $has_block_gap
? $style_attr['spacing']['blockGap']
: $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.
$gallery_styles = array(
Expand All @@ -422,16 +437,32 @@ 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. 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 ) {
$viewport_gap = $global_gallery_styles[ $breakpoint ]['spacing']['blockGap'];
} else {
continue;
}

if ( null === $viewport_gap ) {
continue;
}

$gallery_styles[] = array(
'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
),
),
Expand Down
8 changes: 8 additions & 0 deletions packages/global-styles-engine/src/core/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,11 @@ function getResponsiveStyleNodes(
const {
styles,
selector,
fallbackGapValue,
featureSelectors,
hasLayoutSupport,
layoutHasBlockGapSupport,
layoutSelector,
name,
elementName,
isStyleVariation,
Expand All @@ -1039,10 +1043,14 @@ function getResponsiveStyleNodes(
featureSelectors && typeof featureSelectors !== 'string'
? featureSelectors
: undefined,
fallbackGapValue,
hasLayoutSupport,
name,
elementName,
isStyleVariation,
variationName,
layoutSelector,
layoutHasBlockGapSupport,
},
];
}
Expand Down
41 changes: 41 additions & 0 deletions packages/global-styles-engine/src/test/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Loading