Skip to content

Commit

Permalink
Image block: wrap images with hrefs in an A tag (#55470)
Browse files Browse the repository at this point in the history
* This commit sees what happens when we wrap the image element in an A tag in the editor.
This is to ensure any inherited styles from the link element, such as border color, apply to the image.

* Removing duplicate <a href /> wrapper
Adding disabled onClick and aria attribute
  • Loading branch information
ramonjd authored and Siobhan committed Oct 20, 2023
1 parent a783961 commit 3ade049
Showing 1 changed file with 13 additions and 2 deletions.
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

0 comments on commit 3ade049

Please sign in to comment.