Skip to content

Commit

Permalink
Minimize class names applied and avoid unneeded execution
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Feb 28, 2025
1 parent 7d0f227 commit ce74c36
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
20 changes: 11 additions & 9 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,16 @@ export function ImageEdit( {
ref: containerRef,
className: classes,
} );
// 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 || ! SIZED_LAYOUTS.includes( parentLayout.type );
const [ maxWidthObserver, maxContentWidth ] = useMaxWidthObserver(
blockProps.className
);
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 @@ -473,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
15 changes: 11 additions & 4 deletions packages/block-library/src/image/use-max-width-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
import { useState } from '@wordpress/element';
import { useResizeObserver } from '@wordpress/compose';

function useMaxWidthObserver( className ) {
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
// The block’s class names need to be applied because the max-width
// may vary per theme styles (e.g. alignments).
className={ className }
className={ usedClassName }
aria-hidden="true"
style={ {
position: 'absolute',
Expand Down

0 comments on commit ce74c36

Please sign in to comment.