From 3d10db5269ccab1f7717a60bc65ab7c9fc823767 Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 12 Mar 2018 15:39:04 +0000 Subject: [PATCH] Hide color pickers in paragraph and button if no colors are available. If the theme sets the colors palette to empty and disable custom colors, the color picker mechanism did not work correctly. It just showed a non-functional back color to choose from. Now we hide the color picker mechanism as in that case it is not possible to choose colors. --- blocks/color-palette/index.js | 84 +------------------ blocks/index.js | 1 + blocks/library/button/index.js | 37 ++++---- blocks/library/paragraph/index.js | 25 +++--- blocks/panel-color/index.js | 32 +++++++ blocks/with-color-context/index.js | 32 +++++++ components/color-palette/index.js | 79 +++++++++++++++++ .../color-palette/style.scss | 18 ++-- .../test/__snapshots__/index.js.snap | 44 +++++----- .../color-palette/test/index.js | 6 +- components/index.js | 1 + 11 files changed, 211 insertions(+), 148 deletions(-) create mode 100644 blocks/panel-color/index.js create mode 100644 blocks/with-color-context/index.js create mode 100644 components/color-palette/index.js rename {blocks => components}/color-palette/style.scss (85%) rename {blocks => components}/color-palette/test/__snapshots__/index.js.snap (68%) rename {blocks => components}/color-palette/test/index.js (93%) diff --git a/blocks/color-palette/index.js b/blocks/color-palette/index.js index 22f6cdc8eabd3..0192c93fe266d 100644 --- a/blocks/color-palette/index.js +++ b/blocks/color-palette/index.js @@ -1,89 +1,11 @@ -/** - * External dependencies - */ -import classnames from 'classnames'; -import { ChromePicker } from 'react-color'; -import { map } from 'lodash'; - /** * WordPress dependencies */ -import { Dropdown } from '@wordpress/components'; -import { __, sprintf } from '@wordpress/i18n'; +import { ColorPalette } from '@wordpress/components'; /** * Internal dependencies */ -import './style.scss'; -import { withEditorSettings } from '../editor-settings'; - -export function ColorPalette( { colors, disableCustomColors = false, value, onChange } ) { - function applyOrUnset( color ) { - return () => onChange( value === color ? undefined : color ); - } - - return ( -
- { map( colors, ( color ) => { - const style = { color: color }; - const className = classnames( 'blocks-color-palette__item', { 'is-active': value === color } ); - - return ( -
-
- ); - } ) } - - { ! disableCustomColors && - ( - - ) } - renderContent={ () => ( - onChange( color.hex ) } - style={ { width: '100%' } } - disableAlpha - /> - ) } - /> - } - - -
- ); -} +import withColorContext from '../with-color-context'; -export default withEditorSettings( - ( settings, props ) => ( { - colors: props.colors || settings.colors, - disableCustomColors: props.disableCustomColors !== undefined ? - props.disableCustomColors : - settings.disableCustomColors, - } ) -)( ColorPalette ); +export default withColorContext( ColorPalette ); diff --git a/blocks/index.js b/blocks/index.js index e92c66d0a7281..8f07a31527eea 100644 --- a/blocks/index.js +++ b/blocks/index.js @@ -33,3 +33,4 @@ export { default as RichTextProvider } from './rich-text/provider'; export { default as UrlInput } from './url-input'; export { default as UrlInputButton } from './url-input/button'; export { default as EditorSettings, withEditorSettings } from './editor-settings'; +export { default as withColorContext } from './with-color-context'; diff --git a/blocks/library/button/index.js b/blocks/library/button/index.js index c35f362b28402..47af7bd66fa34 100644 --- a/blocks/library/button/index.js +++ b/blocks/library/button/index.js @@ -7,7 +7,6 @@ import { Dashicon, IconButton, PanelBody, - PanelColor, ToggleControl, withFallbackStyles, } from '@wordpress/components'; @@ -21,8 +20,8 @@ import RichText from '../../rich-text'; import UrlInput from '../../url-input'; import BlockControls from '../../block-controls'; import BlockAlignmentToolbar from '../../block-alignment-toolbar'; -import ColorPalette from '../../color-palette'; import ContrastChecker from '../../contrast-checker'; +import PanelColor from '../../panel-color'; import InspectorControls from '../../inspector-controls'; const { getComputedStyle } = window; @@ -109,24 +108,24 @@ class ButtonBlock extends Component { checked={ !! clear } onChange={ this.toggleClear } /> - - setAttributes( { color: colorValue } ) } - /> - - - setAttributes( { textColor: colorValue } ) } + setAttributes( { color: colorValue } ) } + /> + setAttributes( { textColor: colorValue } ) } + value={ textColor } + /> + { this.nodeRef && + - - { this.nodeRef && } + } } diff --git a/blocks/library/paragraph/index.js b/blocks/library/paragraph/index.js index f776299fda0bd..1ac49362a8c16 100644 --- a/blocks/library/paragraph/index.js +++ b/blocks/library/paragraph/index.js @@ -16,7 +16,6 @@ import { } from '@wordpress/element'; import { PanelBody, - PanelColor, RangeControl, ToggleControl, Button, @@ -37,8 +36,8 @@ import BlockAlignmentToolbar from '../../block-alignment-toolbar'; import BlockControls from '../../block-controls'; import RichText from '../../rich-text'; import InspectorControls from '../../inspector-controls'; -import ColorPalette from '../../color-palette'; import ContrastChecker from '../../contrast-checker'; +import PanelColor from '../../panel-color'; const { getComputedStyle } = window; @@ -198,18 +197,16 @@ class ParagraphBlock extends Component { onChange={ this.toggleDropCap } /> - - setAttributes( { backgroundColor: colorValue } ) } - /> - - - setAttributes( { textColor: colorValue } ) } - /> - + setAttributes( { backgroundColor: colorValue } ) } + /> + setAttributes( { textColor: colorValue } ) } + value={ textColor } + /> + + + ); +} + +export default compose( [ + withColorContext, + ifCondition( ( { hasColorsToChoose } ) => hasColorsToChoose ), +] )( PanelColor ); diff --git a/blocks/with-color-context/index.js b/blocks/with-color-context/index.js new file mode 100644 index 0000000000000..e7107162d0ba5 --- /dev/null +++ b/blocks/with-color-context/index.js @@ -0,0 +1,32 @@ +/** + * External dependencies + */ +import { isEmpty } from 'lodash'; + +/** + * WordPress dependencies + */ +import { deprecated } from '@wordpress/utils'; + +/** + * Internal dependencies + */ +import { withEditorSettings } from '../editor-settings'; + +export default withEditorSettings( + ( settings, ownProps ) => { + if ( ownProps.colors || ownProps.disableCustomColors ) { + deprecated( 'Passing props "colors" or "disableCustomColors" to @blocks/PanelColor or @blocks/ColorPalette', { + version: '2.9', + alternative: 'remove the props and rely on the editor settings or use @wordpress/PanelColor and @wordpress/ColorPalette', + } ); + } + const colors = ownProps.colors || settings.colors; + const disableCustomColors = ownProps.disableCustomColors || settings.disableCustomColors; + return { + colors, + disableCustomColors, + hasColorsToChoose: ! isEmpty( colors ) || ! disableCustomColors, + }; + } +); diff --git a/components/color-palette/index.js b/components/color-palette/index.js new file mode 100644 index 0000000000000..67133c980fffb --- /dev/null +++ b/components/color-palette/index.js @@ -0,0 +1,79 @@ +/** + * External dependencies + */ +import classnames from 'classnames'; +import { ChromePicker } from 'react-color'; +import { map } from 'lodash'; + +/** + * WordPress dependencies + */ +import { __, sprintf } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import './style.scss'; +import Dropdown from '../dropdown'; + +export default function ColorPalette( { colors, disableCustomColors = false, value, onChange } ) { + function applyOrUnset( color ) { + return () => onChange( value === color ? undefined : color ); + } + + return ( +
+ { map( colors, ( color ) => { + const style = { color: color }; + const className = classnames( 'components-color-palette__item', { 'is-active': value === color } ); + + return ( +
+
+ ); + } ) } + + { ! disableCustomColors && + ( + + ) } + renderContent={ () => ( + onChange( color.hex ) } + style={ { width: '100%' } } + disableAlpha + /> + ) } + /> + } + + +
+ ); +} diff --git a/blocks/color-palette/style.scss b/components/color-palette/style.scss similarity index 85% rename from blocks/color-palette/style.scss rename to components/color-palette/style.scss index adaf230671865..d895f7f7d7ec7 100644 --- a/blocks/color-palette/style.scss +++ b/components/color-palette/style.scss @@ -1,16 +1,16 @@ $color-palette-circle-size: 28px; $color-palette-circle-spacing: 14px; -.blocks-color-palette { +.components-color-palette { margin-right: -14px; - .blocks-color-palette__clear { + .components-color-palette__clear { float: right; margin-right: 20px; } } -.blocks-color-palette__item-wrapper { +.components-color-palette__item-wrapper { display: inline-block; height: $color-palette-circle-size; width: $color-palette-circle-size; @@ -30,7 +30,7 @@ $color-palette-circle-spacing: 14px; } } -.blocks-color-palette__item { +.components-color-palette__item { display: inline-block; vertical-align: top; height: 100%; @@ -72,12 +72,12 @@ $color-palette-circle-spacing: 14px; } } -.blocks-color-palette__clear-color .blocks-color-palette__item { +.components-color-palette__clear-color .components-color-palette__item { color: $white; background: $white; } -.blocks-color-palette__clear-color-line { +.components-color-palette__clear-color-line { display: block; position: absolute; border: 2px solid $alert-red; @@ -101,12 +101,12 @@ $color-palette-circle-spacing: 14px; } } -.blocks-color-palette__custom-color .blocks-color-palette__item { +.components-color-palette__custom-color .components-color-palette__item { position: relative; box-shadow: none; } -.blocks-color-palette__custom-color .blocks-color-palette__custom-color-gradient { +.components-color-palette__custom-color .components-color-palette__custom-color-gradient { display: block; width: 100%; height: 100%; @@ -117,7 +117,7 @@ $color-palette-circle-spacing: 14px; overflow: hidden; } -.blocks-color-palette__custom-color .blocks-color-palette__custom-color-gradient:before { +.components-color-palette__custom-color .components-color-palette__custom-color-gradient:before { box-sizing: border-box; content: ''; filter: blur( 6px ) saturate( 0.7 ) brightness( 1.1 ); diff --git a/blocks/color-palette/test/__snapshots__/index.js.snap b/components/color-palette/test/__snapshots__/index.js.snap similarity index 68% rename from blocks/color-palette/test/__snapshots__/index.js.snap rename to components/color-palette/test/__snapshots__/index.js.snap index 2111c8b5f2f07..871d03c853fa1 100644 --- a/blocks/color-palette/test/__snapshots__/index.js.snap +++ b/components/color-palette/test/__snapshots__/index.js.snap @@ -44,20 +44,20 @@ exports[`ColorPalette Dropdown .renderToggle should render dropdown content 1`] `; exports[`ColorPalette Dropdown should render it correctly 1`] = ` @@ -65,16 +65,16 @@ exports[`ColorPalette Dropdown should render it correctly 1`] = ` exports[`ColorPalette should allow disabling custom color picker 1`] = `