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

Layout: Always add semantic classes #60668

Merged
merged 3 commits into from
Apr 15, 2024
Merged
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
15 changes: 12 additions & 3 deletions packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ function BlockWithLayoutStyles( {
block: BlockListBlock,
props,
blockGapSupport,
layoutClasses,
} ) {
const { name, attributes } = props;
const id = useInstanceId( BlockListBlock );
Expand All @@ -369,7 +370,6 @@ function BlockWithLayoutStyles( {
layout?.inherit || layout?.contentSize || layout?.wideSize
? { ...layout, type: 'constrained' }
: layout || defaultBlockLayout || {};
const layoutClasses = useLayoutClasses( attributes, name );

const { kebabCase } = unlock( componentsPrivateApis );
const selectorPrefix = `wp-container-${ kebabCase( name ) }-is-layout-`;
Expand Down Expand Up @@ -415,8 +415,9 @@ function BlockWithLayoutStyles( {
*/
export const withLayoutStyles = createHigherOrderComponent(
( BlockListBlock ) => ( props ) => {
const { clientId, name } = props;
const { clientId, name, attributes } = props;
const blockSupportsLayout = hasLayoutBlockSupport( name );
const layoutClasses = useLayoutClasses( attributes, name );
const extraProps = useSelect(
( select ) => {
// The callback returns early to avoid block editor subscription.
Expand Down Expand Up @@ -444,13 +445,21 @@ export const withLayoutStyles = createHigherOrderComponent(
);

if ( ! extraProps ) {
return <BlockListBlock { ...props } />;
return (
<BlockListBlock
{ ...props }
__unstableLayoutClassNames={
blockSupportsLayout ? layoutClasses : undefined
}
/>
);
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem seems to be that useLayoutClasses doesn't actually check if the block supports layout in order to output the classes (which is fine, that's not its role), so here, we're always passing in layout classes, whether the block supports layout or not. I think the check here should be blockSupportsLayout ? layoutClasses : undefined

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, good catch!

Copy link
Contributor

@tellthemachines tellthemachines Apr 12, 2024

Choose a reason for hiding this comment

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

For posterity: the unwanted layout classes were appearing on Page List because it uses innerBlocksProps, where those classnames are retrieved from the block edit context and added to the inner blocks wrapper. So this would happen on any block with inner blocks and without layout support.

}

return (
<BlockWithLayoutStyles
block={ BlockListBlock }
props={ props }
layoutClasses={ layoutClasses }
{ ...extraProps }
/>
);
Expand Down
Loading