Skip to content

Commit

Permalink
Use state for spacing visualizers
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende committed Jul 19, 2021
1 parent 0574f1f commit 6cbfc20
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
16 changes: 2 additions & 14 deletions packages/block-editor/src/hooks/margin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function MarginEdit( props ) {
name: blockName,
attributes: { style },
setAttributes,
setVisualizer,
} = props;

const units = useCustomUnits( {
Expand Down Expand Up @@ -83,26 +84,13 @@ export function MarginEdit( props ) {
} );
};

const onChangeShowVisualizer = ( next ) => {
const newStyle = {
...style,
visualizers: {
margin: next,
},
};

setAttributes( {
style: cleanEmptyObject( newStyle ),
} );
};

return Platform.select( {
web: (
<>
<BoxControl
values={ style?.spacing?.margin }
onChange={ onChange }
onChangeShowVisualizer={ onChangeShowVisualizer }
onChangeShowVisualizer={ setVisualizer }
label={ __( 'Margin' ) }
sides={ sides }
units={ units }
Expand Down
16 changes: 2 additions & 14 deletions packages/block-editor/src/hooks/padding.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function PaddingEdit( props ) {
name: blockName,
attributes: { style },
setAttributes,
setVisualizer,
} = props;

const units = useCustomUnits( {
Expand Down Expand Up @@ -83,26 +84,13 @@ export function PaddingEdit( props ) {
} );
};

const onChangeShowVisualizer = ( next ) => {
const newStyle = {
...style,
visualizers: {
padding: next,
},
};

setAttributes( {
style: cleanEmptyObject( newStyle ),
} );
};

return Platform.select( {
web: (
<>
<BoxControl
values={ style?.spacing?.padding }
onChange={ onChange }
onChangeShowVisualizer={ onChangeShowVisualizer }
onChangeShowVisualizer={ setVisualizer }
label={ __( 'Padding' ) }
sides={ sides }
units={ units }
Expand Down
15 changes: 13 additions & 2 deletions packages/block-editor/src/hooks/spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const SPACING_SUPPORT_KEY = 'spacing';
export function SpacingPanel( props ) {
const isDisabled = useIsSpacingDisabled( props );
const isSupported = hasSpacingSupport( props.name );
const setSpacingVisualizer = ( key ) => ( next ) =>
props.setSpacingVisualizer( ( prev ) => ( {
...prev,
[ key ]: typeof next === 'function' ? next( prev ) : next,
} ) );

if ( isDisabled || ! isSupported ) {
return null;
Expand All @@ -37,8 +42,14 @@ export function SpacingPanel( props ) {
return (
<InspectorControls key="spacing">
<PanelBody title={ __( 'Spacing' ) }>
<PaddingEdit { ...props } />
<MarginEdit { ...props } />
<PaddingEdit
{ ...props }
setVisualizer={ setSpacingVisualizer( 'padding' ) }
/>
<MarginEdit
{ ...props }
setVisualizer={ setSpacingVisualizer( 'margin' ) }
/>
</PanelBody>
</InspectorControls>
);
Expand Down
16 changes: 14 additions & 2 deletions packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import {
getBlockSupport,
Expand Down Expand Up @@ -224,6 +225,12 @@ export function addEditProps( settings ) {
export const withBlockControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const shouldDisplayControls = useDisplayBlockControls();
const [ visualizers, setVisualizers ] = useState( null );
const setStyleVisualizer = ( key ) => ( next ) =>
setVisualizers( ( prev ) => ( {
...prev,
[ key ]: typeof next === 'function' ? next( prev ) : next,
} ) );

return (
<>
Expand All @@ -232,10 +239,15 @@ export const withBlockControls = createHigherOrderComponent(
<TypographyPanel { ...props } />
<BorderPanel { ...props } />
<ColorEdit { ...props } />
<SpacingPanel { ...props } />
<SpacingPanel
{ ...props }
setSpacingVisualizer={ setStyleVisualizer(
'spacing'
) }
/>
</>
) }
<BlockEdit { ...props } />
<BlockEdit { ...props } styleVisualizer={ visualizers } />
</>
);
},
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ function CoverEdit( {
setAttributes,
setOverlayColor,
toggleSelection,
styleVisualizer,
} ) {
const {
contentPosition,
Expand Down Expand Up @@ -630,7 +631,7 @@ function CoverEdit( {
>
<BoxControlVisualizer
values={ styleAttribute?.spacing?.padding }
showValues={ styleAttribute?.visualizers?.padding }
showValues={ styleVisualizer?.spacing?.padding }
/>
<ResizableCover
className="block-library-cover__resize-container"
Expand Down

0 comments on commit 6cbfc20

Please sign in to comment.