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 legacy widget height overflow #33191

Merged
merged 6 commits into from
Jul 5, 2021
Merged
Changes from 2 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
38 changes: 20 additions & 18 deletions packages/widgets/src/blocks/legacy-widget/edit/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,35 @@ import { Placeholder, Spinner, Disabled } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export default function Preview( { idBase, instance, isVisible } ) {
const [ iframeHeight, setIframeHeight ] = useState();
const [ isLoaded, setIsLoaded ] = useState( false );

// Resize the iframe on either the load event, or when the iframe becomes visible.
const ref = useRefEffect( ( iframe ) => {
function onChange() {
const boundingRect = iframe?.contentDocument?.body?.getBoundingClientRect();
if ( boundingRect ) {
// Include `top` in the height calculation to avoid the bottom
// of widget previews being cut-off. Most widgets have a
// heading at the top that has top margin, and the `height`
// alone doesn't take that margin into account.
setIframeHeight( boundingRect.top + boundingRect.height );
}
function setHeight() {
iframe.style.height = `${ iframe.contentDocument.documentElement.offsetHeight }px`;
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
}

const { IntersectionObserver } = iframe.ownerDocument.defaultView;

// Observe for intersections that might cause a change in the height of
// the iframe, e.g. a Widget Area becoming expanded.
const intersectionObserver = new IntersectionObserver( onChange, {
threshold: 1,
} );
const intersectionObserver = new IntersectionObserver(
( [ entry ] ) => {
if ( entry.isIntersecting ) {
setHeight();
}
},
{
threshold: 1,
}
);
intersectionObserver.observe( iframe );

iframe.addEventListener( 'load', onChange );
iframe.addEventListener( 'load', setHeight );

return () => {
iframe.removeEventListener( 'load', onChange );
intersectionObserver.disconnect();
iframe.removeEventListener( 'load', setHeight );
};
}, [] );

Expand All @@ -53,7 +54,7 @@ export default function Preview( { idBase, instance, isVisible } ) {
move the iframe off-screen instead of hiding it because web browsers
will not trigger onLoad if the iframe is hidden.
*/ }
{ isVisible && iframeHeight === null && (
{ isVisible && ! isLoaded && (
<Placeholder>
<Spinner />
</Placeholder>
Expand All @@ -62,7 +63,7 @@ export default function Preview( { idBase, instance, isVisible } ) {
className={ classnames(
'wp-block-legacy-widget__edit-preview',
{
'is-offscreen': ! isVisible || iframeHeight === null,
'is-offscreen': ! isVisible || ! isLoaded,
}
) }
>
Expand All @@ -84,7 +85,8 @@ export default function Preview( { idBase, instance, isVisible } ) {
instance,
},
} ) }
height={ iframeHeight || 100 }
onLoad={ () => setIsLoaded( true ) }
height={ 100 }
/>
</Disabled>
</div>
Expand Down