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

Implemented color class mechanism in button. #6422

Merged
merged 1 commit into from
Apr 27, 2018
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
148 changes: 125 additions & 23 deletions core-blocks/button/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { omit, pick } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -19,6 +25,8 @@ import {
ColorPalette,
ContrastChecker,
InspectorControls,
getColorClass,
withColors,
} from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -67,6 +75,7 @@ class ButtonBlock extends Component {
render() {
const {
attributes,
initializeColor,
setAttributes,
isSelected,
className,
Expand All @@ -77,11 +86,20 @@ class ButtonBlock extends Component {
url,
title,
align,
color,
textColor,
clear,
} = attributes;

const textColor = initializeColor( {
Copy link
Contributor

Choose a reason for hiding this comment

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

This intiailizeColor calls are bothering me a bit. It seems too magic to me. I wonder if it's not better to transform the withColors HoC to something like

withColors( ( getColor, { attributes } ) => ( {
   backgroundColor: getColor( attributes.backgroundColor, attributes.customBackgroundColor, 'background-color' )
} )

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the review and for the feedback @youknowriad. I like your suggestion, I will give it a try in a separate PR.
Regarding the setters (the logic to set the name attribute or the custom one) do you think we should pass a getColor to retrieve the data and a setColor to build the setter, or getColor returns the setter inside similar to what initiailizeColor does?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd lean towards a separate setColor

colorContext: 'color',
colorAttribute: 'textColor',
customColorAttribute: 'customTextColor',
} );
const backgroundColor = initializeColor( {
colorContext: 'background-color',
colorAttribute: 'backgroundColor',
customColorAttribute: 'customBackgroundColor',
} );

return (
<Fragment>
<BlockControls>
Expand All @@ -94,10 +112,17 @@ class ButtonBlock extends Component {
value={ text }
onChange={ ( value ) => setAttributes( { text: value } ) }
formattingControls={ [ 'bold', 'italic', 'strikethrough' ] }
className="wp-block-button__link"
className={ classnames(
'wp-block-button__link', {
'has-background': backgroundColor.value,
[ backgroundColor.class ]: backgroundColor.class,
'has-text-color': textColor.value,
[ textColor.class ]: textColor.class,
}
) }
style={ {
backgroundColor: color,
color: textColor,
backgroundColor: backgroundColor.class ? undefined : backgroundColor.value,
color: textColor.class ? undefined : textColor.value,
} }
keepPlaceholderOnFocus
/>
Expand All @@ -108,22 +133,22 @@ class ButtonBlock extends Component {
checked={ !! clear }
onChange={ this.toggleClear }
/>
<PanelColor title={ __( 'Background Color' ) } colorValue={ color } >
<PanelColor title={ __( 'Background Color' ) } colorValue={ backgroundColor.value } >
<ColorPalette
value={ color }
onChange={ ( colorValue ) => setAttributes( { color: colorValue } ) }
value={ backgroundColor.value }
onChange={ backgroundColor.set }
/>
</PanelColor>
<PanelColor title={ __( 'Text Color' ) } colorValue={ textColor } >
<PanelColor title={ __( 'Text Color' ) } colorValue={ textColor.value } >
<ColorPalette
value={ textColor }
onChange={ ( colorValue ) => setAttributes( { textColor: colorValue } ) }
value={ textColor.value }
onChange={ textColor.set }
/>
</PanelColor>
{ this.nodeRef && <ContrastCheckerWithFallbackStyles
node={ this.nodeRef }
textColor={ textColor }
backgroundColor={ color }
textColor={ textColor.value }
backgroundColor={ backgroundColor.value }
isLargeText={ true }
/> }
</PanelBody>
Expand Down Expand Up @@ -168,16 +193,30 @@ const blockAttributes = {
type: 'string',
default: 'none',
},
color: {
backgroundColor: {
type: 'string',
},
textColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
};

export const name = 'core/button';

const colorsMigration = ( attributes ) => {
return omit( {
...attributes,
customTextColor: attributes.textColor && '#' === attributes.textColor[ 0 ] ? attributes.textColor : undefined,
customBackgroundColor: attributes.color && '#' === attributes.color[ 0 ] ? attributes.color : undefined,
}, [ 'color', 'textColor' ] );
};

export const settings = {
title: __( 'Button' ),

Expand All @@ -204,23 +243,40 @@ export const settings = {
return props;
},

edit: ButtonBlock,
edit: withColors( ButtonBlock ),

save( { attributes } ) {
const { url, text, title, align, color, textColor } = attributes;
const {
url,
text,
title,
align,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor,
} = attributes;

const textClass = getColorClass( 'color', textColor );
const backgroundClass = getColorClass( 'background-color', backgroundColor );

const buttonClasses = classnames( 'wp-block-button__link', {
'has-text-color': textColor || customTextColor,
[ textClass ]: textClass,
'has-background': backgroundColor || customBackgroundColor,
[ backgroundClass ]: backgroundClass,
} );

const buttonStyle = {
backgroundColor: color,
color: textColor,
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor,
};

const linkClass = 'wp-block-button__link';

return (
<div className={ `align${ align }` }>
<RichText.Content
tagName="a"
className={ linkClass }
className={ buttonClasses }
href={ url }
title={ title }
style={ buttonStyle }
Expand All @@ -231,7 +287,51 @@ export const settings = {
},

deprecated: [ {
attributes: blockAttributes,
attributes: {
...pick( blockAttributes, [ 'url', 'title', 'text', 'align' ] ),
color: {
type: 'string',
},
textColor: {
type: 'string',
},
},

save( { attributes } ) {
const { url, text, title, align, color, textColor } = attributes;

const buttonStyle = {
backgroundColor: color,
color: textColor,
};

const linkClass = 'wp-block-button__link';

return (
<div className={ `align${ align }` }>
<RichText.Content
tagName="a"
className={ linkClass }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
/>
</div>
);
},
migrate: colorsMigration,
},
{
attributes: {
...pick( blockAttributes, [ 'url', 'title', 'text', 'align' ] ),
color: {
type: 'string',
},
textColor: {
type: 'string',
},
},

save( { attributes } ) {
const { url, text, title, align, color, textColor } = attributes;
Expand All @@ -248,5 +348,7 @@ export const settings = {
</div>
);
},
} ],
migrate: colorsMigration,
},
],
};
27 changes: 18 additions & 9 deletions core-blocks/button/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ $blocks-button__line-height: $big-font-size + 6px;
margin-bottom: 1.5em;

& .wp-block-button__link {
background-color: $dark-gray-700;
border: none;
border-radius: $blocks-button__height / 2;
box-shadow: none;
color: $white;
cursor: pointer;
display: inline-block;
font-size: $big-font-size;
Expand All @@ -20,13 +18,6 @@ $blocks-button__line-height: $big-font-size + 6px;
text-decoration: none;
white-space: normal;
word-break: break-all;

&:hover,
&:focus,
&:active {
background-color: $dark-gray-700;
color: $white;
}
}

&.aligncenter {
Expand All @@ -37,3 +28,21 @@ $blocks-button__line-height: $big-font-size + 6px;
text-align: right;
}
}

.wp-block-button__link:not(.has-background) {
background-color: $dark-gray-700;
&:hover,
&:focus,
&:active {
background-color: $dark-gray-700;
}
}

.wp-block-button__link:not(.has-text-color) {
color: $white;
&:hover,
&:focus,
&:active {
color: $white;
}
}