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 initial draft of container block #10562

Closed
wants to merge 8 commits into from
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as preformatted from './preformatted';
import * as pullquote from './pullquote';
import * as reusableBlock from './block';
import * as separator from './separator';
import * as section from './section';
import * as shortcode from './shortcode';
import * as spacer from './spacer';
import * as subhead from './subhead';
Expand Down Expand Up @@ -83,6 +84,7 @@ export const registerCoreBlocks = () => {
nextpage,
preformatted,
pullquote,
section,
separator,
reusableBlock,
spacer,
Expand Down
107 changes: 107 additions & 0 deletions packages/block-library/src/section/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { Fragment } from '@wordpress/element';
import {
getColorClassName,
withColors,
InspectorControls,
InnerBlocks,
PanelColorSettings,
} from '@wordpress/editor';
Copy link
Member

Choose a reason for hiding this comment

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

This is just a nitpick, but could the imports be put in alphabetical order?

Copy link
Member Author

Choose a reason for hiding this comment

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

@ZebulanStanphill I’m waiting to get some feedback on the UX but if we determine to move forward I have a lot of tweaks I’ll make to the code itself. Thanks :)


export const name = 'core/section';
Copy link
Member

Choose a reason for hiding this comment

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

I think "container" is a better name, since "section" implies the usage of a <section> element, but often you just want to use a simple container just to add a background to something that is not semantically a section. Issues to add color options to other blocks keep being rejected because of the assumption that a generic container block could accomplish the same thing. But if the block is limited to using the <section> element, then that limits its usage, unless you want to encourage bad semantics.


export const settings = {
title: sprintf(
/* translators: Block title modifier */
__( '%1$s (%2$s)' ),
__( 'Section' ),
__( 'beta' )
),

icon: <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0V0z" /><g><path d="M21 4H3L2 5v14l1 1h18l1-1V5l-1-1zM8 18H4V6h4v12zm6 0h-4V6h4v12zm6 0h-4V6h4v12z" /></g></svg>,

category: 'layout',

attributes: {
align: {
type: 'string',
},
backgroundColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
},

description: __( 'Group a selection of blocks into a container.' ),

supports: {
align: [ 'wide', 'full' ],
},

edit: withColors( 'backgroundColor' )( ( props ) => {
const {
backgroundColor,
className,
setBackgroundColor,
} = props;

return (
<Fragment>
<InspectorControls>
<PanelColorSettings
title={ __( 'Color Settings' ) }
initialOpen={ false }
colorSettings={ [
{
value: backgroundColor.color,
onChange: setBackgroundColor,
label: __( 'Background Color' ),
},
] }
/>
</InspectorControls>
<div
className={ classnames( 'wp-block-section', className, {
'has-background': backgroundColor.color,
[ backgroundColor.class ]: backgroundColor.class,
} ) }
style={ {
backgroundColor: backgroundColor.color,
} }
>
<InnerBlocks />
</div>
</Fragment>
);
} ),

save( { attributes } ) {
const {
backgroundColor,
customBackgroundColor,
} = attributes;

const backgroundClass = getColorClassName( 'background-color', backgroundColor );

const className = classnames( {
'has-background': backgroundColor || customBackgroundColor,
[ backgroundClass ]: backgroundClass,
} );

return (
<div className={ className }>
<InnerBlocks.Content />
</div>
);
},
};