Skip to content

Commit

Permalink
Post feature image block: wrap images with hrefs in an A tag (#55498)
Browse files Browse the repository at this point in the history
* Wrap post featured image in an A tag so that it simulates the frontend, and ensures any styles the IMG elements inherits from the A element are also displayed in the editor.
The cursor style should be default since, in the editor, we don't want to indicate that the image is clickable.

* - also wrap the placeholder in an a tag for consistency

* Ensure that the placeholder items are visible by overwriting the components CSS, which has an immediate child selector to show the button. With the A tag, the placeholder is no longer the immediate child.
  • Loading branch information
ramonjd authored Nov 9, 2023
1 parent 8e9b4c0 commit 57211ac
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
43 changes: 38 additions & 5 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ function getMediaSourceUrlBySizeSlug( media, slug ) {
);
}

const disabledClickProps = {
onClick: ( event ) => event.preventDefault(),
'aria-disabled': true,
};

export default function PostFeaturedImageEdit( {
clientId,
attributes,
Expand All @@ -67,20 +72,27 @@ export default function PostFeaturedImageEdit( {
postId
);

const { media, postType } = useSelect(
const { media, postType, postPermalink } = useSelect(
( select ) => {
const { getMedia, getPostType } = select( coreStore );
const { getMedia, getPostType, getEditedEntityRecord } =
select( coreStore );
return {
media:
featuredImage &&
getMedia( featuredImage, {
context: 'view',
} ),
postType: postTypeSlug && getPostType( postTypeSlug ),
postPermalink: getEditedEntityRecord(
'postType',
postTypeSlug,
postId
)?.link,
};
},
[ featuredImage, postTypeSlug ]
[ featuredImage, postTypeSlug, postId ]
);

const mediaUrl = getMediaSourceUrlBySizeSlug( media, sizeSlug );

const imageSizes = useSelect(
Expand Down Expand Up @@ -197,7 +209,17 @@ export default function PostFeaturedImageEdit( {
<>
{ controls }
<div { ...blockProps }>
{ placeholder() }
{ !! isLink ? (
<a
href={ postPermalink }
target={ linkTarget }
{ ...disabledClickProps }
>
{ placeholder() }
</a>
) : (
placeholder()
) }
<Overlay
attributes={ attributes }
setAttributes={ setAttributes }
Expand Down Expand Up @@ -295,7 +317,18 @@ export default function PostFeaturedImageEdit( {
</BlockControls>
) }
<figure { ...blockProps }>
{ image }
{ /* If the featured image is linked, wrap in an <a /> tag to trigger any inherited link element styles */ }
{ !! isLink ? (
<a
href={ postPermalink }
target={ linkTarget }
{ ...disabledClickProps }
>
{ image }
</a>
) : (
image
) }
<Overlay
attributes={ attributes }
setAttributes={ setAttributes }
Expand Down
19 changes: 19 additions & 0 deletions packages/block-library/src/post-featured-image/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@
height: 100%;
width: 100%;
}

// When the Post Featured Image block is linked,
// it's wrapped with a disabled <a /> tag.
// Restore cursor style so it doesn't appear 'clickable'.
> a {
cursor: default;
}

// When the Post Featured Image block is linked,
// and wrapped with a disabled <a /> tag
// ensure that the placeholder items are visible when selected.
&.is-selected .components-placeholder.has-illustration {
.components-button,
.components-placeholder__instructions,
.components-placeholder__label {
opacity: 1;
pointer-events: auto;
}
}
}

div[data-type="core/post-featured-image"] {
Expand Down

1 comment on commit 57211ac

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 57211ac.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6817711002
📝 Reported issues:

Please sign in to comment.