Skip to content

Commit

Permalink
Update packages/block-editor/src/components/colors/use-colors.js (#18147
Browse files Browse the repository at this point in the history
)

Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com> (+2 squashed commits)
Squashed commits:
[36484b4d3f] Update packages/block-editor/src/components/colors/use-colors.js

Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>
[9c4c769] Fix: solve some issues in useColors hook
  • Loading branch information
jorgefilipecosta authored and hypest committed Nov 4, 2019
1 parent ab93a67 commit f1d5fbe
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions packages/block-editor/src/components/colors/use-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
* External dependencies
*/
import memoize from 'memize';
import { kebabCase, camelCase, startCase } from 'lodash';
import classnames from 'classnames';
import {
camelCase,
kebabCase,
map,
startCase,
} from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -35,17 +41,17 @@ const ColorPanel = ( {
<PanelColorSettings
title={ title }
initialOpen={ false }
colorSettings={ colorSettings }
colorSettings={ Object.values( colorSettings ) }
{ ...colorPanelProps }
>
{ contrastCheckerProps &&
components.map( ( Component, index ) => (
map( components, ( ( Component, key ) => (
<ContrastChecker
key={ Component.displayName }
textColor={ colorSettings[ index ].value }
key={ key }
textColor={ colorSettings[ key ].value }
{ ...contrastCheckerProps }
/>
) ) }
) ) ) }
{ typeof panelChildren === 'function' ?
panelChildren( components ) :
panelChildren }
Expand Down Expand Up @@ -89,24 +95,35 @@ export default function __experimentalUseColors(
const createComponent = useMemo(
() =>
memoize(
( property, color, colorValue, customColor ) => ( { children } ) =>
( name, property, className, color, colorValue, customColor ) => ( {
children,
className: componentClassName = '',
style: componentStyle = {},
} ) =>
// Clone children, setting the style property from the color configuration,
// if not already set explicitly through props.
Children.map( children, ( child ) => {
let className = child.props.className;
let style = child.props.style;
let colorStyle = {};
if ( color ) {
className = `${ child.props.className } has-${ kebabCase(
color
) }-${ kebabCase( property ) }`;
style = { [ property ]: colorValue, ...child.props.style };
colorStyle = { [ property ]: colorValue };
} else if ( customColor ) {
className = `${ child.props.className } has-${ kebabCase( property ) }`;
style = { [ property ]: customColor, ...child.props.style };
colorStyle = { [ property ]: customColor };
}

return cloneElement( child, {
className,
style,
className: classnames(
componentClassName,
child.props.className,
{
[ `has-${ kebabCase( color ) }-${ kebabCase( property ) }` ]: color,
[ className || `has-${ kebabCase( name ) }` ]: color || customColor,
}
),
style: {
...colorStyle,
...componentStyle,
...( child.props.style || {} ),
},
} );
} ),
{ maxSize: colorConfigs.length }
Expand Down Expand Up @@ -135,7 +152,7 @@ export default function __experimentalUseColors(
);

return useMemo( () => {
const colorSettings = [];
const colorSettings = {};

const components = colorConfigs.reduce( ( acc, colorConfig ) => {
if ( typeof colorConfig === 'string' ) {
Expand All @@ -144,6 +161,7 @@ export default function __experimentalUseColors(
const {
name, // E.g. 'backgroundColor'.
property = name, // E.g. 'backgroundColor'.
className,

panelLabel = startCase( name ), // E.g. 'Background Color'.
componentName = panelLabel.replace( /\s/g, '' ), // E.g. 'BackgroundColor'.
Expand All @@ -159,7 +177,9 @@ export default function __experimentalUseColors(
// when they are used as props for other components.
const _color = colors.find( ( __color ) => __color.slug === color );
acc[ componentName ] = createComponent(
name,
property,
className,
color,
_color && _color.color,
attributes[ camelCase( `custom ${ name }` ) ]
Expand All @@ -168,21 +188,20 @@ export default function __experimentalUseColors(
acc[ componentName ].color = color;
acc[ componentName ].setColor = createSetColor( name, colors );

const newSettingIndex =
colorSettings.push( {
value: _color ?
_color.color :
attributes[ camelCase( `custom ${ name }` ) ],
onChange: acc[ componentName ].setColor,
label: panelLabel,
colors,
} ) - 1;
colorSettings[ componentName ] = {
value: _color ?
_color.color :
attributes[ camelCase( `custom ${ name }` ) ],
onChange: acc[ componentName ].setColor,
label: panelLabel,
colors,
};
// These settings will be spread over the `colors` in
// `colorPanelProps`, so we need to unset the key here,
// if not set to an actual value, to avoid overwriting
// an actual value in `colorPanelProps`.
if ( ! colors ) {
delete colorSettings[ newSettingIndex ].colors;
delete colorSettings[ componentName ].colors;
}

return acc;
Expand All @@ -193,7 +212,7 @@ export default function __experimentalUseColors(
colorSettings,
colorPanelProps,
contrastCheckerProps,
components: Object.values( components ),
components,
panelChildren,
};
return {
Expand Down

0 comments on commit f1d5fbe

Please sign in to comment.