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

Fix max width observer hook for align left/right images #69364

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 12 additions & 9 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
LINK_DESTINATION_MEDIA,
LINK_DESTINATION_NONE,
ALLOWED_MEDIA_TYPES,
SIZED_LAYOUTS,
} from './constants';

export const pickRelevantMediaFiles = ( image, size ) => {
Expand Down Expand Up @@ -113,14 +114,6 @@ export function ImageEdit( {
const [ temporaryURL, setTemporaryURL ] = useState( attributes.blob );

const containerRef = useRef();
// Only observe the max width from the parent container when the parent layout is not flex nor grid.
// This won't work for them because the container width changes with the image.
// TODO: Find a way to observe the container width for flex and grid layouts.
const isMaxWidthContainerWidth =
! parentLayout ||
( parentLayout.type !== 'flex' && parentLayout.type !== 'grid' );
const [ maxWidthObserver, maxContentWidth ] = useMaxWidthObserver();

const [ placeholderResizeListener, { width: placeholderWidth } ] =
useResizeObserver();

Expand Down Expand Up @@ -363,6 +356,16 @@ export function ImageEdit( {
ref: containerRef,
className: classes,
} );
const [ maxWidthObserver, maxContentWidth ] = useMaxWidthObserver( {
className: blockProps.className,
isActive:
isSingleSelected &&
// Only observe the max width from the parent container when the parent layout
// is not flex nor grid. This won't work for them because the container width
// changes with the image.
// TODO: Find a way to observe the container width for flex and grid layouts.
( ! parentLayout || ! SIZED_LAYOUTS.includes( parentLayout.type ) ),
} );

// Much of this description is duplicated from MediaPlaceholder.
const { lockUrlControls = false, lockUrlControlsMessage } = useSelect(
Expand Down Expand Up @@ -472,7 +475,7 @@ export function ImageEdit( {
{
// The listener cannot be placed as the first element as it will break the in-between inserter.
// See https://github.com/WordPress/gutenberg/blob/71134165868298fc15e22896d0c28b41b3755ff7/packages/block-editor/src/components/block-list/use-in-between-inserter.js#L120
isSingleSelected && isMaxWidthContainerWidth && maxWidthObserver
maxWidthObserver
}
</>
);
Expand Down
30 changes: 19 additions & 11 deletions packages/block-library/src/image/use-max-width-observer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { useState } from '@wordpress/element';
import { useResizeObserver } from '@wordpress/compose';

function useMaxWidthObserver() {
const [ contentResizeListener, { width } ] = useResizeObserver();
const observerRef = useRef();
function useMaxWidthObserver( { className, isActive } ) {
const [ usedMaxWidth, setUsedMaxWidth ] = useState();
const setResizeObserved = useResizeObserver( ( [ entry ] ) => {
setUsedMaxWidth( entry.contentRect.width );
} );
if ( ! isActive ) {
return [];
}
// The block’s alignment class names need to be applied because the max-width
// may vary by that. The base `wp-block` is needed for classic themes.
const usedClassName = className
.split( ' ' )
.filter( ( name ) => /^(wp-block|align[^\b]+)$/.test( name ) )
.join( ' ' );

const maxWidthObserver = (
<div
// Some themes set max-width on blocks.
className="wp-block"
className={ usedClassName }
aria-hidden="true"
style={ {
position: 'absolute',
Expand All @@ -20,13 +30,11 @@ function useMaxWidthObserver() {
height: 0,
margin: 0,
} }
ref={ observerRef }
>
{ contentResizeListener }
</div>
ref={ setResizeObserved }
/>
);

return [ maxWidthObserver, width ];
return [ maxWidthObserver, usedMaxWidth ];
}

export { useMaxWidthObserver };
Loading