Skip to content

Commit

Permalink
Post Featured Image: New design for Replace and Remove buttons (#50269)
Browse files Browse the repository at this point in the history
* Post Featured Image: Use dropdown to add/replace image

* Allow popover to shift

* Fix focus style.

* Move the overflow to the button itself.

* Apply a max-height.

* Remove dropdown in favour of a Remove and Edit button

* Use same order as before

* Tweak labels

* Use glass button design

* Fix focus loss

* Add fade in / fade out

---------

Co-authored-by: jasmussen <[email protected]>
  • Loading branch information
noisysocks and jasmussen committed May 8, 2023
1 parent 31792a5 commit 1e5364f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 49 deletions.
61 changes: 26 additions & 35 deletions packages/editor/src/components/post-featured-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
ResponsiveWrapper,
withNotices,
withFilters,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { isBlobURL } from '@wordpress/blob';
import { useState } from '@wordpress/element';
import { useState, useRef } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { useSelect, withDispatch, withSelect } from '@wordpress/data';
import {
Expand All @@ -33,7 +34,6 @@ const ALLOWED_MEDIA_TYPES = [ 'image' ];
// Used when labels from post type were not yet loaded or when they are not present.
const DEFAULT_FEATURE_IMAGE_LABEL = __( 'Featured image' );
const DEFAULT_SET_FEATURE_IMAGE_LABEL = __( 'Set featured image' );
const DEFAULT_REMOVE_FEATURE_IMAGE_LABEL = __( 'Remove image' );

const instructions = (
<p>
Expand Down Expand Up @@ -96,6 +96,7 @@ function PostFeaturedImage( {
noticeUI,
noticeOperations,
} ) {
const toggleRef = useRef();
const [ isLoading, setIsLoading ] = useState( false );
const mediaUpload = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings().mediaUpload;
Expand Down Expand Up @@ -163,6 +164,7 @@ function PostFeaturedImage( {
render={ ( { open } ) => (
<div className="editor-post-featured-image__container">
<Button
ref={ toggleRef }
className={
! featuredImageId
? 'editor-post-featured-image__toggle'
Expand All @@ -172,7 +174,7 @@ function PostFeaturedImage( {
aria-label={
! featuredImageId
? null
: __( 'Edit or update the image' )
: __( 'Edit or replace the image' )
}
aria-describedby={
! featuredImageId
Expand All @@ -199,44 +201,33 @@ function PostFeaturedImage( {
?.set_featured_image ||
DEFAULT_SET_FEATURE_IMAGE_LABEL ) }
</Button>
{ !! featuredImageId && (
<HStack className="editor-post-featured-image__actions">
<Button
className="editor-post-featured-image__action"
onClick={ open }
// Prefer that screen readers use the .editor-post-featured-image__preview button.
aria-hidden="true"
>
{ __( 'Replace' ) }
</Button>
<Button
className="editor-post-featured-image__action"
onClick={ () => {
onRemoveImage();
toggleRef.current.focus();
} }
>
{ __( 'Remove' ) }
</Button>
</HStack>
) }
<DropZone onFilesDrop={ onDropFiles } />
</div>
) }
value={ featuredImageId }
/>
</MediaUploadCheck>
{ !! featuredImageId && (
<MediaUploadCheck>
{ media && (
<MediaUpload
title={
postType?.labels?.featured_image ||
DEFAULT_FEATURE_IMAGE_LABEL
}
onSelect={ onUpdateImage }
unstableFeaturedImageFlow
allowedTypes={ ALLOWED_MEDIA_TYPES }
modalClass="editor-post-featured-image__media-modal"
render={ ( { open } ) => (
<Button
onClick={ open }
variant="secondary"
>
{ __( 'Replace Image' ) }
</Button>
) }
/>
) }
<Button
onClick={ onRemoveImage }
variant="link"
isDestructive
>
{ postType?.labels?.remove_featured_image ||
DEFAULT_REMOVE_FEATURE_IMAGE_LABEL }
</Button>
</MediaUploadCheck>
) }
</div>
</PostFeaturedImageCheck>
);
Expand Down
52 changes: 38 additions & 14 deletions packages/editor/src/components/post-featured-image/style.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
.editor-post-featured-image {
padding: 0;

&__container {
margin-bottom: 1em;
position: relative;
}

.components-spinner {
position: absolute;
Expand All @@ -14,12 +10,6 @@
margin-left: -9px;
}

// Stack consecutive buttons.
.components-button + .components-button {
display: block;
margin-top: 1em;
}

// This keeps images at their intrinsic size (eg. a 50px
// image will never be wider than 50px).
.components-responsive-wrapper__content {
Expand All @@ -28,22 +18,40 @@
}
}

.editor-post-featured-image__container {
position: relative;

&:hover,
&:focus,
&:focus-within {
.editor-post-featured-image__actions {
opacity: 1;
}
}
}

.editor-post-featured-image__toggle,
.editor-post-featured-image__preview {
display: block;
width: 100%;
padding: 0;
transition: all 0.1s ease-out;
@include reduce-motion("transition");
box-shadow: 0 0 0 0 var(--wp-admin-theme-color);
overflow: hidden; // Ensure the focus style properly encapsulates the image.

// Apply a max-height.
display: flex;
justify-content: center;
max-height: 150px;
}

.editor-post-featured-image__preview {
height: auto;
}

.editor-post-featured-image__preview:not(:disabled):not([aria-disabled="true"]):focus {
box-shadow: 0 0 0 4px var(--wp-admin-theme-color);
.components-responsive-wrapper {
width: 100%;
background: $gray-100;
}
}

.editor-post-featured-image__toggle {
Expand All @@ -59,3 +67,19 @@
color: $gray-900;
}
}

.editor-post-featured-image__actions {
@include reduce-motion("transition");
bottom: 0;
opacity: 0; // Use opacity instead of visibility so that the buttons remain in the tab order.
padding: $grid-unit-10;
position: absolute;
transition: opacity 50ms ease-out;
}

.editor-post-featured-image__action {
backdrop-filter: blur(16px) saturate(180%);
background: rgba(255, 255, 255, 0.75);
flex-grow: 1;
justify-content: center;
}

0 comments on commit 1e5364f

Please sign in to comment.