Skip to content

Commit

Permalink
Image cropper: remove clientWidth prop with useResizeObserver (#60674)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 7fc8973 commit e05558a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 66 deletions.
13 changes: 11 additions & 2 deletions packages/block-editor/src/components/image-editor/cropper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { Spinner } from '@wordpress/components';
import { useResizeObserver } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -20,7 +21,6 @@ export default function ImageCropper( {
url,
width,
height,
clientWidth,
naturalHeight,
naturalWidth,
borderProps,
Expand All @@ -36,14 +36,16 @@ export default function ImageCropper( {
setZoom,
rotation,
} = useImageEditingContext();
const [ contentResizeListener, { width: clientWidth } ] =
useResizeObserver();

let editedHeight = height || ( clientWidth * naturalHeight ) / naturalWidth;

if ( rotation % 180 === 90 ) {
editedHeight = ( clientWidth * naturalWidth ) / naturalHeight;
}

return (
const area = (
<div
className={ classnames(
'wp-block-image__crop-area',
Expand Down Expand Up @@ -79,4 +81,11 @@ export default function ImageCropper( {
{ isInProgress && <Spinner /> }
</div>
);

return (
<>
{ contentResizeListener }
{ area }
</>
);
}
2 changes: 0 additions & 2 deletions packages/block-editor/src/components/image-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default function ImageEditor( {
url,
width,
height,
clientWidth,
naturalHeight,
naturalWidth,
onSaveImage,
Expand All @@ -40,7 +39,6 @@ export default function ImageEditor( {
url={ url }
width={ width }
height={ height }
clientWidth={ clientWidth }
naturalHeight={ naturalHeight }
naturalWidth={ naturalWidth }
/>
Expand Down
7 changes: 1 addition & 6 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ export function ImageEdit( {
}
}, [ align ] );

const ref = useRef();
const { getSettings } = useSelect( blockEditorStore );
const blockEditingMode = useBlockEditingMode();

Expand Down Expand Up @@ -301,10 +300,7 @@ export function ImageEdit( {
Object.keys( borderProps.style ).length > 0 ),
} );

const blockProps = useBlockProps( {
ref,
className: classes,
} );
const blockProps = useBlockProps( { className: classes } );

// Much of this description is duplicated from MediaPlaceholder.
const { lockUrlControls = false, lockUrlControlsMessage } = useSelect(
Expand Down Expand Up @@ -386,7 +382,6 @@ export function ImageEdit( {
onSelectImage={ onSelectImage }
onSelectURL={ onSelectURL }
onUploadError={ onUploadError }
containerRef={ ref }
context={ context }
clientId={ clientId }
blockEditingMode={ blockEditingMode }
Expand Down
8 changes: 0 additions & 8 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import { store as coreStore } from '@wordpress/core-data';
*/
import { unlock } from '../lock-unlock';
import { createUpgradedEmbedBlock } from '../embed/util';
import useClientWidth from './use-client-width';
import { isExternalImage } from './edit';
import { Caption } from '../utils/caption';

Expand Down Expand Up @@ -103,7 +102,6 @@ export default function Image( {
onSelectImage,
onSelectURL,
onUploadError,
containerRef,
context,
clientId,
blockEditingMode,
Expand Down Expand Up @@ -176,7 +174,6 @@ export default function Image( {
] = useState( {} );
const [ isEditingImage, setIsEditingImage ] = useState( false );
const [ externalBlob, setExternalBlob ] = useState();
const clientWidth = useClientWidth( containerRef, [ align ] );
const hasNonContentControls = blockEditingMode === 'default';
const isContentOnlyMode = blockEditingMode === 'contentOnly';
const isResizable =
Expand Down Expand Up @@ -788,10 +785,6 @@ export default function Image( {
/* eslint-enable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/click-events-have-key-events */
);

// clientWidth needs to be a number for the image Cropper to work, but sometimes it's 0
// So we try using the imageRef width first and fallback to clientWidth.
const fallbackClientWidth = imageRef.current?.width || clientWidth;

if ( canEditImage && isEditingImage ) {
img = (
<ImageWrapper href={ href }>
Expand All @@ -800,7 +793,6 @@ export default function Image( {
url={ url }
width={ numericWidth }
height={ numericHeight }
clientWidth={ fallbackClientWidth }
naturalHeight={ naturalHeight }
naturalWidth={ naturalWidth }
onSaveImage={ ( imageAttributes ) =>
Expand Down
25 changes: 0 additions & 25 deletions packages/block-library/src/image/use-client-width.js

This file was deleted.

25 changes: 2 additions & 23 deletions packages/block-library/src/site-logo/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
createInterpolateElement,
useEffect,
useState,
useRef,
} from '@wordpress/element';
import { __, isRTL } from '@wordpress/i18n';
import {
Expand Down Expand Up @@ -50,11 +49,6 @@ import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import useClientWidth from '../image/use-client-width';

/**
* Module constants
*/
import { MIN_SIZE } from '../image/constants';

const ALLOWED_MEDIA_TYPES = [ 'image' ];
Expand All @@ -63,7 +57,6 @@ const ACCEPT_MEDIA_STRING = 'image/*';
const SiteLogo = ( {
alt,
attributes: { align, width, height, isLink, linkTarget, shouldSyncIcon },
containerRef,
isSelected,
setAttributes,
setLogo,
Expand All @@ -74,7 +67,6 @@ const SiteLogo = ( {
setIcon,
canUserEdit,
} ) => {
const clientWidth = useClientWidth( containerRef, [ align ] );
const isLargeViewport = useViewportMatch( 'medium' );
const isWideAligned = [ 'wide', 'full' ].includes( align );
const isResizable = ! isWideAligned && isLargeViewport;
Expand Down Expand Up @@ -154,14 +146,7 @@ const SiteLogo = ( {
);
}

let imageWidthWithinContainer;

if ( clientWidth && naturalWidth && naturalHeight ) {
const exceedMaxWidth = naturalWidth > clientWidth;
imageWidthWithinContainer = exceedMaxWidth ? clientWidth : naturalWidth;
}

if ( ! isResizable || ! imageWidthWithinContainer ) {
if ( ! isResizable || ! naturalWidth || ! naturalHeight ) {
return <div style={ { width, height } }>{ imgWrapper }</div>;
}

Expand Down Expand Up @@ -227,7 +212,6 @@ const SiteLogo = ( {
url={ logoUrl }
width={ currentWidth }
height={ currentHeight }
clientWidth={ clientWidth }
naturalHeight={ naturalHeight }
naturalWidth={ naturalWidth }
onSaveImage={ ( imageAttributes ) => {
Expand Down Expand Up @@ -408,7 +392,6 @@ export default function LogoEdit( {
isSelected,
} ) {
const { width, shouldSyncIcon } = attributes;
const ref = useRef();

const {
siteLogoId,
Expand Down Expand Up @@ -551,7 +534,6 @@ export default function LogoEdit( {
alt={ alt }
attributes={ attributes }
className={ className }
containerRef={ ref }
isSelected={ isSelected }
setAttributes={ setAttributes }
logoUrl={ logoUrl }
Expand Down Expand Up @@ -588,10 +570,7 @@ export default function LogoEdit( {
'is-default-size': ! width,
} );

const blockProps = useBlockProps( {
ref,
className: classes,
} );
const blockProps = useBlockProps( { className: classes } );

const label = __( 'Add a site logo' );

Expand Down

0 comments on commit e05558a

Please sign in to comment.