Skip to content

Commit

Permalink
Wrap post featured image in an A tag so that it simulates the fronten…
Browse files Browse the repository at this point in the history
…d, 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.
  • Loading branch information
ramonjd committed Oct 20, 2023
1 parent c1a3d5a commit ba56c3b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
31 changes: 27 additions & 4 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 @@ -295,7 +307,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
7 changes: 7 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,13 @@
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;
}
}

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

0 comments on commit ba56c3b

Please sign in to comment.