Skip to content

Commit

Permalink
Show Cover block placeholder only if it has no inner blocks (#31402)
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman authored May 12, 2021
1 parent ff4c3c4 commit 60432f0
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { withDispatch } from '@wordpress/data';
import { withDispatch, useSelect } from '@wordpress/data';
import { cover as icon } from '@wordpress/icons';
import { isBlobURL } from '@wordpress/blob';

Expand Down Expand Up @@ -253,7 +253,7 @@ function mediaPosition( { x, y } ) {
const isTemporaryMedia = ( id, url ) => ! id && isBlobURL( url );

function CoverPlaceholder( {
hasBackground = false,
disableMediaButtons = false,
children,
noticeUI,
noticeOperations,
Expand All @@ -273,7 +273,7 @@ function CoverPlaceholder( {
accept="image/*,video/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
notices={ noticeUI }
disableMediaButtons={ hasBackground }
disableMediaButtons={ disableMediaButtons }
onError={ ( message ) => {
removeAllNotices();
createErrorNotice( message );
Expand All @@ -286,6 +286,7 @@ function CoverPlaceholder( {

function CoverEdit( {
attributes,
clientId,
isSelected,
noticeUI,
noticeOperations,
Expand Down Expand Up @@ -409,6 +410,13 @@ function CoverEdit( {
styleOfRef[ property ] = mediaPosition( value );
};

const hasInnerBlocks = useSelect(
( select ) =>
select( blockEditorStore ).getBlock( clientId ).innerBlocks.length >
0,
[ clientId ]
);

const controls = (
<>
<BlockControls group="block">
Expand All @@ -420,12 +428,12 @@ function CoverEdit( {
contentPosition: nextPosition,
} )
}
isDisabled={ ! hasBackground }
isDisabled={ ! hasInnerBlocks }
/>
<FullHeightAlignmentControl
isActive={ isMinFullHeight }
onToggle={ toggleMinFullHeight }
isDisabled={ ! hasBackground }
isDisabled={ ! hasInnerBlocks }
/>
</BlockControls>
<BlockControls group="other">
Expand Down Expand Up @@ -551,7 +559,7 @@ function CoverEdit( {
}
);

if ( ! hasBackground ) {
if ( ! hasInnerBlocks && ! hasBackground ) {
return (
<>
{ controls }
Expand Down Expand Up @@ -658,7 +666,7 @@ function CoverEdit( {
) }
{ isUploadingMedia && <Spinner /> }
<CoverPlaceholder
hasBackground={ hasBackground }
disableMediaButtons
noticeUI={ noticeUI }
onSelectMedia={ onSelectMedia }
noticeOperations={ noticeOperations }
Expand Down

0 comments on commit 60432f0

Please sign in to comment.