-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a DimensionsPanel component as a reusable component between G…
…lobal Styles and Block Inspector (#48070)
- Loading branch information
1 parent
692715d
commit 2b39d98
Showing
17 changed files
with
1,070 additions
and
1,806 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
packages/block-editor/src/components/child-layout-control/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} |
Oops, something went wrong.
2b39d98
There was a problem hiding this comment.
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:
specs/editor/plugins/custom-post-types.test.js