Skip to content

Commit

Permalink
Extract a DimensionsPanel component as a reusable component between G…
Browse files Browse the repository at this point in the history
…lobal Styles and Block Inspector (#48070)
  • Loading branch information
youknowriad authored Feb 28, 2023
1 parent 692715d commit 2b39d98
Show file tree
Hide file tree
Showing 17 changed files with 1,070 additions and 1,806 deletions.
106 changes: 106 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,106 @@
/**
* 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' && orientation === 'horizontal' ) {
return __( 'Specify a fixed width.' );
} else if ( selfStretch === 'fixed' ) {
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',
} );
}
}, [] );

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

1 comment on commit 2b39d98

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 2b39d98.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4291248776
📝 Reported issues:

Please sign in to comment.