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

Site Logo: Prevent focus loss when updating media from the sidebar #68621

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
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
75 changes: 29 additions & 46 deletions packages/block-library/src/site-logo/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import {
BlockControls,
InspectorControls,
MediaPlaceholder,
MediaUpload,
MediaUploadCheck,
MediaReplaceFlow,
useBlockProps,
store as blockEditorStore,
Expand Down Expand Up @@ -347,29 +345,24 @@ const SiteLogo = ( {

// This is a light wrapper around MediaReplaceFlow because the block has two
// different MediaReplaceFlows, one for the inspector and one for the toolbar.
function SiteLogoReplaceFlow( {
mediaURL,
onRemoveLogo,
...mediaReplaceProps
} ) {
function SiteLogoReplaceFlow( { mediaURL, ...mediaReplaceProps } ) {
return (
<MediaReplaceFlow
{ ...mediaReplaceProps }
mediaURL={ mediaURL }
allowedTypes={ ALLOWED_MEDIA_TYPES }
accept={ ACCEPT_MEDIA_STRING }
onReset={ onRemoveLogo }
/>
);
}

const InspectorLogoPreview = ( { mediaItemData = {}, itemGroupProps } ) => {
const InspectorLogoPreview = ( { media, itemGroupProps } ) => {
const {
alt_text: alt,
source_url: logoUrl,
slug: logoSlug,
media_details: logoMediaDetails,
} = mediaItemData;
} = media ?? {};
Copy link
Member Author

Choose a reason for hiding this comment

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

The media can be null, where the default parameter isn't used and triggers an error when deconstructing. Using fallback here prevents that.

const logoLabel = logoMediaDetails?.sizes?.full?.file || logoSlug;
return (
<ItemGroup { ...itemGroupProps } as="span">
Expand Down Expand Up @@ -530,7 +523,7 @@ export default function LogoEdit( {
name: ! logoUrl ? __( 'Choose logo' ) : __( 'Replace' ),
onSelect: onSelectLogo,
onError: onUploadError,
onRemoveLogo,
onReset: onRemoveLogo,
Copy link
Member Author

Choose a reason for hiding this comment

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

Just mapping callback to support prop.

};
const controls = canUserEdit && (
<BlockControls group="other">
Expand Down Expand Up @@ -603,50 +596,40 @@ export default function LogoEdit( {
<InspectorControls>
<PanelBody title={ __( 'Media' ) }>
<div className="block-library-site-logo__inspector-media-replace-container">
{ ! canUserEdit && !! logoUrl && (
{ ! canUserEdit ? (
<InspectorLogoPreview
mediaItemData={ mediaItemData }
media={ mediaItemData }
itemGroupProps={ {
isBordered: true,
className:
'block-library-site-logo__inspector-readonly-logo-preview',
} }
/>
) }
{ canUserEdit && !! logoUrl && (
<SiteLogoReplaceFlow
{ ...mediaReplaceFlowProps }
name={
<InspectorLogoPreview
mediaItemData={ mediaItemData }
/>
}
popoverProps={ {} }
Copy link
Member Author

Choose a reason for hiding this comment

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

This prop was doing nothing. The popoverProps is undefined by default.

/>
) }
{ canUserEdit && ! logoUrl && (
<MediaUploadCheck>
<MediaUpload
onSelect={ onInitialSelectLogo }
allowedTypes={ ALLOWED_MEDIA_TYPES }
render={ ( { open } ) => (
<div className="block-library-site-logo__inspector-upload-container">
<Button
__next40pxDefaultSize
onClick={ open }
variant="secondary"
>
{ isLoading ? (
<Spinner />
) : (
__( 'Choose logo' )
) }
</Button>
<DropZone onFilesDrop={ onFilesDrop } />
</div>
) : (
<>
<SiteLogoReplaceFlow
{ ...mediaReplaceFlowProps }
name={
!! logoUrl ? (
<InspectorLogoPreview
media={ mediaItemData }
/>
) : (
__( 'Choose logo' )
)
}
renderToggle={ ( props ) => (
<Button { ...props } __next40pxDefaultSize>
{ temporaryURL ? (
<Spinner />
) : (
props.children
) }
</Button>
Comment on lines +621 to +628
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixes spacing inconsistency with the button. See #68084.

) }
/>
</MediaUploadCheck>
<DropZone onFilesDrop={ onFilesDrop } />
</>
) }
</div>
</PanelBody>
Expand Down
9 changes: 3 additions & 6 deletions packages/block-library/src/site-logo/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,16 @@
}
}

.block-library-site-logo__inspector-upload-container {
.block-library-site-logo__inspector-media-replace-container {
// Ensure the dropzone is positioned to the size of the item.
position: relative;

// Since there is no option to skip rendering the drag'n'drop icon in drop
// zone, we hide it for now.
.components-drop-zone__content-icon {
display: none;
}
}

.block-library-site-logo__inspector-upload-container,
.block-library-site-logo__inspector-media-replace-container {
button.components-button {
color: $gray-900;
box-shadow: inset 0 0 0 1px $gray-400;
Expand All @@ -144,9 +143,7 @@
text-align: start;
text-align-last: center;
}
}

.block-library-site-logo__inspector-media-replace-container {
.components-dropdown {
display: block;
}
Expand Down
Loading