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

Spacing presets: Add support for margins #43246

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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