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

Add content width to Group block by default #42582

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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ Gather blocks in a layout container. ([Source](https://github.com/WordPress/gute
- **Name:** core/group
- **Category:** design
- **Supports:** align (full, wide), anchor, ariaLabel, color (background, gradients, link, text), spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** tagName, templateLock
- **Attributes:** layout, tagName, templateLock

## Heading

Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"templateLock": {
"type": [ "string", "boolean" ],
"enum": [ "all", "insert", false ]
},
"layout": {
"type": "object",
"default": {
"inherit": true
}
}
},
"supports": {
Expand Down
74 changes: 74 additions & 0 deletions packages/block-library/src/group/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
InnerBlocks,
getColorClassName,
useBlockProps,
useInnerBlocksProps,
} from '@wordpress/block-editor';

const migrateAttributes = ( attributes ) => {
Expand Down Expand Up @@ -38,6 +39,79 @@ const migrateAttributes = ( attributes ) => {
};

const deprecated = [
// Version with no default content width.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need the deprecation? The markup of the two different versions should be identical, so I wasn't sure if it needs a migration, since we don't want to make changes to existing blocks 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again (and this might be connected to what I'm missing above?) if I don't add the migration, existing blocks with no layout set suddenly acquire a content width 😬

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, excellent, thanks for confirming — at least knowing that much gives us a good base of behaviour for exploring our options 👍

{
attributes: {
tagName: {
type: 'string',
default: 'div',
},
templateLock: {
type: [ 'string', 'boolean' ],
enum: [ 'all', 'insert', false ],
},
},
supports: {
__experimentalOnEnter: true,
__experimentalSettings: true,
align: [ 'wide', 'full' ],
anchor: true,
ariaLabel: true,
html: false,
color: {
gradients: true,
link: true,
__experimentalDefaultControls: {
background: true,
text: true,
},
},
spacing: {
margin: [ 'top', 'bottom' ],
padding: true,
blockGap: true,
__experimentalDefaultControls: {
padding: true,
blockGap: true,
},
},
__experimentalBorder: {
color: true,
radius: true,
style: true,
width: true,
__experimentalDefaultControls: {
color: true,
radius: true,
style: true,
width: true,
},
},
typography: {
fontSize: true,
lineHeight: true,
__experimentalFontStyle: true,
__experimentalFontWeight: true,
__experimentalLetterSpacing: true,
__experimentalTextTransform: true,
__experimentalDefaultControls: {
fontSize: true,
},
},
__experimentalLayout: true,
},
isEligible: ( { layout } ) => ! layout,
migrate: ( attributes ) => {
if ( ! attributes.layout ) {
return attributes;
}
},
save( { attributes: { tagName: Tag } } ) {
return (
<Tag { ...useInnerBlocksProps.save( useBlockProps.save() ) } />
);
},
},
// Version of the block with the double div.
{
attributes: {
Expand Down
13 changes: 12 additions & 1 deletion packages/block-library/src/group/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import {
InnerBlocks,
useBlockProps,
Expand Down Expand Up @@ -67,6 +68,16 @@ function GroupEdit( { attributes, setAttributes, clientId } ) {
}
);

const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
const { inherit = false } = layout;
useEffect( () => {
if ( inherit ) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { layout: { inherit } } );
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this line required? It looks like inherit is retrieved from layout.attributes already, so I wasn't sure what the setAttributes call winds up doing here 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm probably doing something wrong elsewhere, but { layout: { inherit } } doesn't actually get saved to the block attributes without this bit of code. So without it, if you add a Group and then go into code view you can see the layout attribute isn't there. I feel there must be something I'm missing, not sure what 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, gotcha! I wonder if it's to do with the behaviour where a default value isn't serialized, but if we switch a value off and on so that setAttributes is explicitly called, then it is serialized 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That might be it! I'll dig around for any precedent with adding default attributes to blocks and see if there's a better way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking through block-library, I can't find another instance of an attribute having to be set on a block by default, without being tied to a specific action (e.g. in File and Image, new attributes have been added, but they are only set during upload). Not sure we'll be able to do without explicitly setting the attribute.

This is awkward, especially if new blocks want to have content width set by default 🤔

I'll continue looking into it.

}
}, [ inherit ] );

return (
<>
<InspectorControls __experimentalGroup="advanced">
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/group/variations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const variations = [
name: 'group',
title: __( 'Group' ),
description: __( 'Gather blocks in a layout container.' ),
attributes: { layout: { type: 'default' } },
attributes: { layout: { inherit: true } },
scope: [ 'transform' ],
isActive: ( blockAttributes ) =>
! blockAttributes.layout ||
! blockAttributes.layout?.type ||
blockAttributes.layout?.inherit === true ||
blockAttributes.layout?.type === 'default',
icon: group,
},
Expand Down