Skip to content

Commit

Permalink
Cover: Allow drag n drop media replacement (#29813)
Browse files Browse the repository at this point in the history
* Cover: Allow drag n drop media replacement

See #26388.

* Update transition duration and use grid unit for positioning
  • Loading branch information
Mamaduka authored Mar 12, 2021
1 parent 065fdcb commit 877f615
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 21 deletions.
64 changes: 43 additions & 21 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,38 @@ function mediaPosition( { x, y } ) {
return `${ Math.round( x * 100 ) }% ${ Math.round( y * 100 ) }%`;
}

function CoverPlaceholder( {
coverUrl,
children,
noticeUI,
noticeOperations,
onSelectMedia,
} ) {
const { removeAllNotices, createErrorNotice } = noticeOperations;
return (
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
labels={ {
title: __( 'Cover' ),
instructions: __(
'Upload an image or video file, or pick one from your media library.'
),
} }
onSelect={ onSelectMedia }
accept="image/*,video/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
notices={ noticeUI }
disableMediaButtons={ !! coverUrl }
onError={ ( message ) => {
removeAllNotices();
createErrorNotice( message );
} }
>
{ children }
</MediaPlaceholder>
);
}

function CoverEdit( {
attributes,
isSelected,
Expand Down Expand Up @@ -329,7 +361,6 @@ function CoverEdit( {
const isVideoBackground = VIDEO_BACKGROUND_TYPE === backgroundType;

const [ temporaryMinHeight, setTemporaryMinHeight ] = useState( null );
const { removeAllNotices, createErrorNotice } = noticeOperations;

const minHeightWithUnit = minHeightUnit
? `${ minHeight }${ minHeightUnit }`
Expand Down Expand Up @@ -507,9 +538,6 @@ function CoverEdit( {
);

if ( ! hasBackground ) {
const placeholderIcon = <BlockIcon icon={ icon } />;
const label = __( 'Cover' );

return (
<>
{ controls }
Expand All @@ -520,22 +548,10 @@ function CoverEdit( {
blockProps.className
) }
>
<MediaPlaceholder
icon={ placeholderIcon }
labels={ {
title: label,
instructions: __(
'Upload an image or video file, or pick one from your media library.'
),
} }
onSelect={ onSelectMedia }
accept="image/*,video/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
notices={ noticeUI }
onError={ ( message ) => {
removeAllNotices();
createErrorNotice( message );
} }
<CoverPlaceholder
noticeUI={ noticeUI }
onSelectMedia={ onSelectMedia }
noticeOperations={ noticeOperations }
>
<div className="wp-block-cover__placeholder-background-options">
<ColorPalette
Expand All @@ -545,7 +561,7 @@ function CoverEdit( {
clearable={ false }
/>
</div>
</MediaPlaceholder>
</CoverPlaceholder>
</div>
</>
);
Expand Down Expand Up @@ -627,6 +643,12 @@ function CoverEdit( {
/>
) }
{ isBlogUrl && <Spinner /> }
<CoverPlaceholder
coverUrl={ url }
noticeUI={ noticeUI }
onSelectMedia={ onSelectMedia }
noticeOperations={ noticeOperations }
/>
<div { ...innerBlocksProps } />
</div>
</>
Expand Down
40 changes: 40 additions & 0 deletions packages/block-library/src/cover/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,43 @@
// Important is used to have higher specificity than the inline style set by re-resizable library.
height: auto !important;
}

// Only target direct dropzone.
.wp-block-cover > .components-drop-zone {
&.is-active {
transition: 0.2s opacity, 0.2s border;
@include reduce-motion("transition");
}

&.is-dragging-over-element {
background-color: transparent;
border: $grid-unit-60 solid var(--wp-admin-theme-color);

.components-drop-zone__content {
transform: none;
}
}

.components-drop-zone__content {
display: flex;
align-items: center;
top: -($grid-unit-15 * 3);
left: -($grid-unit-15 * 3);
transform: none;
}

.components-drop-zone__content-icon,
.components-drop-zone__content-text {
display: inline;
}

.components-drop-zone__content-icon {
// Reset margin.
margin: 0;
margin-right: $grid-unit;
}

.components-drop-zone__content-text {
font-size: $default-font-size;
}
}

0 comments on commit 877f615

Please sign in to comment.