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

Extract a DimensionsPanel component as a reusable component between Global Styles and Block Inspector #48070

Merged
merged 16 commits into from
Feb 28, 2023
Merged
107 changes: 107 additions & 0 deletions packages/block-editor/src/components/child-layout-control/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* WordPress dependencies
*/
import {
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
__experimentalUnitControl as UnitControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEffect } from '@wordpress/element';

function helpText( selfStretch, parentLayout ) {
const { orientation = 'horizontal' } = parentLayout;

if ( selfStretch === 'fill' ) {
return __( 'Stretch to fill available space.' );
}
if ( selfStretch === 'fixed' ) {
if ( orientation === 'horizontal' ) {
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
return __( 'Specify a fixed width.' );
}
return __( 'Specify a fixed height.' );
}
return __( 'Fit contents.' );
}

/**
* Form to edit the child layout value.
*
* @param {Object} props Props.
* @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.
*
* @return {WPElement} child layout edit element.
*/
export default function ChildLayoutControl( {
value: childLayout = {},
onChange,
parentLayout,
} ) {
const { selfStretch, flexSize } = childLayout;

useEffect( () => {
if ( selfStretch === 'fixed' && ! flexSize ) {
onChange( {
...childLayout,
selfStretch: 'fit',
} );
}
}, [] );
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

return (
<>
<ToggleGroupControl
__nextHasNoMarginBottom
size={ '__unstable-large' }
label={ childLayoutOrientation( parentLayout ) }
value={ selfStretch || 'fit' }
help={ helpText( selfStretch, parentLayout ) }
onChange={ ( value ) => {
const newFlexSize = value !== 'fixed' ? null : flexSize;
onChange( {
...childLayout,
selfStretch: value,
flexSize: newFlexSize,
} );
} }
isBlock={ true }
>
<ToggleGroupControlOption
key={ 'fit' }
value={ 'fit' }
label={ __( 'Fit' ) }
/>
<ToggleGroupControlOption
key={ 'fill' }
value={ 'fill' }
label={ __( 'Fill' ) }
/>
<ToggleGroupControlOption
key={ 'fixed' }
value={ 'fixed' }
label={ __( 'Fixed' ) }
/>
</ToggleGroupControl>
{ selfStretch === 'fixed' && (
<UnitControl
size={ '__unstable-large' }
onChange={ ( value ) => {
onChange( {
...childLayout,
flexSize: value,
} );
} }
value={ flexSize }
/>
) }
</>
);
}

export function childLayoutOrientation( parentLayout ) {
const { orientation = 'horizontal' } = parentLayout;

return orientation === 'horizontal' ? __( 'Width' ) : __( 'Height' );
}
Loading