diff --git a/packages/block-editor/src/components/spacing-sizes-control/all-input-control.js b/packages/block-editor/src/components/spacing-sizes-control/all-input-control.js index d0243b90c56acb..8f9509349c2a5a 100644 --- a/packages/block-editor/src/components/spacing-sizes-control/all-input-control.js +++ b/packages/block-editor/src/components/spacing-sizes-control/all-input-control.js @@ -19,7 +19,7 @@ export default function AllInputControl( { } ) { const allValue = getAllRawValue( values ); const hasValues = isValuesDefined( values ); - const isMixed = hasValues && isValuesMixed( values ); + const isMixed = hasValues && isValuesMixed( values, sides ); const handleOnChange = ( next ) => { const nextValues = applyValueToSides( values, next, sides ); diff --git a/packages/block-editor/src/components/spacing-sizes-control/index.js b/packages/block-editor/src/components/spacing-sizes-control/index.js index 0653375a574b9e..ba379713b4bcc5 100644 --- a/packages/block-editor/src/components/spacing-sizes-control/index.js +++ b/packages/block-editor/src/components/spacing-sizes-control/index.js @@ -43,7 +43,7 @@ export default function SpacingSizesControl( { const hasOneSide = sides?.length === 1; const [ isLinked, setIsLinked ] = useState( - ! hasInitialValue || ! isValuesMixed( inputValues ) || hasOneSide + ! hasInitialValue || ! isValuesMixed( inputValues, sides ) || hasOneSide ); const toggleLinked = () => { diff --git a/packages/block-editor/src/components/spacing-sizes-control/utils.js b/packages/block-editor/src/components/spacing-sizes-control/utils.js index 12071e3c63dc45..3790dbe94862ae 100644 --- a/packages/block-editor/src/components/spacing-sizes-control/utils.js +++ b/packages/block-editor/src/components/spacing-sizes-control/utils.js @@ -159,13 +159,14 @@ export function getAllRawValue( values = {} ) { * Checks to determine if values are mixed. * * @param {Object} values Box values. + * @param {Array} sides Sides that values relate to. * * @return {boolean} Whether values are mixed. */ -export function isValuesMixed( values = {} ) { +export function isValuesMixed( values = {}, sides = ALL_SIDES ) { return ( ( Object.values( values ).length >= 1 && - Object.values( values ).length < 4 ) || + Object.values( values ).length < sides.length ) || new Set( Object.values( values ) ).size > 1 ); } diff --git a/packages/block-editor/src/hooks/dimensions.js b/packages/block-editor/src/hooks/dimensions.js index fc695a64f6ca11..6140e756c1d799 100644 --- a/packages/block-editor/src/hooks/dimensions.js +++ b/packages/block-editor/src/hooks/dimensions.js @@ -100,6 +100,10 @@ export function DimensionsPanel( props ) { ) } { ! isMarginDisabled && ( 0, + } ) } hasValue={ () => hasMarginValue( props ) } label={ __( 'Margin' ) } onDeselect={ () => resetMargin( props ) } diff --git a/packages/block-editor/src/hooks/margin.js b/packages/block-editor/src/hooks/margin.js index 06d86c85345fc9..229e744f70d51e 100644 --- a/packages/block-editor/src/hooks/margin.js +++ b/packages/block-editor/src/hooks/margin.js @@ -28,6 +28,8 @@ import { } from './dimensions'; import { cleanEmptyObject } from './utils'; import BlockPopover from '../components/block-popover'; +import SpacingSizesControl from '../components/spacing-sizes-control'; +import { getCustomValueFromPreset } from '../components/spacing-sizes-control/utils'; /** * Determines if there is margin support. @@ -101,6 +103,8 @@ export function MarginEdit( props ) { setAttributes, } = props; + const spacingSizes = useSetting( 'spacing.spacingSizes' ); + const units = useCustomUnits( { availableUnits: useSetting( 'spacing.units' ) || [ '%', @@ -135,15 +139,28 @@ export function MarginEdit( props ) { return Platform.select( { web: ( <> - + { ( ! spacingSizes || spacingSizes?.length === 0 ) && ( + + ) } + { spacingSizes?.length > 0 && ( + + ) } ), native: null, @@ -152,16 +169,31 @@ export function MarginEdit( props ) { export function MarginVisualizer( { clientId, attributes } ) { const margin = attributes?.style?.spacing?.margin; + const spacingSizes = useSetting( 'spacing.spacingSizes' ); + const style = useMemo( () => { + const marginTop = margin?.top + ? getCustomValueFromPreset( margin?.top, spacingSizes ) + : 0; + const marginRight = margin?.right + ? getCustomValueFromPreset( margin?.right, spacingSizes ) + : 0; + const marginBottom = margin?.bottom + ? getCustomValueFromPreset( margin?.bottom, spacingSizes ) + : 0; + const marginLeft = margin?.left + ? getCustomValueFromPreset( margin?.left, spacingSizes ) + : 0; + return { - borderTopWidth: margin?.top ?? 0, - borderRightWidth: margin?.right ?? 0, - borderBottomWidth: margin?.bottom ?? 0, - borderLeftWidth: margin?.left ?? 0, - top: margin?.top ? `-${ margin.top }` : 0, - right: margin?.right ? `-${ margin.right }` : 0, - bottom: margin?.bottom ? `-${ margin.bottom }` : 0, - left: margin?.left ? `-${ margin.left }` : 0, + borderTopWidth: marginTop, + borderRightWidth: marginRight, + borderBottomWidth: marginBottom, + borderLeftWidth: marginLeft, + top: marginTop !== 0 ? `-${ marginTop }` : 0, + right: marginRight !== 0 ? `-${ marginRight }` : 0, + bottom: marginBottom !== 0 ? `-${ marginBottom }` : 0, + left: marginLeft !== 0 ? `-${ marginLeft }` : 0, }; }, [ margin ] ); diff --git a/packages/edit-site/src/components/global-styles/dimensions-panel.js b/packages/edit-site/src/components/global-styles/dimensions-panel.js index 9a31f59ee4828a..cca4fdd0298282 100644 --- a/packages/edit-site/src/components/global-styles/dimensions-panel.js +++ b/packages/edit-site/src/components/global-styles/dimensions-panel.js @@ -435,16 +435,32 @@ export default function DimensionsPanel( { name } ) { label={ __( 'Margin' ) } onDeselect={ resetMarginValue } isShownByDefault={ true } + className={ classnames( { + 'tools-panel-item-spacing': showSpacingPresetsControl, + } ) } > - + { ! showSpacingPresetsControl && ( + + ) } + { showSpacingPresetsControl && ( + + ) } ) } { showGapControl && (