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

Post feature image block: wrap images with hrefs in an A tag #55498

Merged
merged 3 commits into from
Nov 9, 2023
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
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;
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 {
Copy link
Member Author

Choose a reason for hiding this comment

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

.components-button,
.components-placeholder__instructions,
.components-placeholder__label {
opacity: 1;
pointer-events: auto;
}
}
}

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