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

Replace deprecated useResizeObserver for useScaleCanvas #67508

Open
wants to merge 5 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
9 changes: 4 additions & 5 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ function Iframe( {
}, [] );

const {
contentResizeListener,
containerResizeListener,
contentRef: scaleContentRef,
containerRef,
isZoomedOut,
scaleContainerWidth,
} = useScaleCanvas( {
Expand All @@ -234,6 +234,7 @@ function Iframe( {
clearerRef,
writingFlowRef,
disabledRef,
scaleContentRef,
] );

// Correct doctype is required to enable rendering in standards
Expand Down Expand Up @@ -341,7 +342,6 @@ function Iframe( {
...bodyClasses
) }
>
{ contentResizeListener }
<StyleProvider document={ iframeDocument }>
{ children }
</StyleProvider>
Expand All @@ -354,8 +354,7 @@ function Iframe( {
);

return (
<div className="block-editor-iframe__container">
{ containerResizeListener }
<div className="block-editor-iframe__container" ref={ containerRef }>
<div
className={ clsx(
'block-editor-iframe__scale-container',
Expand Down
49 changes: 38 additions & 11 deletions packages/block-editor/src/components/iframe/use-scale-canvas.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useEffect, useRef, useCallback } from '@wordpress/element';
import { useEffect, useRef, useCallback, useState } from '@wordpress/element';
import { useReducedMotion, useResizeObserver } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -132,12 +132,34 @@ function getAnimationKeyframes( transitionFrom, transitionTo ) {
];
}

/**
* @typedef {Object} ObservedSize
* @property {number|null} width The width of the observed element.
* @property {number|null} height The height of the observed element.
*/
/** @type {ObservedSize} */
const NULL_SIZE = { width: null, height: null };

/**
* Get the size of the observed element.
*
* @param {ResizeObserverEntry[]} entries Array of the new dimensions of the element after each change.
* @return {ObservedSize} Latest width and height of the observed element.
*/
function extractSize( entries ) {
const contentBoxSize = entries.at( -1 ).contentBoxSize[ 0 ];
return {
width: contentBoxSize.inlineSize,
height: contentBoxSize.blockSize,
};
}

/**
* @typedef {Object} ScaleCanvasResult
* @property {boolean} isZoomedOut A boolean indicating if the canvas is zoomed out.
* @property {number} scaleContainerWidth The width of the container used to calculate the scale.
* @property {Object} contentResizeListener A resize observer for the content.
* @property {Object} containerResizeListener A resize observer for the container.
* @property {boolean} isZoomedOut A boolean indicating if the canvas is zoomed out.
* @property {number} scaleContainerWidth The width of the container used to calculate the scale.
* @property {import('react').Ref<Element>} contentRef A callback ref to the content element.
* @property {import('react').Ref<Element>} containerRef A callback ref to the container element.
*/

/**
Expand All @@ -157,12 +179,17 @@ export function useScaleCanvas( {
maxContainerWidth = 750,
scale,
} ) {
const [ contentResizeListener, { height: contentHeight } ] =
useResizeObserver();
const [ { height: contentHeight }, setContentRect ] = useState( NULL_SIZE );
const contentRef = useResizeObserver( ( entries ) => {
setContentRect( extractSize( entries ) );
ajlende marked this conversation as resolved.
Show resolved Hide resolved
} );
const [
containerResizeListener,
{ width: containerWidth, height: containerHeight },
] = useResizeObserver();
setContainerRect,
] = useState( NULL_SIZE );
const containerRef = useResizeObserver( ( entries ) => {
setContainerRect( extractSize( entries ) );
} );

const initialContainerWidthRef = useRef( 0 );
const isZoomedOut = scale !== 1;
Expand Down Expand Up @@ -484,7 +511,7 @@ export function useScaleCanvas( {
return {
isZoomedOut,
scaleContainerWidth,
contentResizeListener,
containerResizeListener,
contentRef,
containerRef,
};
}
Loading