Skip to content

Commit

Permalink
Combine color palettes into a single settings panel for Button and Pa…
Browse files Browse the repository at this point in the history
…ragraph blocks (#7924)

Combines ColorPalettes into a single panel for Button and Paragraph blocks.

Changes:
* Extracts ColorIndicator into a new component from the PanelColor component
* Introduces a new editor component ColorPaletteControl that wraps ColorPalette in a BaseControl and displays a ColorIndicator next to the control's label
* Introduces a new editor component PanelColorSettings that accepts an array of colorSettings and renders a ColorPaletteControl per colorSetting. Also displays ColorIndicators on the title for each colorSetting
* Uses ColorPaletteControl for both Button and Paragraph blocks.
  • Loading branch information
talldan authored Jul 27, 2018
1 parent b3230f0 commit 288fe78
Show file tree
Hide file tree
Showing 21 changed files with 430 additions and 100 deletions.
68 changes: 44 additions & 24 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 @@ -19,7 +23,7 @@ import {
ContrastChecker,
InspectorControls,
withColors,
PanelColor,
PanelColorSettings,
} from '@wordpress/editor';

/**
Expand All @@ -29,15 +33,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 All @@ -60,6 +66,8 @@ class ButtonEdit extends Component {
textColor,
setBackgroundColor,
setTextColor,
fallbackBackgroundColor,
fallbackTextColor,
setAttributes,
isSelected,
className,
Expand Down Expand Up @@ -95,22 +103,31 @@ 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' ),
},
{
value: textColor.value,
onChange: setTextColor,
label: __( 'Text Color' ),
},
] }
>
<ContrastChecker
{ ...{
isLargeText: true,
textColor: textColor.value,
backgroundColor: backgroundColor.value,
fallbackBackgroundColor,
fallbackTextColor,
} }
/>
</PanelColorSettings>
</InspectorControls>
</span>
{ isSelected && (
Expand All @@ -130,4 +147,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>
`;
49 changes: 27 additions & 22 deletions core-blocks/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import {
withColors,
AlignmentToolbar,
BlockControls,
ContrastChecker,
InspectorControls,
PanelColor,
PanelColorSettings,
RichText,
ContrastChecker,
} from '@wordpress/editor';
import {
createBlock,
Expand Down Expand Up @@ -235,27 +235,32 @@ 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 }
{ ...{
fontSize,
fallbackBackgroundColor,
fallbackTextColor,
} }
/>
colorSettings={ [
{
value: backgroundColor.value,
onChange: setBackgroundColor,
label: __( 'Background Color' ),
},
{
value: textColor.value,
onChange: setTextColor,
label: __( 'Text Color' ),
},
] }
>
<ContrastChecker
{ ...{
fontSize,
textColor: textColor.value,
backgroundColor: backgroundColor.value,
fallbackTextColor,
fallbackBackgroundColor,
} }
/>
</PanelColorSettings>
</InspectorControls>
<RichText
tagName="p"
Expand Down
48 changes: 48 additions & 0 deletions editor/components/color-palette/control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* WordPress dependencies
*/
import { BaseControl, ColorIndicator } from '@wordpress/components';
import { Fragment } from '@wordpress/element';
import { sprintf, __ } from '@wordpress/i18n';

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

// translators: first %s: The type of color (e.g. background color), second %s: the color name or value (e.g. red or #ff0000)
const colorIndicatorAriaLabel = __( '(current %s: %s)' );

export function ColorPaletteControl( { label, value, onChange, colors } ) {
const colorName = getColorName( colors, value );
const ariaLabel = sprintf( colorIndicatorAriaLabel, label.toLowerCase(), colorName || value );

const labelElement = (
<Fragment>
{ label }
{ value && (
<ColorIndicator
colorValue={ value }
aria-label={ ariaLabel }
/>
) }
</Fragment>
);

return (
<BaseControl
className="editor-color-palette-control"
label={ labelElement }>
<ColorPalette
className="editor-color-palette-control__color-palette"
value={ value }
onChange={ onChange }
/>
</BaseControl>
);
}

export default withColorContext( 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;
}
22 changes: 22 additions & 0 deletions editor/components/color-palette/test/__snapshots__/control.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ColorPaletteControl matches the snapshot 1`] = `
<BaseControl
className="editor-color-palette-control"
label={
<React.Fragment>
Test Color
<ColorIndicator
aria-label="(current test color: red)"
colorValue="#f00"
/>
</React.Fragment>
}
>
<WithColorContext(ColorPalette)
className="editor-color-palette-control__color-palette"
onChange={[Function]}
value="#f00"
/>
</BaseControl>
`;
25 changes: 25 additions & 0 deletions editor/components/color-palette/test/control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';
import { noop } from 'lodash';

/**
* Internal dependencies
*/
import { ColorPaletteControl } from '../control';

describe( 'ColorPaletteControl', () => {
it( 'matches the snapshot', () => {
const wrapper = shallow(
<ColorPaletteControl
label="Test Color"
value="#f00"
colors={ [ { color: '#f00', name: 'red' } ] }
onChange={ noop }
/>
);

expect( wrapper ).toMatchSnapshot();
} );
} );
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
Loading

0 comments on commit 288fe78

Please sign in to comment.