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

Use immutableSet utility to set style properties #49365

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 88 additions & 138 deletions packages/block-editor/src/components/global-styles/color-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { __ } from '@wordpress/i18n';
import ColorGradientControl from '../colors-gradients/control';
import { useColorsPerOrigin, useGradientsPerOrigin } from './hooks';
import { getValueFromVariable } from './utils';
import { immutableSet } from '../../utils/object';

export function useHasColorPanel( settings ) {
const hasTextPanel = useHasTextPanel( settings );
Expand Down Expand Up @@ -328,10 +329,13 @@ export default function ColorPanel( {
const userTextColor = decodeValue( value?.color?.text );
const hasTextColor = () => !! userTextColor;
const setTextColor = ( newColor ) => {
onChange( {
...value,
color: { ...value?.color, text: encodeColorValue( newColor ) },
} );
onChange(
immutableSet(
value,
[ 'color', 'text' ],
encodeColorValue( newColor )
)
);
};
const resetTextColor = () => setTextColor( undefined );

Expand All @@ -343,34 +347,31 @@ export default function ColorPanel( {
const userGradient = decodeValue( value?.color?.gradient );
const hasBackground = () => !! userBackgroundColor || !! userGradient;
const setBackgroundColor = ( newColor ) => {
onChange( {
...value,
color: {
...value?.color,
background: encodeColorValue( newColor ),
gradient: undefined,
},
} );
const newValue = immutableSet(
value,
[ 'color', 'background' ],
encodeColorValue( newColor )
);
newValue.color.gradient = undefined;
onChange( newValue );
};
const setGradient = ( newGradient ) => {
onChange( {
...value,
color: {
...value?.color,
background: undefined,
gradient: encodeGradientValue( newGradient ),
},
} );
const newValue = immutableSet(
value,
[ 'color', 'gradient' ],
encodeGradientValue( newGradient )
);
newValue.color.background = undefined;
onChange( newValue );
};
const resetBackground = () => {
onChange( {
...value,
color: {
...value?.color,
background: undefined,
gradient: undefined,
},
} );
const newValue = immutableSet(
value,
[ 'color', 'background' ],
undefined
);
newValue.color.gradient = undefined;
onChange( newValue );
};

// Links
Expand All @@ -380,19 +381,13 @@ export default function ColorPanel( {
);
const userLinkColor = decodeValue( value?.elements?.link?.color?.text );
const setLinkColor = ( newColor ) => {
onChange( {
...value,
elements: {
...value?.elements,
link: {
...value?.elements?.link,
color: {
...value?.elements?.link?.color,
text: encodeColorValue( newColor ),
},
},
},
} );
onChange(
immutableSet(
value,
[ 'elements', 'link', 'color', 'text' ],
encodeColorValue( newColor )
)
);
};
const hoverLinkColor = decodeValue(
inheritedValue?.elements?.link?.[ ':hover' ]?.color?.text
Expand All @@ -401,46 +396,28 @@ export default function ColorPanel( {
value?.elements?.link?.[ ':hover' ]?.color?.text
);
const setHoverLinkColor = ( newColor ) => {
onChange( {
...value,
elements: {
...value?.color?.elements,
link: {
...value?.elements?.link,
':hover': {
...value?.elements?.link?.[ ':hover' ],
color: {
...value?.elements?.link?.[ ':hover' ]?.color,
text: encodeColorValue( newColor ),
},
},
},
},
} );
onChange(
immutableSet(
value,
[ 'elements', 'link', ':hover', 'color', 'text' ],
encodeColorValue( newColor )
)
);
};
const hasLink = () => !! userLinkColor || !! userHoverLinkColor;
const resetLink = () =>
onChange( {
...value,
elements: {
...value?.color?.elements,
link: {
...value?.color?.elements?.link,
color: {
...value?.color?.elements?.link?.color,
text: undefined,
},
':hover': {
...value?.color?.elements?.link?.[ ':hover' ],
color: {
...value?.color?.elements?.link?.[ ':hover' ]
?.color,
text: undefined,
},
},
},
},
} );
const resetLink = () => {
let newValue = immutableSet(
value,
[ 'elements', 'link', ':hover', 'color', 'text' ],
undefined
);
newValue = immutableSet(
newValue,
[ 'elements', 'link', 'color', 'text' ],
undefined
);
onChange( newValue );
};

// Elements
const elements = [
Expand Down Expand Up @@ -614,69 +591,42 @@ export default function ColorPanel( {
elementGradientUserColor
);
const resetElement = () => {
onChange( {
...value,
elements: {
...value?.elements,
[ name ]: {
...value?.elements?.[ name ],
color: {
...value?.elements?.[ name ]?.color,
background: undefined,
gradient: undefined,
text: undefined,
},
},
},
} );
const newValue = immutableSet(
value,
[ 'elements', name, 'color', 'background' ],
undefined
);
newValue.elements[ name ].color.gradient = undefined;
newValue.elements[ name ].color.text = undefined;
onChange( newValue );
};

const setElementTextColor = ( newValue ) => {
onChange( {
...value,
elements: {
...value?.elements,
[ name ]: {
...value?.elements?.[ name ],
color: {
...value?.elements?.[ name ]?.color,
text: encodeColorValue( newValue ),
},
},
},
} );
const setElementTextColor = ( newTextColor ) => {
onChange(
immutableSet(
value,
[ 'elements', name, 'color', 'text' ],
encodeColorValue( newTextColor )
)
);
};
const setElementBackgroundColor = ( newValue ) => {
onChange( {
...value,
elements: {
...value?.elements,
[ name ]: {
...value?.elements?.[ name ],
color: {
...value?.elements?.[ name ]?.color,
background: encodeColorValue( newValue ),
gradient: undefined,
},
},
},
} );
const setElementBackgroundColor = ( newBackgroundColor ) => {
const newValue = immutableSet(
value,
[ 'elements', name, 'color', 'background' ],
encodeColorValue( newBackgroundColor )
);
newValue.elements[ name ].color.gradient = undefined;
onChange( newValue );
};
const setElementGradient = ( newValue ) => {
onChange( {
...value,
elements: {
...value?.elements,
[ name ]: {
...value?.elements?.[ name ],
color: {
...value?.elements?.[ name ]?.color,
gradient: encodeGradientValue( newValue ),
background: undefined,
},
},
},
} );
const setElementGradient = ( newGradient ) => {
const newValue = immutableSet(
value,
[ 'elements', name, 'color', 'gradient' ],
encodeGradientValue( newGradient )
);
newValue.elements[ name ].color.background = undefined;
onChange( newValue );
};
const supportsTextColor = true;
// Background color is not supported for `caption`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import SpacingSizesControl from '../spacing-sizes-control';
import HeightControl from '../height-control';
import ChildLayoutControl from '../child-layout-control';
import { cleanEmptyObject } from '../../hooks/utils';
import { immutableSet } from '../../utils/object';

const AXIAL_SIDES = [ 'horizontal', 'vertical' ];

Expand Down Expand Up @@ -223,13 +224,9 @@ export default function DimensionsPanel( {
useHasContentSize( settings ) && includeLayoutControls;
const contentSizeValue = decodeValue( inheritedValue?.layout?.contentSize );
const setContentSizeValue = ( newValue ) => {
onChange( {
...value,
layout: {
...value?.layout,
contentSize: newValue,
},
} );
onChange(
immutableSet( value, [ 'layout', 'contentSize' ], newValue )
);
};
const hasUserSetContentSizeValue = () => !! value?.layout?.contentSize;
const resetContentSizeValue = () => setContentSizeValue( undefined );
Expand All @@ -239,13 +236,7 @@ export default function DimensionsPanel( {
useHasWideSize( settings ) && includeLayoutControls;
const wideSizeValue = decodeValue( inheritedValue?.layout?.wideSize );
const setWideSizeValue = ( newValue ) => {
onChange( {
...value,
layout: {
...value?.layout,
wideSize: newValue,
},
} );
onChange( immutableSet( value, [ 'layout', 'wideSize' ], newValue ) );
};
const hasUserSetWideSizeValue = () => !! value?.layout?.wideSize;
const resetWideSizeValue = () => setWideSizeValue( undefined );
Expand All @@ -262,13 +253,7 @@ export default function DimensionsPanel( {
paddingSides.some( ( side ) => AXIAL_SIDES.includes( side ) );
const setPaddingValues = ( newPaddingValues ) => {
const padding = filterValuesBySides( newPaddingValues, paddingSides );
onChange( {
...value,
spacing: {
...value?.spacing,
padding,
},
} );
onChange( immutableSet( value, [ 'spacing', 'padding' ], padding ) );
};
const hasPaddingValue = () =>
!! value?.spacing?.padding &&
Expand All @@ -288,13 +273,7 @@ export default function DimensionsPanel( {
marginSides.some( ( side ) => AXIAL_SIDES.includes( side ) );
const setMarginValues = ( newMarginValues ) => {
const margin = filterValuesBySides( newMarginValues, marginSides );
onChange( {
...value,
spacing: {
...value?.spacing,
margin,
},
} );
onChange( immutableSet( value, [ 'spacing', 'margin' ], margin ) );
};
const hasMarginValue = () =>
!! value?.spacing?.margin &&
Expand All @@ -312,13 +291,9 @@ export default function DimensionsPanel( {
const isAxialGap =
gapSides && gapSides.some( ( side ) => AXIAL_SIDES.includes( side ) );
const setGapValue = ( newGapValue ) => {
onChange( {
...value,
spacing: {
...value?.spacing,
blockGap: newGapValue,
},
} );
onChange(
immutableSet( value, [ 'spacing', 'blockGap' ], newGapValue )
);
};
const setGapValues = ( nextBoxGapValue ) => {
if ( ! nextBoxGapValue ) {
Expand All @@ -341,13 +316,9 @@ export default function DimensionsPanel( {
const showMinHeightControl = useHasMinHeight( settings );
const minHeightValue = decodeValue( inheritedValue?.dimensions?.minHeight );
const setMinHeightValue = ( newValue ) => {
onChange( {
...value,
dimensions: {
...value?.dimensions,
minHeight: newValue,
},
} );
onChange(
immutableSet( value, [ 'dimensions', 'minHeight' ], newValue )
);
};
const resetMinHeightValue = () => {
setMinHeightValue( undefined );
Expand Down
Loading