Skip to content

Commit

Permalink
Revert to access color props specifically
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Dec 3, 2021
1 parent 163a736 commit 6a825e6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
8 changes: 7 additions & 1 deletion packages/block-editor/src/components/colors/with-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const withCustomColorPalette = ( colorsArray ) =>
'withCustomColorPalette'
);

const EMPTY_OBJECT = {};

/**
* Higher order component factory for injecting the editor colors as the
* `colors` prop in the `withColors` HOC.
Expand All @@ -45,7 +47,11 @@ const withCustomColorPalette = ( colorsArray ) =>
const withEditorColorPalette = () =>
createHigherOrderComponent(
( WrappedComponent ) => ( props ) => {
const { palette: colorPerOrigin } = useSetting( 'color' ) || {};
// Some color settings have a special handling for deprecated flags in `useSetting`,
// so we can't unwrap them by doing const { ... } = useSetting('color')
// until https://github.com/WordPress/gutenberg/issues/37094 is fixed.
const colorPerOrigin =
useSetting( 'color.palette' ) || EMPTY_OBJECT;
const allColors = useMemo(
() => [
...( colorPerOrigin?.custom || [] ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ export function getGradientSlugByValue( gradients, value ) {
return gradient && gradient.slug;
}

const EMPTY_OBJECT = {};

export function __experimentalUseGradient( {
gradientAttribute = 'gradient',
customGradientAttribute = 'customGradient',
} = {} ) {
const { clientId } = useBlockEditContext();

const { gradients: gradientsPerOrigin } = useSetting( 'color' ) || {};
const gradientsPerOrigin = useSetting( 'color.gradients' ) || EMPTY_OBJECT;
const allGradients = useMemo(
() => [
...( gradientsPerOrigin?.custom || [] ),
Expand Down
22 changes: 11 additions & 11 deletions packages/block-editor/src/hooks/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import useSetting from '../components/use-setting';

export const COLOR_SUPPORT_KEY = 'color';

const EMPTY_OBJECT = {};

const hasColorSupport = ( blockType ) => {
const colorSupport = getBlockSupport( blockType, COLOR_SUPPORT_KEY );
return (
Expand Down Expand Up @@ -216,18 +218,16 @@ function immutableSet( object, path, value ) {
*/
export function ColorEdit( props ) {
const { name: blockName, attributes } = props;
const {
palette: solidsPerOrigin,
gradients: gradientsPerOrigin,
text: isTextEnabled,
background: isBackgroundEnabled,
link: isLinkEnabled,
} = useSetting( 'color' ) || {};

// The following color settings need to be accessed explicitly due to
// special handling for deprecated flags for these paths in `useSetting`.
// Some color settings have a special handling for deprecated flags in `useSetting`,
// so we can't unwrap them by doing const { ... } = useSetting('color')
// until https://github.com/WordPress/gutenberg/issues/37094 is fixed.
const solidsPerOrigin = useSetting( 'color.palette' ) || EMPTY_OBJECT;
const gradientsPerOrigin = useSetting( 'color.gradients' ) || EMPTY_OBJECT;
const areCustomSolidsEnabled = useSetting( 'color.custom' );
const areCustomGradientsEnabled = useSetting( 'color.customGradient' );
const isBackgroundEnabled = useSetting( 'color.background' );
const isLinkEnabled = useSetting( 'color.link' );
const isTextEnabled = useSetting( 'color.text' );

const solidsEnabled =
areCustomSolidsEnabled ||
Expand Down Expand Up @@ -444,7 +444,7 @@ export const withColorPaletteStyles = createHigherOrderComponent(
( BlockListBlock ) => ( props ) => {
const { name, attributes } = props;
const { backgroundColor, textColor } = attributes;
const { palette: solidsPerOrigin } = useSetting( 'color' ) || {};
const solidsPerOrigin = useSetting( 'color.palette' ) || EMPTY_OBJECT;
const colors = useMemo(
() => [
...( solidsPerOrigin?.custom || [] ),
Expand Down
10 changes: 8 additions & 2 deletions packages/block-editor/src/hooks/use-color-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export function getColorClassesAndStyles( attributes ) {
};
}

const EMPTY_OBJECT = {};

/**
* Determines the color related props for a block derived from its color block
* support attributes.
Expand All @@ -87,8 +89,12 @@ export function getColorClassesAndStyles( attributes ) {
export function useColorProps( attributes ) {
const { backgroundColor, textColor, gradient } = attributes;

const { palette: solidsPerOrigin, gradients: gradientsPerOrigin } =
useSetting( 'color' ) || {};
// Some color settings have a special handling for deprecated flags in `useSetting`,
// so we can't unwrap them by doing const { ... } = useSetting('color')
// until https://github.com/WordPress/gutenberg/issues/37094 is fixed.
const solidsPerOrigin = useSetting( 'color.palette' ) || EMPTY_OBJECT;
const gradientsPerOrigin = useSetting( 'color.gradients' ) || EMPTY_OBJECT;

const colors = useMemo(
() => [
...( solidsPerOrigin?.custom || [] ),
Expand Down

0 comments on commit 6a825e6

Please sign in to comment.