Skip to content

Commit

Permalink
Spacing presets: Add support for margins (#43246)
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz authored Aug 19, 2022
1 parent e4d81f9 commit ff3b6d3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,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
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/hooks/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export function DimensionsPanel( props ) {
) }
{ ! isMarginDisabled && (
<ToolsPanelItem
className={ classnames( {
'tools-panel-item-spacing':
spacingSizes && spacingSizes.length > 0,
} ) }
hasValue={ () => hasMarginValue( props ) }
label={ __( 'Margin' ) }
onDeselect={ () => resetMargin( props ) }
Expand Down
66 changes: 49 additions & 17 deletions packages/block-editor/src/hooks/margin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -101,6 +103,8 @@ export function MarginEdit( props ) {
setAttributes,
} = props;

const spacingSizes = useSetting( 'spacing.spacingSizes' );

const units = useCustomUnits( {
availableUnits: useSetting( 'spacing.units' ) || [
'%',
Expand Down Expand Up @@ -135,15 +139,28 @@ export function MarginEdit( props ) {
return Platform.select( {
web: (
<>
<BoxControl
values={ style?.spacing?.margin }
onChange={ onChange }
label={ __( 'Margin' ) }
sides={ sides }
units={ units }
allowReset={ false }
splitOnAxis={ splitOnAxis }
/>
{ ( ! spacingSizes || spacingSizes?.length === 0 ) && (
<BoxControl
values={ style?.spacing?.margin }
onChange={ onChange }
label={ __( 'Margin' ) }
sides={ sides }
units={ units }
allowReset={ false }
splitOnAxis={ splitOnAxis }
/>
) }
{ spacingSizes?.length > 0 && (
<SpacingSizesControl
values={ style?.spacing?.margin }
onChange={ onChange }
label={ __( 'Margin' ) }
sides={ sides }
units={ units }
allowReset={ false }
splitOnAxis={ false }
/>
) }
</>
),
native: null,
Expand All @@ -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 ] );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,32 @@ export default function DimensionsPanel( { name } ) {
label={ __( 'Margin' ) }
onDeselect={ resetMarginValue }
isShownByDefault={ true }
className={ classnames( {
'tools-panel-item-spacing': showSpacingPresetsControl,
} ) }
>
<BoxControl
values={ marginValues }
onChange={ setMarginValues }
label={ __( 'Margin' ) }
sides={ marginSides }
units={ units }
allowReset={ false }
splitOnAxis={ isAxialMargin }
/>
{ ! showSpacingPresetsControl && (
<BoxControl
values={ marginValues }
onChange={ setMarginValues }
label={ __( 'Margin' ) }
sides={ marginSides }
units={ units }
allowReset={ false }
splitOnAxis={ isAxialMargin }
/>
) }
{ showSpacingPresetsControl && (
<SpacingSizesControl
values={ marginValues }
onChange={ setMarginValues }
label={ __( 'Margin' ) }
sides={ marginSides }
units={ units }
allowReset={ false }
splitOnAxis={ isAxialMargin }
/>
) }
</ToolsPanelItem>
) }
{ showGapControl && (
Expand Down

0 comments on commit ff3b6d3

Please sign in to comment.