Skip to content

Commit

Permalink
Fix booboos
Browse files Browse the repository at this point in the history
  • Loading branch information
tellthemachines committed Feb 23, 2024
1 parent 1c1f9bb commit 8498d43
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
36 changes: 25 additions & 11 deletions packages/block-editor/src/components/child-layout-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import { useEffect } from '@wordpress/element';
* @param {Object} props.value The child layout value.
* @param {Function} props.onChange Function to update the child layout value.
* @param {Object} props.parentLayout The parent layout value.
* @param {Object} props.alignments
*
* @return {Element} child layout edit element.
*/
export default function ChildLayoutControl( {
value: childLayout = {},
onChange,
onChangeAlign,
parentLayout,
alignments,
} ) {
const {
selfStretch,
Expand All @@ -36,13 +37,20 @@ export default function ChildLayoutControl( {
height,
width,
} = childLayout;

const {
current: currentAlignment,
supported: supportedAlignments,
onChangeAlignment,
} = alignments || {};

const {
orientation = 'horizontal',
type: parentType,
default: { type: defaultParentType = 'default' } = {},
justifyContent = 'left',
verticalAlignment = 'center',
alignWidth = 'none',
alignWidth: parentAlignment = 'none',
} = parentLayout ?? {};
const parentLayoutType = parentType || defaultParentType;

Expand All @@ -68,14 +76,20 @@ export default function ChildLayoutControl( {
value: 'content',
name: __( 'Default' ),
} );
if ( alignWidth === 'wide' ) {
if (
supportedAlignments?.includes( 'wide' ) &&
( parentAlignment === 'wide' || parentAlignment === 'full' )
) {
widthOptions.push( {
key: 'wide',
value: 'wide',
name: __( 'Wide' ),
} );
}
if ( alignWidth === 'full' ) {
if (
supportedAlignments?.includes( 'full' ) &&
parentAlignment === 'full'
) {
widthOptions.push( {
key: 'fill',
value: 'fill',
Expand All @@ -91,7 +105,7 @@ export default function ChildLayoutControl( {
{
key: 'fixedNoShrink',
value: 'fixedNoShrink',
name: __( 'Custom' ),
name: __( 'Fixed' ),
}
);
} else if (
Expand Down Expand Up @@ -178,9 +192,9 @@ export default function ChildLayoutControl( {
let selectedValue;
if ( isFlowOrConstrained ) {
// Replace "full" with "fill" for full width alignments.
if ( alignWidth === 'full' ) {
if ( currentAlignment === 'full' ) {
selectedValue = 'fill';
} else if ( alignWidth === 'wide' ) {
} else if ( currentAlignment === 'wide' ) {
selectedValue = 'wide';
} else if ( selfAlign === 'fixedNoShrink' ) {
selectedValue = 'fixedNoShrink';
Expand Down Expand Up @@ -233,19 +247,19 @@ export default function ChildLayoutControl( {
if ( isFlowOrConstrained ) {
if ( key === 'fill' ) {
onChange( { [ widthProp ]: key } );
onChangeAlign( { align: 'full' } );
onChangeAlignment( 'full' );
} else if ( key === 'wide' ) {
onChange( { [ widthProp ]: key } );
onChangeAlign( { align: 'wide' } );
onChangeAlignment( 'wide' );
} else if ( key === 'fixedNoShrink' ) {
onChange( {
...childLayout,
[ widthProp ]: key,
} );
onChangeAlign( { align: 'none' } );
onChangeAlignment( null );
} else {
onChange( { [ widthProp ]: key } );
onChangeAlign( { align: 'none' } );
onChangeAlignment( null );
}
} else if ( parentLayoutType === 'flex' ) {
onChange( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ const DEFAULT_CONTROLS = {
export default function DimensionsPanel( {
as: Wrapper = DimensionsToolsPanel,
value,
alignments = null,
onChange,
inheritedValue = value,
settings,
Expand Down Expand Up @@ -397,16 +398,9 @@ export default function DimensionsPanel( {
orientation === 'horizontal' ? __( 'Width' ) : __( 'Height' );
const childLayoutResetLabel =
parentLayoutType === 'flex' ? flexResetLabel : __( 'Grid spans' );

const setChildLayout = ( newChildLayout ) => {
onChange( {
...value,
layout: {
...newChildLayout,
},
} );
};
const setChildLayoutAlign = ( newChildLayoutAlign ) => {
onChange( newChildLayoutAlign );
onChange( setImmutably( value, [ 'layout' ], newChildLayout ) );
};

const resetChildLayoutValue = () => {
Expand Down Expand Up @@ -660,8 +654,8 @@ export default function DimensionsPanel( {
<ChildLayoutControl
value={ childLayout }
onChange={ setChildLayout }
onChangeAlign={ setChildLayoutAlign }
parentLayout={ settings?.parentLayout }
alignments={ alignments }
/>
</VStack>
) }
Expand Down
27 changes: 24 additions & 3 deletions packages/block-editor/src/hooks/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ function DimensionsInspectorControl( { children, resetAllFilter } ) {

export function DimensionsPanel( { clientId, name, setAttributes, settings } ) {
const isEnabled = useHasDimensionsPanel( settings );
const value = useSelect(
( select ) =>
select( blockEditorStore ).getBlockAttributes( clientId )?.style,
const { value, align } = useSelect(
( select ) => {
const blockAttributes =
select( blockEditorStore ).getBlockAttributes( clientId );

return {
value: blockAttributes?.style,
align: blockAttributes?.align,
};
},
[ clientId ]
);

const [ visualizedProperty, setVisualizedProperty ] = useVisualizer();
const onChange = ( newStyle ) => {
setAttributes( {
Expand All @@ -100,13 +108,26 @@ export function DimensionsPanel( { clientId, name, setAttributes, settings } ) {
...defaultSpacingControls,
};

/**
* Alignments are needed for the child layout control.
*/
const supportedAlignments = getBlockSupport( name, 'align' );
const onChangeAlignment = ( newAlign ) => {
setAttributes( { align: newAlign } );
};

return (
<>
<StylesDimensionsPanel
as={ DimensionsInspectorControl }
panelId={ clientId }
settings={ settings }
value={ value }
alignments={ {
current: align,
supported: supportedAlignments,
onChangeAlignment,
} }
onChange={ onChange }
defaultControls={ defaultControls }
onVisualize={ setVisualizedProperty }
Expand Down

0 comments on commit 8498d43

Please sign in to comment.