Skip to content

Commit

Permalink
Image: Manually update resizable box state when we don't have width/h…
Browse files Browse the repository at this point in the history
…eight

When changing to a different image, we reset dimension attributes, including size props for the resizable box. But this doesn't automatically reset dimensions in the resizable box state, which is used for inline styles. So we have to do it manually.

See WordPress#23985.
  • Loading branch information
Mamaduka committed Mar 26, 2021
1 parent c62ccd8 commit 687c2ea
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default function Image( {
containerRef,
} ) {
const captionRef = useRef();
const resizableBoxRef = useRef();
const prevUrl = usePrevious( url );
const { block, currentId, image, multiImageSelection } = useSelect(
( select ) => {
Expand Down Expand Up @@ -177,6 +178,17 @@ export default function Image( {
}
}, [ url, prevUrl ] );

// Set resizable box dimensions to "auto" when we don't have width/height.
// This is done to reset inline styles after changing to a different image.
useEffect( () => {
if ( url && resizableBoxRef.current && ! width && ! height ) {
resizableBoxRef.current.updateSize( {
width: 'auto',
height: 'auto',
} );
}
}, [ url, width, height ] );

function onResizeStart() {
toggleSelection( false );
}
Expand Down Expand Up @@ -510,6 +522,7 @@ export default function Image( {

img = (
<ResizableBox
ref={ resizableBoxRef }
size={ { width, height } }
showHandle={ isSelected }
minWidth={ minWidth }
Expand Down

0 comments on commit 687c2ea

Please sign in to comment.