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 infinite render of inline block preview #47697

Merged
merged 1 commit into from
Feb 6, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
*/
import { BlockPreview } from '@wordpress/block-editor';
import { getBlockType, getBlockFromExample } from '@wordpress/blocks';
import { useResizeObserver } from '@wordpress/compose';
import { __experimentalSpacer as Spacer } from '@wordpress/components';

const BlockPreviewPanel = ( { name, variation = '' } ) => {
const [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. why we needed useResizeObserver in the first place here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To set the min-height of the preview iframe container, so that block preview (like button) is vertically center aligned.
It was captured using useResizeObserver, but seems like it was a bad idea for non-floating scrollbars that change with and height when they appear on content overflow.

containerResizeListener,
{ width: containerWidth, height: containerHeight },
] = useResizeObserver();
const blockExample = getBlockType( name )?.example;
const blockExampleWithVariation = {
...blockExample,
Expand All @@ -25,23 +20,24 @@ const BlockPreviewPanel = ( { name, variation = '' } ) => {
name,
variation ? blockExampleWithVariation : blockExample
);
const viewportWidth = blockExample?.viewportWidth || containerWidth;
const minHeight = containerHeight;
const viewportWidth = blockExample?.viewportWidth || null;
const previewHeight = '150px';

return ! blockExample ? null : (
<Spacer marginX={ 4 } marginBottom={ 4 }>
<div className="edit-site-global-styles__block-preview-panel">
{ containerResizeListener }

<div
className="edit-site-global-styles__block-preview-panel"
style={ { maxHeight: previewHeight, boxSizing: 'initial' } }
>
<BlockPreview
blocks={ blocks }
viewportWidth={ viewportWidth }
minHeight={ minHeight }
minHeight={ previewHeight }
additionalStyles={ [
{
css: `
body{
min-height:${ minHeight }px;
min-height:${ previewHeight };
display:flex;align-items:center;justify-content:center;
}
`,
Expand Down
4 changes: 0 additions & 4 deletions packages/edit-site/src/components/global-styles/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Variables
$block-preview-height: 150px;

.edit-site-global-styles-preview {
display: flex;
align-items: center;
Expand Down Expand Up @@ -130,7 +127,6 @@ $block-preview-height: 150px;
.edit-site-global-styles__block-preview-panel {
position: relative;
width: 100%;
height: $block-preview-height + 2 * $border-width;
overflow: auto;
border: $gray-200 $border-width solid;
border-radius: $radius-block-ui;
Expand Down