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

Show Cover block placeholder only if it has no inner blocks #31402

Merged
merged 3 commits into from
May 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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