-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
3cee715
4a46fdc
83a9704
dee2ba2
872d6fe
b524cd5
5acc4e6
3d33685
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.wp-block-container .editor-block-list__block { | ||
// We need to enforce a cascade by telling nested blocks to inherit | ||
// their text color from the container block. | ||
color: inherit; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withFallbackStyles } from '@wordpress/components'; | ||
import { compose } from '@wordpress/compose'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
getColorClassName, | ||
withColors, | ||
ContrastChecker, | ||
InnerBlocks, | ||
InspectorControls, | ||
PanelColorSettings, | ||
} from '@wordpress/editor'; | ||
import { Fragment } from '@wordpress/element'; | ||
|
||
const applyFallbackStyles = withFallbackStyles( ( node, ownProps ) => { | ||
const { textColor, backgroundColor } = ownProps.attributes; | ||
|
||
return { | ||
fallbackBackgroundColor: backgroundColor, | ||
fallbackTextColor: textColor, | ||
}; | ||
} ); | ||
|
||
export const name = 'core/container'; | ||
|
||
export const settings = { | ||
title: __( 'Container' ), | ||
|
||
icon: <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M19 12h-2v3h-3v2h5v-5zM7 9h3V7H5v5h2V9zm14-6H3c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16.01H3V4.99h18v14.02z" /><path d="M0 0h24v24H0z" fill="none" /></svg>, | ||
|
||
category: 'layout', | ||
|
||
attributes: { | ||
align: { | ||
type: 'string', | ||
}, | ||
backgroundColor: { | ||
type: 'string', | ||
}, | ||
customBackgroundColor: { | ||
type: 'string', | ||
}, | ||
textColor: { | ||
type: 'string', | ||
}, | ||
customTextColor: { | ||
type: 'string', | ||
}, | ||
}, | ||
|
||
description: __( 'Group blocks into a container.' ), | ||
|
||
supports: { | ||
align: [ 'wide', 'full' ], | ||
anchor: true, | ||
}, | ||
|
||
edit: compose( [ | ||
withColors( 'backgroundColor', { textColor: 'color' } ), | ||
applyFallbackStyles, | ||
] )( ( props ) => { | ||
const { | ||
attributes: { | ||
customBackgroundColor, | ||
customTextColor, | ||
}, | ||
backgroundColor, | ||
className, | ||
fallbackBackgroundColor, | ||
fallbackTextColor, | ||
setBackgroundColor, | ||
setTextColor, | ||
textColor, | ||
} = props; | ||
|
||
return ( | ||
<Fragment> | ||
<InspectorControls> | ||
<PanelColorSettings | ||
title={ __( 'Color Settings' ) } | ||
initialOpen={ false } | ||
colorSettings={ [ | ||
{ | ||
value: backgroundColor.color, | ||
onChange: setBackgroundColor, | ||
label: __( 'Background Color' ), | ||
}, | ||
{ | ||
value: textColor.color, | ||
onChange: setTextColor, | ||
label: __( 'Text Color' ), | ||
}, | ||
] } | ||
> | ||
<ContrastChecker | ||
{ ...{ | ||
textColor: textColor.color, | ||
backgroundColor: backgroundColor.color, | ||
fallbackTextColor, | ||
fallbackBackgroundColor, | ||
} } | ||
/> | ||
</PanelColorSettings> | ||
</InspectorControls> | ||
<div | ||
className={ classnames( className, { | ||
'has-background': backgroundColor.color, | ||
'has-text-color': textColor.color, | ||
[ backgroundColor.class ]: backgroundColor.class, | ||
[ textColor.class ]: textColor.class, | ||
} ) } | ||
style={ { | ||
backgroundColor: customBackgroundColor && backgroundColor.color, | ||
color: customTextColor && textColor.color, | ||
} } | ||
> | ||
<InnerBlocks /> | ||
</div> | ||
</Fragment> | ||
); | ||
} ), | ||
|
||
save( { attributes } ) { | ||
const { | ||
backgroundColor, | ||
customBackgroundColor, | ||
customTextColor, | ||
textColor, | ||
} = attributes; | ||
|
||
const backgroundColorClass = getColorClassName( 'background-color', backgroundColor ); | ||
const textColorClass = getColorClassName( 'color', textColor ); | ||
|
||
return ( | ||
<div | ||
className={ classnames( { | ||
'has-background': backgroundColor || customBackgroundColor, | ||
'has-text-color': textColor || customTextColor, | ||
[ backgroundColorClass ]: backgroundColorClass, | ||
[ textColorClass ]: textColorClass, | ||
} ) } | ||
style={ { | ||
backgroundColor: backgroundColorClass ? undefined : customBackgroundColor, | ||
color: textColorClass ? undefined : customTextColor, | ||
} } | ||
> | ||
<InnerBlocks.Content /> | ||
</div> | ||
); | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.wp-block-container { | ||
// 1px top/bottom padding allows us to prevent margin collapsing while | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does margin collapse happen on the front-end, or is this meant to offset the editor styles? If the latter, should we then include this only in an editor-specific stylesheet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Front and back-end, but in my testing it was good/necessary for both cases. |
||
// avoiding excessive top/bottom margins. | ||
padding: 1px 1em; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!-- wp:container --> | ||
<div class="wp-block-container"> | ||
<!-- wp:paragraph --> | ||
<p>Container, Paragraph One</p> | ||
<!-- /wp:paragraph --> | ||
<!-- wp:paragraph --> | ||
<p>Container, Paragraph Two</p> | ||
<!-- /wp:paragraph --> | ||
</div> | ||
<!-- /wp:container --> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/container", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/paragraph", | ||
"isValid": true, | ||
"attributes": { | ||
"content": "Container, Paragraph One", | ||
"dropCap": false | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "<p>Container, Paragraph One</p>" | ||
}, | ||
{ | ||
"clientId": "_clientId_1", | ||
"name": "core/paragraph", | ||
"isValid": true, | ||
"attributes": { | ||
"content": "Container, Paragraph Two", | ||
"dropCap": false | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "<p>Container, Paragraph Two</p>" | ||
} | ||
], | ||
"originalContent": "<div class=\"wp-block-container\">\n\t\n\t\n</div>" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[ | ||
{ | ||
"blockName": "core/container", | ||
"attrs": {}, | ||
"innerBlocks": [ | ||
{ | ||
"blockName": "core/paragraph", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n\t<p>Container, Paragraph One</p>\n\t" | ||
}, | ||
{ | ||
"blockName": "core/paragraph", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n\t<p>Container, Paragraph Two</p>\n\t" | ||
} | ||
], | ||
"innerHTML": "\n<div class=\"wp-block-container\">\n\t\n\t\n</div>\n" | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!-- wp:container --> | ||
<div class="wp-block-container"><!-- wp:paragraph --> | ||
<p>Container, Paragraph One</p> | ||
<!-- /wp:paragraph --> | ||
|
||
<!-- wp:paragraph --> | ||
<p>Container, Paragraph Two</p> | ||
<!-- /wp:paragraph --></div> | ||
<!-- /wp:container --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is there a color otherwise being assigned that is breaking the default cascade? It's not clear to me that this should be needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aduth See my comment here: #10067 (comment)
We map
body
to.editor-block-list__block
insideeditor-styles.scss
which means each block resets the cascade for these properties.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a bit unfortunate 😕 But OK 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed we should refactor the editor styles to apply to the editor container. The downside though is that it can affect the UI bits (toolbars, movers...) so we need to be careful there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I almost wonder if there's a way to do a forced reset for Gutenberg chrome? Something to ponder for the future but for now this works :)