diff --git a/blocks/color-palette/index.js b/blocks/color-palette/index.js index 22f6cdc8eabd3a..0192c93fe266d7 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 e92c66d0a72816..8f07a31527eea6 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 c35f362b284020..47af7bd66fa349 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 f776299fda0bde..1ac49362a8c161 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 00000000000000..e7107162d0ba5f --- /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 00000000000000..67133c980fffb1 --- /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 adaf230671865e..d895f7f7d7ec77 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 2111c8b5f2f07a..871d03c853fa18 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`] = `