Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color hooks for the mobile implementation. #21257

Merged
merged 10 commits into from
Mar 31, 2020
Prev Previous commit
Next Next commit
Update use-color for native
  • Loading branch information
SergioEstevao committed Mar 30, 2020
commit 78043df35023b60cd0c50b8e6c15dafa80f2122c
91 changes: 91 additions & 0 deletions packages/block-editor/src/components/colors/color-panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* External dependencies
*/
import { map } from 'lodash';

/**
* Internal dependencies
*/
import PanelColorSettings from '../panel-color-settings';
import ContrastChecker from '../contrast-checker';

const resolveContrastCheckerColor = ( color, colorSettings, detectedColor ) => {
if ( typeof color === 'function' ) {
return color( colorSettings );
} else if ( color === true ) {
return detectedColor;
}
return color;
};

export default function ColorPanel( {
title,
colorSettings,
colorPanelProps,
contrastCheckers,
detectedBackgroundColor,
detectedColor,
panelChildren,
initialOpen,
} ) {
return (
<PanelColorSettings
title={ title }
initialOpen={ initialOpen }
colorSettings={ Object.values( colorSettings ) }
{ ...colorPanelProps }
>
{ contrastCheckers &&
( Array.isArray( contrastCheckers )
? contrastCheckers.map(
( { backgroundColor, textColor, ...rest } ) => {
backgroundColor = resolveContrastCheckerColor(
backgroundColor,
colorSettings,
detectedBackgroundColor
);
textColor = resolveContrastCheckerColor(
textColor,
colorSettings,
detectedColor
);
return (
<ContrastChecker
key={ `${ backgroundColor }-${ textColor }` }
backgroundColor={ backgroundColor }
textColor={ textColor }
{ ...rest }
/>
);
}
)
: map( colorSettings, ( { value } ) => {
let {
backgroundColor,
textColor,
} = contrastCheckers;
backgroundColor = resolveContrastCheckerColor(
backgroundColor || value,
colorSettings,
detectedBackgroundColor
);
textColor = resolveContrastCheckerColor(
textColor || value,
colorSettings,
detectedColor
);
return (
<ContrastChecker
{ ...contrastCheckers }
key={ `${ backgroundColor }-${ textColor }` }
backgroundColor={ backgroundColor }
textColor={ textColor }
/>
);
} ) ) }
{ typeof panelChildren === 'function'
? panelChildren( colorSettings )
: panelChildren }
</PanelColorSettings>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function ColorPanel() {
return null;
}
87 changes: 2 additions & 85 deletions packages/block-editor/src/components/colors/use-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
*/
import memoize from 'memize';
import classnames from 'classnames';
import {
map,
kebabCase,
camelCase,
castArray,
startCase,
isFunction,
} from 'lodash';
import { kebabCase, camelCase, castArray, startCase, isFunction } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -29,10 +22,9 @@ import {
/**
* Internal dependencies
*/
import PanelColorSettings from '../panel-color-settings';
import ContrastChecker from '../contrast-checker';
import InspectorControls from '../inspector-controls';
import { useBlockEditContext } from '../block-edit';
import ColorPanel from './color-panel';

/**
* Browser dependencies
Expand All @@ -46,81 +38,6 @@ const COMMON_COLOR_LABELS = {
backgroundColor: __( 'Background Color' ),
};

const resolveContrastCheckerColor = ( color, colorSettings, detectedColor ) => {
if ( typeof color === 'function' ) {
return color( colorSettings );
} else if ( color === true ) {
return detectedColor;
}
return color;
};

const ColorPanel = ( {
title,
colorSettings,
colorPanelProps,
contrastCheckers,
detectedBackgroundColor,
detectedColor,
panelChildren,
initialOpen,
} ) => (
<PanelColorSettings
title={ title }
initialOpen={ initialOpen }
colorSettings={ Object.values( colorSettings ) }
{ ...colorPanelProps }
>
{ contrastCheckers &&
( Array.isArray( contrastCheckers )
? contrastCheckers.map(
( { backgroundColor, textColor, ...rest } ) => {
backgroundColor = resolveContrastCheckerColor(
backgroundColor,
colorSettings,
detectedBackgroundColor
);
textColor = resolveContrastCheckerColor(
textColor,
colorSettings,
detectedColor
);
return (
<ContrastChecker
key={ `${ backgroundColor }-${ textColor }` }
backgroundColor={ backgroundColor }
textColor={ textColor }
{ ...rest }
/>
);
}
)
: map( colorSettings, ( { value } ) => {
let { backgroundColor, textColor } = contrastCheckers;
backgroundColor = resolveContrastCheckerColor(
backgroundColor || value,
colorSettings,
detectedBackgroundColor
);
textColor = resolveContrastCheckerColor(
textColor || value,
colorSettings,
detectedColor
);
return (
<ContrastChecker
{ ...contrastCheckers }
key={ `${ backgroundColor }-${ textColor }` }
backgroundColor={ backgroundColor }
textColor={ textColor }
/>
);
} ) ) }
{ typeof panelChildren === 'function'
? panelChildren( colorSettings )
: panelChildren }
</PanelColorSettings>
);
const InspectorControlsColorPanel = ( props ) => (
<InspectorControls>
<ColorPanel { ...props } />
Expand Down
10 changes: 0 additions & 10 deletions packages/block-editor/src/components/colors/use-colors.native.js

This file was deleted.