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

Cover: Allow drag n drop media replacement #29813

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
39 changes: 39 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,42 @@
// 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.3s opacity, 0.3s border;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. We use 0.2s in most other places to make it snappy — if that feels okay to you, might be a good change. If it feels too snappy, okay to keep 0.3, but worth a shot.

Might want to include this right below the transition rule:

@include reduce-motion("transition");

Copy link
Member Author

Choose a reason for hiding this comment

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

I think 0.2s looks better, and it isn't too snappy.

Wasn't aware of the reduce-motion mixin, Thanks for mentioning it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, it's easily missed. We have one for the animation property as well, and if you basically output it on the line right after either transition or animation both, you'll be sure that for people to check the system level setting have their needs met. It's a nice little trick, it just sets the duration to about zero.

}

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

Choose a reason for hiding this comment

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

Nice 👌


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

.components-drop-zone__content {
display: flex;
align-items: center;
top: -36px;
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should round this number up to 40px ($grid-unit-50), or down to 32 ($grid-unit-40). It's a small thing, but might be slightly more griddy. Alternately, I see the grid as being fundamentally base12, even though we leverage an 8px base. You could do ($grid-unit-15 * 3).

Copy link
Member Author

Choose a reason for hiding this comment

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

Tested with both $grid-unit-40 and $grid-unit-40 initially, but 36px give it more "centered" look.

I've updated styles to use ($grid-unit-15 * 3).

left: -36px;
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;
}
}