Skip to content
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

Gallery block: Add a backwards compat fallback to 16px for non-block themes #39557

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 11 additions & 2 deletions packages/block-library/src/gallery/gap-styles.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
/**
* WordPress dependencies
*/
import { BlockList } from '@wordpress/block-editor';
import { BlockList, store as blockEditorStore } from '@wordpress/block-editor';
import { useContext, createPortal } from '@wordpress/element';
import { useSelect } from '@wordpress/data';

export default function GapStyles( { blockGap, clientId } ) {
const themeSupportsLayout = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return getSettings()?.supportsLayout;
}, [] );
const styleElement = useContext( BlockList.__unstableElementContext );

const gapFallback = themeSupportsLayout ? '0.5em' : '16px';
const backwardsCompatGap = ! themeSupportsLayout
? `; gap: ${ gapFallback }`
: '';
const gap = blockGap
? `#block-${ clientId } { --wp--style--unstable-gallery-gap: ${ blockGap } }`
: `#block-${ clientId } { --wp--style--unstable-gallery-gap: var( --wp--style--block-gap, 0.5em ) }`;
: `#block-${ clientId } { --wp--style--unstable-gallery-gap: var( --wp--style--block-gap, ${ gapFallback } ) ${ backwardsCompatGap } }`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some inline comments about the different values and also about backwardsCompatGap to help our future selves?

I don't know why the following reads better for me. Please ignore. There's no real saving in line numbers or anything useful 😄

const gapFallbackValue = themeSupportsLayout ? '0.5em' : '16px';
const blockGapValue = blockGap || `var( --wp--style--block-gap, ${ gapFallback } )`

const gapCSS = `
#block-${ clientId } { 
    --wp--style--unstable-gallery-gap: ${ blockGap };  
    { ! themeSupportsLayout ? 'gap: 16px;' : '' }
}`;


const GapStyle = () => {
return <style>{ gap }</style>;
Expand Down
15 changes: 11 additions & 4 deletions packages/block-library/src/gallery/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,22 @@ function block_core_gallery_render( $attributes, $content ) {
$content,
1
);
$gap_value = $gap ? $gap : 'var( --wp--style--block-gap, 0.5em )';
$style = '.' . $class . '{ --wp--style--unstable-gallery-gap: ' . $gap_value . '}';
$gap_fallback = WP_Theme_JSON_Resolver::theme_has_support() ? '0.5em' : '16px';
$gap_value = $gap ? $gap : 'var( --wp--style--block-gap, ' . $gap_fallback . ' )';
$style = '.' . $class . '{ --wp--style--unstable-gallery-gap: ' . $gap_value . '}';

$backwards_compat_style = '';
if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
$backwards_compat_style = '.' . $class . '.wp-block-gallery' . '{ gap: ' . $gap_value . '}';
}

// Ideally styles should be loaded in the head, but blocks may be parsed
// after that, so loading in the footer for now.
// See https://core.trac.wordpress.org/ticket/53494.
add_action(
'wp_footer',
function () use ( $style ) {
echo '<style> ' . $style . '</style>';
function () use ( $style, $backwards_compat_style ) {
echo '<style> ' . $style . ' ' . $backwards_compat_style . '</style>';
}
);
return $content;
Expand Down