Skip to content

Commit

Permalink
Display two ColorPallette components in PanelColor, one for bg, one f…
Browse files Browse the repository at this point in the history
…or text

Also bring title color rendering that was previously in another PanelColor
component into this one.

Next ColorPallettes in BaseControl components

Extract repeated code for building color labels into separate function

Handle change events for both text and bg colors

Uncouple the FallbackStyles HOC from the ContrastChecker in the Button block

Reorganise props to PanelColor, group text and background props into objects

Move ContrastChecker into PanelColor component

Remove incorrect class name

Move title rendering into a separate func and use Fragement over array

Reintroduce the old PanelColor component and rename this new one PanelTextColor

Extract PanelBody for text color panel into its own component

Hide/show text color preview dependent on the open/close state of the panel

Extract ColorArea into its own component so that it can be used in more than just panels

Revert "Extract PanelBody for text color panel into its own component"

This reverts commit fe7a803.

Refactor PanelTextColor component to reuse title for both PanelBody and BaseControl

 Relocate styles for hiding/showing ColorAreas to PanelTextColor

Modify ColorPalette component to accept an additional class name

Styling tweaks for text color pallete

Fix incorrect class name on ColorArea

Refactoring to remove need to add class to ColorArea

Minor tidy up to avoid use of not needed consts

Rename from PanelTextColor to PanelColorSettings

Update PanelColor tests to reflect use of ColorArea component

Add snapshot tests for new components

Minor formatting change

Fixes for FallbackStyles HOC usage on button.

Migrate to new compose package for PanelColorSettings and button block

Rename ColorArea to ColorIndicator

Make ContrastChecker render as a child of the PanelColorSettings component

Refactor PanelColorSettings to accept an array of settings

Make TextWithColorIndicators a component

Fix classname typo - I can't spell 'palette'.

Remove unecessary span in favour of Fragment

Refactor, avoid passing colors through several levels

Extract ColorPaletteControl, TextWithColorIndicators and a ColorIndicator that has color context from PanelColorSettings

Set initialOpen for color palettes to false for Paragraph block
  • Loading branch information
talldan committed Jul 20, 2018
1 parent f820d14 commit 32aa85d
Show file tree
Hide file tree
Showing 21 changed files with 435 additions and 94 deletions.
74 changes: 49 additions & 25 deletions core-blocks/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import {
Component,
Fragment,
} from '@wordpress/element';
import { compose } from '@wordpress/compose';
import {
Dashicon,
IconButton,
Expand All @@ -18,10 +22,10 @@ import {
RichText,
BlockControls,
BlockAlignmentToolbar,
ContrastChecker,
InspectorControls,
ContrastChecker,
withColors,
PanelColor,
PanelColorSettings,
} from '@wordpress/editor';

/**
Expand All @@ -31,15 +35,17 @@ import './editor.scss';

const { getComputedStyle } = window;

const ContrastCheckerWithFallbackStyles = withFallbackStyles( ( node, ownProps ) => {
const FallbackStyles = withFallbackStyles( ( node, ownProps ) => {
const { textColor, backgroundColor } = ownProps;
const backgroundColorValue = backgroundColor && backgroundColor.value;
const textColorValue = textColor && textColor.value;
//avoid the use of querySelector if textColor color is known and verify if node is available.
const textNode = ! textColor && node ? node.querySelector( '[contenteditable="true"]' ) : null;
const textNode = ! textColorValue && node ? node.querySelector( '[contenteditable="true"]' ) : null;
return {
fallbackBackgroundColor: backgroundColor || ! node ? undefined : getComputedStyle( node ).backgroundColor,
fallbackTextColor: textColor || ! textNode ? undefined : getComputedStyle( textNode ).color,
fallbackBackgroundColor: backgroundColorValue || ! node ? undefined : getComputedStyle( node ).backgroundColor,
fallbackTextColor: textColorValue || ! textNode ? undefined : getComputedStyle( textNode ).color,
};
} )( ContrastChecker );
} );

class ButtonEdit extends Component {
constructor() {
Expand Down Expand Up @@ -67,6 +73,8 @@ class ButtonEdit extends Component {
textColor,
setBackgroundColor,
setTextColor,
fallbackBackgroundColor,
fallbackTextColor,
setAttributes,
isSelected,
className,
Expand Down Expand Up @@ -106,22 +114,35 @@ class ButtonEdit extends Component {
keepPlaceholderOnFocus
/>
<InspectorControls>
<PanelColor
colorValue={ backgroundColor.value }
title={ __( 'Background Color' ) }
onChange={ setBackgroundColor }
/>
<PanelColor
colorValue={ textColor.value }
title={ __( 'Text Color' ) }
onChange={ setTextColor }
/>
{ this.nodeRef && <ContrastCheckerWithFallbackStyles
node={ this.nodeRef }
textColor={ textColor.value }
backgroundColor={ backgroundColor.value }
isLargeText={ true }
/> }
<PanelColorSettings
title={ __( 'Color Settings' ) }
colorSettings={ [
{
value: backgroundColor.value,
onChange: setBackgroundColor,
label: __( 'Background Color' ),
// translators: %s: The name of the color e.g: "vivid red" or color hex code if name is not available e.g: "#f00".
colorIndicatorAriaLabel: __( '(current background color: %s)' ),
},
{
value: textColor.value,
onChange: setTextColor,
label: __( 'Text Color' ),
// translators: %s: The name of the color e.g: "vivid red" or color hex code if name is not available e.g: "#f00".
colorIndicatorAriaLabel: __( '(current text color: %s)' ),
},
] }
>
<ContrastChecker
{ ...{
isLargeText: true,
textColor: textColor.value,
backgroundColor: backgroundColor.value,
fallbackBackgroundColor,
fallbackTextColor,
} }
/>
</PanelColorSettings>
</InspectorControls>
</span>
{ isSelected && (
Expand All @@ -141,4 +162,7 @@ class ButtonEdit extends Component {
}
}

export default withColors( 'backgroundColor', { textColor: 'color' } )( ButtonEdit );
export default compose( [
withColors( 'backgroundColor', { textColor: 'color' } ),
FallbackStyles,
] )( ButtonEdit );
54 changes: 29 additions & 25 deletions core-blocks/button/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`core/button block edit matches snapshot 1`] = `
<span
class="wp-block-button"
>
<div
class="editor-rich-text"
<div>
<span
class="wp-block-button"
>
<div>
<div
class="editor-rich-text"
>
<div>
<div
class="components-autocomplete"
>
<span
aria-autocomplete="list"
aria-expanded="false"
aria-label="Add text…"
aria-multiline="true"
class="wp-block-button__link editor-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
role="textbox"
/>
<span
class="editor-rich-text__tinymce wp-block-button__link"
<div>
<div
class="components-autocomplete"
>
Add text…
</span>
<span
aria-autocomplete="list"
aria-expanded="false"
aria-label="Add text…"
aria-multiline="true"
class="wp-block-button__link editor-rich-text__tinymce"
contenteditable="true"
data-is-placeholder-visible="true"
role="textbox"
/>
<span
class="editor-rich-text__tinymce wp-block-button__link"
>
Add text…
</span>
</div>
</div>
</div>
</div>
</div>
</span>
</span>
</div>
`;
53 changes: 31 additions & 22 deletions core-blocks/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import {
withColors,
AlignmentToolbar,
BlockControls,
ContrastChecker,
InspectorControls,
PanelColor,
PanelColorSettings,
RichText,
ContrastChecker,
} from '@wordpress/editor';
import { createBlock, getPhrasingContentSchema } from '@wordpress/blocks';
import { compose } from '@wordpress/compose';
Expand Down Expand Up @@ -232,27 +232,36 @@ class ParagraphBlock extends Component {
help={ this.getDropCapHelp }
/>
</PanelBody>
<PanelColor
colorValue={ backgroundColor.value }
initialOpen={ false }
title={ __( 'Background Color' ) }
onChange={ setBackgroundColor }
/>
<PanelColor
colorValue={ textColor.value }
<PanelColorSettings
title={ __( 'Color Settings' ) }
initialOpen={ false }
title={ __( 'Text Color' ) }
onChange={ setTextColor }
/>
<ContrastChecker
textColor={ textColor.value }
backgroundColor={ backgroundColor.value }
{ ...{
fallbackBackgroundColor,
fallbackTextColor,
} }
isLargeText={ fontSize >= 18 }
/>
colorSettings={ [
{
value: backgroundColor.value,
onChange: setBackgroundColor,
label: __( 'Background Color' ),
// translators: %s: The name of the color e.g: "vivid red" or color hex code if name is not available e.g: "#f00".
colorIndicatorAriaLabel: __( '(current background color: %s)' ),
},
{
value: textColor.value,
onChange: setTextColor,
label: __( 'Text Color' ),
// translators: %s: The name of the color e.g: "vivid red" or color hex code if name is not available e.g: "#f00".
colorIndicatorAriaLabel: __( '(current text color: %s)' ),
},
] }
>
<ContrastChecker
{ ...{
isLargeText: fontSize >= 18,
textColor: textColor.value,
backgroundColor: backgroundColor.value,
fallbackTextColor,
fallbackBackgroundColor,
} }
/>
</PanelColorSettings>
</InspectorControls>
<RichText
tagName="p"
Expand Down
28 changes: 28 additions & 0 deletions editor/components/color-indicator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* WordPress dependencies
*/
import {
ColorIndicator,
} from '@wordpress/components';
import { sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import withColorContext from '../color-palette/with-color-context';
import { getColorName } from '../colors';

export default withColorContext( ( { colors, colorValue, ariaLabel } ) => {
if ( ! colorValue ) {
return null;
}

const colorName = getColorName( colors, colorValue );

return (
<ColorIndicator
colorValue={ colorValue }
ariaLabel={ sprintf( ariaLabel, colorName || colorValue ) }
/>
);
} );
39 changes: 39 additions & 0 deletions editor/components/color-palette/control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* External dependencies
*/
import { pick } from 'lodash';

/**
* WordPress dependencies
*/
import { BaseControl } from '@wordpress/components';

/**
* Internal dependencies
*/
import './control.scss';
import ColorPalette from './';
import TextWithColorIndicators from '../text-with-color-indicators';

const ColorPaletteControl = ( { label, ...props } ) => {
const labelElement = (
<TextWithColorIndicators
colorSettings={ pick( props, [ 'value', 'colorIndicatorAriaLabel' ] ) }
>
{ label }
</TextWithColorIndicators>
);

return (
<BaseControl
className="editor-color-palette-control"
label={ labelElement }>
<ColorPalette
className="editor-color-palette-control__color-palette"
{ ...pick( props, [ 'value', 'onChange' ] ) }
/>
</BaseControl>
);
};

export default ColorPaletteControl;
4 changes: 4 additions & 0 deletions editor/components/color-palette/control.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.editor-color-palette-control__color-palette {
margin-top: .6rem;
margin-bottom: 1.4rem;
}
1 change: 1 addition & 0 deletions editor/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { default as InnerBlocks } from './inner-blocks';
export { default as InspectorAdvancedControls } from './inspector-advanced-controls';
export { default as InspectorControls } from './inspector-controls';
export { default as PanelColor } from './panel-color';
export { default as PanelColorSettings } from './panel-color-settings';
export { default as PlainText } from './plain-text';
export { default as RichText } from './rich-text';
export { default as RichTextProvider } from './rich-text/provider';
Expand Down
52 changes: 52 additions & 0 deletions editor/components/panel-color-settings/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* External dependencies
*/
import { omit } from 'lodash';

/**
* WordPress dependencies
*/
import {
PanelBody,
} from '@wordpress/components';
import { ifCondition, compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import './style.scss';
import ColorPaletteControl from '../color-palette/control';
import TextWithColorIndicators from '../text-with-color-indicators';
import withColorContext from '../color-palette/with-color-context';

export function PanelColorSettings( { title, colorSettings, children, ...props } ) {
const className = 'editor-panel-color-settings';

const titleElement = (
<TextWithColorIndicators
className={ `${ className }__panel-title` }
colorSettings={ colorSettings }
>
{ title }
</TextWithColorIndicators>
);

return (
<PanelBody
className={ className }
title={ titleElement }
{ ...omit( props, 'colors' ) }
>
{ colorSettings.map( ( settings, index ) => (
<ColorPaletteControl key={ index } { ...settings } />
) ) }

{ children }
</PanelBody>
);
}

export default compose( [
withColorContext,
ifCondition( ( { hasColorsToChoose } ) => hasColorsToChoose ),
] )( PanelColorSettings );
Loading

0 comments on commit 32aa85d

Please sign in to comment.