Skip to content

Commit

Permalink
Implemented mechanism to use classes for configured colors instead of…
Browse files Browse the repository at this point in the history
… inline styles.
  • Loading branch information
jorgefilipecosta committed Mar 21, 2018
1 parent f4cc3bf commit f95623c
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 31 deletions.
90 changes: 90 additions & 0 deletions blocks/color-mechanism/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* External dependencies
*/
import { find, get, kebabCase } from 'lodash';

/**
* WordPress dependencies
*/
import { getWrapperDisplayName } from '@wordpress/element';
import { withContext } from '@wordpress/components';

/**
* Internal dependencies
*/
import './style.scss';

const getColorValue = ( colors, namedColor, customColor ) => {
if ( namedColor ) {
const colorObj = find( colors, { name: namedColor } );
return colorObj && colorObj.color;
}
if ( customColor ) {
return customColor;
}
};

const setColorValue = ( colors, colorAttributeName, customColorAttributeName, setAttributes ) =>
( colorValue ) => {
const colorObj = find( colors, { color: colorValue } );
if ( colorObj ) {
setAttributes( {
[ colorAttributeName ]: colorObj.name,
[ customColorAttributeName ]: undefined,
} );
return;
}
setAttributes( {
[ colorAttributeName ]: undefined,
[ customColorAttributeName ]: colorValue,
} );
};

/**
* Returns a class based on the context a color is being used and its name.
*
* @param {string} colorContextName Context/place where color is being used e.g: background, text etc...
* @param {string} colorName Name of the color.
*
* @return {string} String with the class corresponding to the color in the provided context.
*/
export function getColorClass( colorContextName, colorName ) {
if ( ! colorContextName || ! colorName ) {
return undefined;
}

return `has-${ kebabCase( colorName ) }-${ colorContextName }-color`;
}

/**
* Higher-order component, which handles color logic for class generation
* color value, retrieval and color attribute setting.
*
* @param {WPElement} WrappedComponent The wrapped component.
*
* @return {Component} Component with a new colors prop.
*/
export function withColors( WrappedComponent ) {
const ComponentWithColorContext = withContext( 'editor' )(
( settings, props ) => {
const colors = get( settings, 'colors', [] );
return {
colors: ( colorContextName, colorAttributeName, customColorAttributeName ) => ( {
value: getColorValue(
colors,
props.attributes[ colorAttributeName ],
props.attributes[ customColorAttributeName ]
),
class: getColorClass( colorContextName, props.attributes[ colorAttributeName ] ),
set: setColorValue( colors, colorAttributeName, customColorAttributeName, props.setAttributes ),
} ),
};
} )( WrappedComponent );

const NewComponent = ( props ) => {
return <ComponentWithColorContext { ...props } />;
};
NewComponent.displayName = getWrapperDisplayName( WrappedComponent, 'colorMechanism' );

return NewComponent;
}
87 changes: 87 additions & 0 deletions blocks/color-mechanism/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
p.has-pale-pink-background-color {
background-color: #f78da7;
}

p.has-vivid-red-background-color {
background-color: #cf2e2e;
}

p.has-luminous-vivid-orange-background-color {
background-color: #ff6900;
}

p.has-luminous-vivid-amber-background-color {
background-color: #fcb900;
}

p.has-light-green-cyan-background-color {
background-color: #7bdcb5;
}

p.has-vivid-green-cyan-background-color {
background-color: #00d084;
}

p.has-pale-cyan-blue-background-color {
background-color: #8ed1fc;
}

p.has-vivid-cyan-blue-background-color {
background-color: #0693e3;
}

p.has-very-light-gray-background-color {
background-color: #eeeeee;
}

p.has-cyan-bluish-gray-background-color {
background-color: #abb8c3;
}

p.has-very-dark-gray-background-color {
background-color: #313131;
}

p.has-pale-pink-text-color {
color: #f78da7;
}

p.has-vivid-red-text-color {
color: #cf2e2e;
}

p.has-luminous-vivid-orange-text-color {
color: #ff6900;
}

p.has-luminous-vivid-amber-text-color {
color: #fcb900;
}

p.has-light-green-cyan-text-color {
color: #7bdcb5;
}

p.has-vivid-green-cyan-text-color {
color: #00d084;
}

p.has-pale-cyan-blue-text-color {
color: #8ed1fc;
}

p.has-vivid-cyan-blue-text-color {
color: #0693e3;
}

p.has-very-light-gray-text-color {
color: #eeeeee;
}

p.has-cyan-bluish-gray-text-color {
color: #abb8c3;
}

p.has-very-dark-gray-text-color {
color: #313131;
}
2 changes: 1 addition & 1 deletion blocks/color-palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function ColorPalette( { colors, disableCustomColors = false, value, onCh

return (
<div className="blocks-color-palette">
{ map( colors, ( color ) => {
{ map( colors, ( { color } ) => {
const style = { color: color };
const className = classnames( 'blocks-color-palette__item', { 'is-active': value === color } );

Expand Down
2 changes: 1 addition & 1 deletion blocks/color-palette/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { shallow } from 'enzyme';
import { ColorPalette } from '../';

describe( 'ColorPalette', () => {
const colors = [ 'red', 'white', 'blue' ];
const colors = [ { name: 'red', color: 'red' }, { name: 'white', color: 'white' }, { name: 'blue', color: 'blue' } ];
const currentColor = 'red';
const onChange = jest.fn();

Expand Down
2 changes: 1 addition & 1 deletion blocks/library/paragraph/editor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.editor-block-list__block:not( .is-multi-selected ) .wp-block-paragraph {
.editor-block-list__block:not( .is-multi-selected ) .wp-block-paragraph:not( .has-background ) {
background: white;
}

Expand Down
52 changes: 35 additions & 17 deletions blocks/library/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import RichText from '../../rich-text';
import InspectorControls from '../../inspector-controls';
import ColorPalette from '../../color-palette';
import ContrastChecker from '../../contrast-checker';
import { getColorClass, withColors } from '../../color-mechanism';

const { getComputedStyle } = window;

Expand Down Expand Up @@ -129,20 +130,22 @@ class ParagraphBlock extends Component {
mergeBlocks,
onReplace,
className,
colors,
} = this.props;

const {
align,
content,
dropCap,
placeholder,
backgroundColor,
textColor,
width,
} = attributes;

const fontSize = this.getFontSize();

const textColor = colors( 'text', 'textColor', 'customTextColor' );
const backgroundColor = colors( 'background', 'backgroundColor', 'customBackgroundColor' );

return [
isSelected && (
<BlockControls key="controls">
Expand Down Expand Up @@ -198,22 +201,22 @@ class ParagraphBlock extends Component {
onChange={ this.toggleDropCap }
/>
</PanelBody>
<PanelColor title={ __( 'Background Color' ) } colorValue={ backgroundColor } initialOpen={ false }>
<PanelColor title={ __( 'Background Color' ) } colorValue={ backgroundColor.value } initialOpen={ false }>
<ColorPalette
value={ backgroundColor }
onChange={ ( colorValue ) => setAttributes( { backgroundColor: colorValue } ) }
value={ backgroundColor.value }
onChange={ backgroundColor.set }
/>
</PanelColor>
<PanelColor title={ __( 'Text Color' ) } colorValue={ textColor } initialOpen={ false }>
<PanelColor title={ __( 'Text Color' ) } colorValue={ textColor.value } initialOpen={ false }>
<ColorPalette
value={ textColor }
onChange={ ( colorValue ) => setAttributes( { textColor: colorValue } ) }
value={ textColor.value }
onChange={ textColor.set }
/>
</PanelColor>
{ this.nodeRef && <ContrastCheckerWithFallbackStyles
node={ this.nodeRef }
textColor={ textColor }
backgroundColor={ backgroundColor }
textColor={ textColor.value }
backgroundColor={ backgroundColor.value }
isLargeText={ fontSize >= 18 }
/> }
<PanelBody title={ __( 'Block Alignment' ) }>
Expand All @@ -233,12 +236,14 @@ class ParagraphBlock extends Component {
<RichText
tagName="p"
className={ classnames( 'wp-block-paragraph', className, {
'has-background': backgroundColor,
'has-background': backgroundColor.value,
'has-drop-cap': dropCap,
[ backgroundColor.class ]: backgroundColor.class,
[ textColor.class ]: textColor.class,
} ) }
style={ {
backgroundColor: backgroundColor,
color: textColor,
backgroundColor: ! backgroundColor.class ? backgroundColor.value : undefined,
color: ! textColor.class ? textColor.value : undefined,
fontSize: fontSize ? fontSize + 'px' : undefined,
textAlign: align,
} }
Expand Down Expand Up @@ -302,9 +307,15 @@ const schema = {
textColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
backgroundColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
fontSize: {
type: 'string',
},
Expand Down Expand Up @@ -414,7 +425,7 @@ export const settings = {
}
},

edit: ParagraphBlock,
edit: withColors( ParagraphBlock ),

save( { attributes } ) {
const {
Expand All @@ -424,20 +435,27 @@ export const settings = {
dropCap,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor,
fontSize,
customFontSize,
} = attributes;

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

const className = classnames( {
[ `align${ width }` ]: width,
'has-background': backgroundColor,
'has-background': backgroundColor || customBackgroundColor,
'has-drop-cap': dropCap,
[ `is-${ fontSize }-text` ]: fontSize && FONT_SIZES[ fontSize ],
[ textClass ]: textClass,
[ backgroundClass ]: backgroundClass,
} );

const styles = {
backgroundColor: backgroundColor,
color: textColor,
backgroundColor: ! backgroundColor ? customBackgroundColor : undefined,
color: ! textColor ? customTextColor : undefined,
fontSize: ! fontSize && customFontSize ? customFontSize : undefined,
textAlign: align,
};
Expand Down
Loading

0 comments on commit f95623c

Please sign in to comment.