Skip to content

Commit

Permalink
fix inline preview infinite render (#47697)
Browse files Browse the repository at this point in the history
  • Loading branch information
madhusudhand authored and ntsekouras committed Feb 7, 2023
1 parent 4acaa40 commit af82a70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
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 [
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

0 comments on commit af82a70

Please sign in to comment.