Skip to content

Commit

Permalink
Group block: Add a row variation (#34535)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Sep 7, 2021
1 parent ffe0707 commit 5341bac
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
11 changes: 6 additions & 5 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ function () use ( $style ) {
*/
function gutenberg_restore_group_inner_container( $block_content, $block ) {
$group_with_inner_container_regex = '/(^\s*<div\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/';

if (
'core/group' !== $block['blockName'] ||
WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ||
1 === preg_match( $group_with_inner_container_regex, $block_content )
1 === preg_match( $group_with_inner_container_regex, $block_content ) ||
( isset( $block['attrs']['layout']['type'] ) && 'default' !== $block['attrs']['layout']['type'] )
) {
return $block_content;
}
Expand All @@ -180,7 +180,8 @@ function( $matches ) {
return $updated_content;
}

// This can be removed when plugin support requires WordPress 5.8.0+.
if ( ! function_exists( 'wp_restore_group_inner_container' ) ) {
add_filter( 'render_block', 'gutenberg_restore_group_inner_container', 10, 2 );
if ( function_exists( 'wp_restore_group_inner_container' ) ) {
remove_filter( 'render_block', 'wp_restore_group_inner_container', 10, 2 );
}
add_filter( 'render_block', 'gutenberg_restore_group_inner_container', 10, 2 );

10 changes: 6 additions & 4 deletions packages/block-library/src/group/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ function GroupEdit( { attributes, setAttributes, clientId } ) {
const defaultLayout = useSetting( 'layout' ) || {};
const { tagName: TagName = 'div', templateLock, layout = {} } = attributes;
const usedLayout = !! layout && layout.inherit ? defaultLayout : layout;
const { type = 'default' } = usedLayout;
const layoutSupportEnabled = themeSupportsLayout || type !== 'default';

const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps(
themeSupportsLayout
layoutSupportEnabled
? blockProps
: { className: 'wp-block-group__inner-container' },
{
templateLock,
renderAppender: hasInnerBlocks
? undefined
: InnerBlocks.ButtonBlockAppender,
__experimentalLayout: themeSupportsLayout ? usedLayout : undefined,
__experimentalLayout: layoutSupportEnabled ? usedLayout : undefined,
}
);

Expand All @@ -63,10 +65,10 @@ function GroupEdit( { attributes, setAttributes, clientId } ) {
}
/>
</InspectorControls>
{ themeSupportsLayout && <TagName { ...innerBlocksProps } /> }
{ layoutSupportEnabled && <TagName { ...innerBlocksProps } /> }
{ /* Ideally this is not needed but it's there for backward compatibility reason
to keep this div for themes that might rely on its presence */ }
{ ! themeSupportsLayout && (
{ ! layoutSupportEnabled && (
<TagName { ...blockProps }>
<div { ...innerBlocksProps } />
</TagName>
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/group/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import deprecated from './deprecated';
import edit from './edit';
import metadata from './block.json';
import save from './save';
import variations from './variations';

const { name } = metadata;

Expand Down Expand Up @@ -135,4 +136,5 @@ export const settings = {
edit,
save,
deprecated,
variations,
};
18 changes: 18 additions & 0 deletions packages/block-library/src/group/variations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

const variations = [
{
name: 'group-row',
title: __( 'Row' ),
description: __( 'Blocks shown in a row.' ),
attributes: { layout: { type: 'flex' } },
scope: [ 'inserter' ],
isActive: ( blockAttributes ) =>
blockAttributes.layout?.type === 'flex',
},
];

export default variations;

0 comments on commit 5341bac

Please sign in to comment.