-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Editor: Implement new colors hook. (#16781)
* Block Editor: Implement new colors hook. * Block Library: Swap usage of the colors HOC with the colors hook in the heading edit component. * Use Colors: Add 'has-x-color' class names. * Use Colors: Avoid memory leaks by making caches limited in size, and tied to hook instances. * Use Colors: Support children and optional contrast checking in the color panel. * Use Colors: Expose colors panel without inspector slot/fill wrapper. * Use Colors: Mark hook as experimental. * Use Colors: Support custom colors. * Block Edit: Remove extra context values and use selectors/actions instead. * Heading: Remove unnecessary color class and set text color on save. * Use Colors: Add custom/preset color logic. * Use Colors: Fix panel bugs. * Heading Block: Detect actual background color for contrast checking. * Block Edit: Add new export to native file. * Use Colors: Change CSS "attribute" to "property".
- Loading branch information
Showing
8 changed files
with
270 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
packages/block-editor/src/components/colors/use-colors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import memoize from 'memize'; | ||
import { kebabCase, camelCase, startCase } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
useCallback, | ||
useMemo, | ||
Children, | ||
cloneElement, | ||
} from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PanelColorSettings from '../panel-color-settings'; | ||
import ContrastChecker from '../contrast-checker'; | ||
import InspectorControls from '../inspector-controls'; | ||
import { useBlockEditContext } from '../block-edit'; | ||
|
||
const ColorPanel = ( { | ||
title, | ||
colorSettings, | ||
colorPanelProps, | ||
contrastCheckerProps, | ||
components, | ||
panelChildren, | ||
} ) => ( | ||
<PanelColorSettings | ||
title={ title } | ||
initialOpen={ false } | ||
colorSettings={ colorSettings } | ||
{ ...colorPanelProps } | ||
> | ||
{ contrastCheckerProps && | ||
components.map( ( Component, index ) => ( | ||
<ContrastChecker | ||
key={ Component.displayName } | ||
textColor={ colorSettings[ index ].value } | ||
{ ...contrastCheckerProps } | ||
/> | ||
) ) } | ||
{ typeof panelChildren === 'function' ? | ||
panelChildren( components ) : | ||
panelChildren } | ||
</PanelColorSettings> | ||
); | ||
const InspectorControlsColorPanel = ( props ) => ( | ||
<InspectorControls> | ||
<ColorPanel { ...props } /> | ||
</InspectorControls> | ||
); | ||
|
||
export default function __experimentalUseColors( | ||
colorConfigs, | ||
{ | ||
panelTitle = __( 'Color Settings' ), | ||
colorPanelProps, | ||
contrastCheckerProps, | ||
panelChildren, | ||
} = { | ||
panelTitle: __( 'Color Settings' ), | ||
}, | ||
deps = [] | ||
) { | ||
const { clientId } = useBlockEditContext(); | ||
const { attributes, settingsColors } = useSelect( | ||
( select ) => { | ||
const { getBlockAttributes, getSettings } = select( 'core/block-editor' ); | ||
return { | ||
attributes: getBlockAttributes( clientId ), | ||
settingsColors: getSettings().colors, | ||
}; | ||
}, | ||
[ clientId ] | ||
); | ||
const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); | ||
const setAttributes = useCallback( | ||
( newAttributes ) => updateBlockAttributes( clientId, newAttributes ), | ||
[ updateBlockAttributes, clientId ] | ||
); | ||
|
||
const createComponent = useMemo( | ||
() => | ||
memoize( | ||
( property, color, colorValue, customColor ) => ( { children } ) => | ||
// 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; | ||
if ( color ) { | ||
className = `${ child.props.className } has-${ kebabCase( | ||
color | ||
) }-${ kebabCase( property ) }`; | ||
style = { [ property ]: colorValue, ...child.props.style }; | ||
} else if ( customColor ) { | ||
className = `${ child.props.className } has-${ kebabCase( property ) }`; | ||
style = { [ property ]: customColor, ...child.props.style }; | ||
} | ||
return cloneElement( child, { | ||
className, | ||
style, | ||
} ); | ||
} ), | ||
{ maxSize: colorConfigs.length } | ||
), | ||
[ colorConfigs.length ] | ||
); | ||
const createSetColor = useMemo( | ||
() => | ||
memoize( | ||
( name, colors ) => ( newColor ) => { | ||
const color = colors.find( ( _color ) => _color.color === newColor ); | ||
setAttributes( { | ||
[ color ? camelCase( `custom ${ name }` ) : name ]: undefined, | ||
} ); | ||
setAttributes( { | ||
[ color ? name : camelCase( `custom ${ name }` ) ]: color ? | ||
color.slug : | ||
newColor, | ||
} ); | ||
}, | ||
{ | ||
maxSize: colorConfigs.length, | ||
} | ||
), | ||
[ setAttributes, colorConfigs.length ] | ||
); | ||
|
||
return useMemo( () => { | ||
const colorSettings = []; | ||
|
||
const components = colorConfigs.reduce( ( acc, colorConfig ) => { | ||
if ( typeof colorConfig === 'string' ) { | ||
colorConfig = { name: colorConfig }; | ||
} | ||
const { | ||
name, // E.g. 'backgroundColor'. | ||
property = name, // E.g. 'backgroundColor'. | ||
|
||
panelLabel = startCase( name ), // E.g. 'Background Color'. | ||
componentName = panelLabel.replace( /\s/g, '' ), // E.g. 'BackgroundColor'. | ||
|
||
color = colorConfig.color, | ||
colors = settingsColors, | ||
} = { | ||
...colorConfig, | ||
color: attributes[ colorConfig.name ], | ||
}; | ||
|
||
// We memoize the non-primitives to avoid unnecessary updates | ||
// when they are used as props for other components. | ||
const _color = colors.find( ( __color ) => __color.slug === color ); | ||
acc[ componentName ] = createComponent( | ||
property, | ||
color, | ||
_color && _color.color, | ||
attributes[ camelCase( `custom ${ name }` ) ] | ||
); | ||
acc[ componentName ].displayName = componentName; | ||
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; | ||
// 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; | ||
} | ||
|
||
return acc; | ||
}, {} ); | ||
|
||
const wrappedColorPanelProps = { | ||
title: panelTitle, | ||
colorSettings, | ||
colorPanelProps, | ||
contrastCheckerProps, | ||
components: Object.values( components ), | ||
panelChildren, | ||
}; | ||
return { | ||
...components, | ||
ColorPanel: <ColorPanel { ...wrappedColorPanelProps } />, | ||
InspectorControlsColorPanel: ( | ||
<InspectorControlsColorPanel { ...wrappedColorPanelProps } /> | ||
), | ||
}; | ||
}, [ attributes, setAttributes, ...deps ] ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.