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

Update the heading block to use the colors hook #21039

Merged
merged 3 commits into from
Mar 27, 2020
Merged
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 packages/block-library/src/columns/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const migrateCustomColors = ( attributes ) => {
style.color.background = attributes.customBackgroundColor;
}
return {
...attributes,
...omit( attributes, [ 'customTextColor', 'customBackgroundColor' ] ),
style,
};
};
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/group/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { omit } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -20,7 +21,7 @@ const migrateCustomColors = ( attributes ) => {
style.color.background = attributes.customBackgroundColor;
}
return {
...attributes,
...omit( attributes, [ 'customTextColor', 'customBackgroundColor' ] ),
style,
};
};
Expand Down
6 changes: 0 additions & 6 deletions packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
},
"placeholder": {
"type": "string"
},
"textColor": {
"type": "string"
},
"customTextColor": {
"type": "string"
}
}
}
86 changes: 78 additions & 8 deletions packages/block-library/src/heading/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { omit } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -30,17 +31,77 @@ const blockAttributes = {
placeholder: {
type: 'string',
},
textColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
};

const migrateCustomColors = ( attributes ) => {
if ( ! attributes.customTextColor ) {
return attributes;
}
const style = {
color: {
text: attributes.customTextColor,
},
};
return {
...omit( attributes, [ 'customTextColor' ] ),
style,
};
};

const deprecated = [
{
attributes: blockAttributes,
supports: blockSupports,
Copy link
Member

Choose a reason for hiding this comment

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

I forgot about one thing, it would be nice to add the test case that covers this new deprecation:

  • a block that has has-text-color class set?

@jorgefilipecosta should know best what's the snippet that makes this deprecation unique.

Copy link
Member

Choose a reason for hiding this comment

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

I will look into the deprecations and try to add a snippet specific for this deprecation 👍

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @jorgefilipecosta 🙇

attributes: {
...blockAttributes,
customTextColor: {
type: 'string',
},
textColor: {
type: 'string',
},
},
migrate: migrateCustomColors,
save( { attributes } ) {
const {
align,
content,
customTextColor,
level,
textColor,
} = attributes;
const tagName = 'h' + level;

const textClass = getColorClassName( 'color', textColor );

const className = classnames( {
[ textClass ]: textClass,
'has-text-color': textColor || customTextColor,
[ `has-text-align-${ align }` ]: align,
} );

return (
<RichText.Content
className={ className ? className : undefined }
tagName={ tagName }
style={ {
color: textClass ? undefined : customTextColor,
} }
value={ content }
/>
);
},
},
{
attributes: {
...blockAttributes,
customTextColor: {
type: 'string',
},
textColor: {
type: 'string',
},
},
migrate: migrateCustomColors,
save( { attributes } ) {
const {
align,
Expand Down Expand Up @@ -73,7 +134,16 @@ const deprecated = [
},
{
supports: blockSupports,
attributes: blockAttributes,
attributes: {
...blockAttributes,
customTextColor: {
type: 'string',
},
textColor: {
type: 'string',
},
},
migrate: migrateCustomColors,
save( { attributes } ) {
const {
align,
Expand Down
63 changes: 23 additions & 40 deletions packages/block-library/src/heading/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,11 @@ import {
BlockControls,
InspectorControls,
RichText,
__experimentalUseColors,
__experimentalBlock as Block,
} from '@wordpress/block-editor';
import { useRef, Platform } from '@wordpress/element';
import { Platform } from '@wordpress/element';

function HeadingEdit( { attributes, setAttributes, mergeBlocks, onReplace } ) {
const ref = useRef();
const { TextColor, InspectorControlsColorPanel } = __experimentalUseColors(
[ { name: 'textColor', property: 'color' } ],
{
contrastCheckers: { backgroundColor: true, textColor: true },
colorDetector: { targetRef: ref },
},
[]
);

const { align, content, level, placeholder } = attributes;
const tagName = 'h' + level;

Expand Down Expand Up @@ -72,36 +61,30 @@ function HeadingEdit( { attributes, setAttributes, mergeBlocks, onReplace } ) {
</PanelBody>
</InspectorControls>
) }
{ InspectorControlsColorPanel }
<TextColor>
<RichText
ref={ ref }
identifier="content"
tagName={ Block[ tagName ] }
value={ content }
onChange={ ( value ) =>
setAttributes( { content: value } )
<RichText
identifier="content"
tagName={ Block[ tagName ] }
value={ content }
onChange={ ( value ) => setAttributes( { content: value } ) }
onMerge={ mergeBlocks }
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( 'core/paragraph' );
}
onMerge={ mergeBlocks }
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( 'core/paragraph' );
}

return createBlock( 'core/heading', {
...attributes,
content: value,
} );
} }
onReplace={ onReplace }
onRemove={ () => onReplace( [] ) }
className={ classnames( {
[ `has-text-align-${ align }` ]: align,
} ) }
placeholder={ placeholder || __( 'Write heading…' ) }
textAlign={ align }
/>
</TextColor>
return createBlock( 'core/heading', {
...attributes,
content: value,
} );
} }
onReplace={ onReplace }
onRemove={ () => onReplace( [] ) }
className={ classnames( {
[ `has-text-align-${ align }` ]: align,
} ) }
placeholder={ placeholder || __( 'Write heading…' ) }
textAlign={ align }
/>
</>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const settings = {
anchor: true,
__unstablePasteTextInline: true,
lightBlockWrapper: true,
__experimentalColor: true,
},
example: {
attributes: {
Expand Down
11 changes: 2 additions & 9 deletions packages/block-library/src/heading/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,20 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { getColorClassName, RichText } from '@wordpress/block-editor';
import { RichText } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { align, content, customTextColor, level, textColor } = attributes;
const { align, content, level } = attributes;
const tagName = 'h' + level;

const textClass = getColorClassName( 'color', textColor );

const className = classnames( {
[ textClass ]: textClass,
'has-text-color': textColor || customTextColor,
[ `has-text-align-${ align }` ]: align,
} );

return (
<RichText.Content
className={ className ? className : undefined }
tagName={ tagName }
style={ {
color: textClass ? undefined : customTextColor,
} }
value={ content }
/>
);
Expand Down
13 changes: 13 additions & 0 deletions packages/block-library/src/heading/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
h1,
Copy link
Member

Choose a reason for hiding this comment

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

The same question as for Paragraph block:

Are you sure it’s safe to set those values for all headings including front-end as part of the block definition?

h2,
h3,
h4,
h5,
h6 {
background-color: var(--wp--color--background);
color: var(--wp--color--text);

&.has-background {
padding: $block-bg-padding--v $block-bg-padding--h;
}
}
2 changes: 1 addition & 1 deletion packages/block-library/src/paragraph/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const migrateCustomColors = ( attributes ) => {
style.color.background = attributes.customBackgroundColor;
}
return {
...attributes,
...omit( attributes, [ 'customTextColor', 'customBackgroundColor' ] ),
style,
};
};
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@import "./file/style.scss";
@import "./gallery/style.scss";
@import "./group/style.scss";
@import "./heading/style.scss";
@import "./image/style.scss";
@import "./latest-comments/style.scss";
@import "./latest-posts/style.scss";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"attributes": {
"content": "Text",
"level": 2,
"customTextColor": "#268ebb"
"style": {
"color": {
"text": "#268ebb"
}
}
},
"innerBlocks": [],
"originalContent": "<h2 style=\"color:#268ebb\">Text</h2>"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:heading {"customTextColor":"#268ebb"} -->
<h2 class="has-text-color" style="color:#268ebb">Text</h2>
<!-- wp:heading {"style":{"color":{"text":"#268ebb"}}} -->
<h2 class="has-text-color" style="--wp--color--text:#268ebb">Text</h2>
<!-- /wp:heading -->
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ exports[`Heading can be created by prefixing number sign and a space 1`] = `
`;

exports[`Heading it should correctly apply custom colors 1`] = `
"<!-- wp:heading {\\"level\\":3,\\"customTextColor\\":\\"#7700ff\\"} -->
<h3 class=\\"has-text-color\\" style=\\"color:#7700ff\\">Heading</h3>
"<!-- wp:heading {\\"level\\":3,\\"style\\":{\\"color\\":{\\"text\\":\\"#7700ff\\"}}} -->
<h3 class=\\"has-text-color\\" style=\\"--wp--color--text:#7700ff\\">Heading</h3>
<!-- /wp:heading -->"
`;

Expand Down