Skip to content

Commit

Permalink
Image Block: Enable image block to be selected correctly when clicked (
Browse files Browse the repository at this point in the history
…#56043)

* Image Block: Enable image blocks to be selected correctly when clicked

* Add tabIndex

* Update packages/block-library/src/image/editor.scss

Co-authored-by: Ramon <[email protected]>

* Wrap img element directly to make the image resizable

* Use `display:inline` instead of `display:inline-block`

---------

Co-authored-by: Ramon <[email protected]>
  • Loading branch information
2 people authored and mikachan committed Nov 22, 2023
1 parent 1dd3145 commit d51b7c6
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,29 @@ const scaleOptions = [
},
];

const disabledClickProps = {
onClick: ( event ) => event.preventDefault(),
'aria-disabled': true,
// If the image has a href, wrap in an <a /> tag to trigger any inherited link element styles.
const ImageWrapper = ( { href, children } ) => {
if ( ! href ) {
return children;
}
return (
<a
href={ href }
onClick={ ( event ) => event.preventDefault() }
aria-disabled={ true }
style={ {
// When the Image block is linked,
// it's wrapped with a disabled <a /> tag.
// Restore cursor style so it doesn't appear 'clickable'
// and remove pointer events. Safari needs the display property.
pointerEvents: 'none',
cursor: 'default',
display: 'inline',
} }
>
{ children }
</a>
);
};

export default function Image( {
Expand Down Expand Up @@ -653,25 +673,31 @@ export default function Image( {

if ( canEditImage && isEditingImage ) {
img = (
<ImageEditor
id={ id }
url={ url }
width={ numericWidth }
height={ numericHeight }
clientWidth={ fallbackClientWidth }
naturalHeight={ naturalHeight }
naturalWidth={ naturalWidth }
onSaveImage={ ( imageAttributes ) =>
setAttributes( imageAttributes )
}
onFinishEditing={ () => {
setIsEditingImage( false );
} }
borderProps={ isRounded ? undefined : borderProps }
/>
<ImageWrapper href={ href }>
<ImageEditor
id={ id }
url={ url }
width={ numericWidth }
height={ numericHeight }
clientWidth={ fallbackClientWidth }
naturalHeight={ naturalHeight }
naturalWidth={ naturalWidth }
onSaveImage={ ( imageAttributes ) =>
setAttributes( imageAttributes )
}
onFinishEditing={ () => {
setIsEditingImage( false );
} }
borderProps={ isRounded ? undefined : borderProps }
/>
</ImageWrapper>
);
} else if ( ! isResizable ) {
img = <div style={ { width, height, aspectRatio } }>{ img }</div>;
img = (
<div style={ { width, height, aspectRatio } }>
<ImageWrapper href={ href }>{ img }</ImageWrapper>
</div>
);
} else {
const numericRatio = aspectRatio && evalAspectRatio( aspectRatio );
const customRatio = numericWidth / numericHeight;
Expand Down Expand Up @@ -774,7 +800,7 @@ export default function Image( {
} }
resizeRatio={ align === 'center' ? 2 : 1 }
>
{ img }
<ImageWrapper href={ href }>{ img }</ImageWrapper>
</ResizableBox>
);
}
Expand All @@ -788,14 +814,7 @@ export default function Image( {
{ /* Hide controls during upload to avoid component remount,
which causes duplicated image upload. */ }
{ ! temporaryURL && controls }
{ /* 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
) }
{ img }
{ showCaption &&
( ! RichText.isEmpty( caption ) || isSelected ) && (
<RichText
Expand Down

0 comments on commit d51b7c6

Please sign in to comment.