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

Image block: wrap images with hrefs in an A tag #55470

Merged
merged 2 commits into from
Oct 19, 2023
Merged
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
15 changes: 13 additions & 2 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ const scaleOptions = [
},
];

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

export default function Image( {
temporaryURL,
attributes,
Expand Down Expand Up @@ -725,7 +730,6 @@ export default function Image( {
}
}
/* eslint-enable no-lonely-if */

img = (
<ResizableBox
style={ {
Expand Down Expand Up @@ -784,7 +788,14 @@ export default function Image( {
{ /* Hide controls during upload to avoid component remount,
which causes duplicated image upload. */ }
{ ! temporaryURL && controls }
{ img }
{ /* If the image has a href, wrap in an <a /> tag to trigger any inherited link element styles */ }
{ !! href ? (
<a href={ href } { ...disabledClickProps }>
{ img }
</a>
) : (
img
) }
{ showCaption &&
( ! RichText.isEmpty( caption ) || isSelected ) && (
<RichText
Expand Down
Loading