Skip to content

Commit

Permalink
Recalculate height on rerender
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende committed May 8, 2024
1 parent db88df3 commit 04b181a
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,31 @@ function Iframe( {
};
}, [] );

const windowResizeRef = useRefEffect( ( node ) => {
const nodeWindow = node.ownerDocument.defaultView;

const onResize = () => {
setIframeWindowInnerHeight( nodeWindow.innerHeight );
};
nodeWindow.addEventListener( 'resize', onResize );
return () => {
nodeWindow.removeEventListener( 'resize', onResize );
};
}, [] );

const [ iframeWindowInnerHeight, setIframeWindowInnerHeight ] = useState();

const disabledRef = useDisabled( { isDisabled: ! readonly } );
const bodyRef = useMergeRefs( [
useBubbleEvents( iframeDocument ),
contentRef,
clearerRef,
writingFlowRef,
disabledRef,
// Avoid resize listeners when not needed, these will trigger
// unnecessary re-renders when animating the iframe width, or when
// expanding preview iframes.
scale === 1 ? null : windowResizeRef,
] );

// Correct doctype is required to enable rendering in standards
Expand Down Expand Up @@ -282,7 +300,7 @@ function Iframe( {
);
iframeDocument.documentElement.style.setProperty(
'--wp-zoom-out-inner-height',
`${ iframeDocument.defaultView.innerHeight }px`
`${ iframeWindowInnerHeight }px`
);

return () => {
Expand All @@ -301,7 +319,14 @@ function Iframe( {
'--wp-zoom-out-inner-height'
);
};
}, [ scale, frameSize, iframeDocument, contentHeight, isZoomedOut ] );
}, [
scale,
frameSize,
iframeDocument,
iframeWindowInnerHeight,
contentHeight,
isZoomedOut,
] );

// Make sure to not render the before and after focusable div elements in view
// mode. They're only needed to capture focus in edit mode.
Expand Down

0 comments on commit 04b181a

Please sign in to comment.