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

Inline block preview: add preview block in nested global style panels #46401

Merged
merged 1 commit into from
Dec 19, 2022
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 @@ -4,25 +4,30 @@
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 } ) => {
const blockExample = getBlockType( name )?.example;
const [
containerResizeListener,
{ width: containerWidth, height: containerHeight },
] = useResizeObserver();
const blockExample = getBlockType( name )?.example;
const blocks = blockExample && getBlockFromExample( name, blockExample );
const viewportWidth = blockExample?.viewportWidth || containerWidth;
const minHeight = containerHeight;

return ! blockExample ? null : (
<div className="edit-site-global-styles__block-preview-panel">
{ containerResizeListener }
<Spacer marginX={ 4 } marginBottom={ 4 }>
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
<div className="edit-site-global-styles__block-preview-panel">
{ containerResizeListener }

<BlockPreview
viewportWidth={ viewportWidth }
__experimentalMinHeight={ containerHeight }
blocks={ getBlockFromExample( name, blockExample ) }
/>
</div>
<BlockPreview
Copy link
Member Author

Choose a reason for hiding this comment

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

BlockPreview uses memo, I was hoping the preview to not re-render on forward and back navigation.
However, it still renders.
Also tried wrapping BlockPreviewPanel component with memo, yet it re-renders.
Please suggest.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm.. is it re-rendering because the clientId of the elements is changing? I guess memo should stop this happening, but it still re-renders?

Copy link
Member

Choose a reason for hiding this comment

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

You can't avoid re-renders unless you're using the same component instance. So you'd need the same <BlockPreview /> call for all the previews.

viewportWidth={ viewportWidth }
__experimentalMinHeight={ minHeight }
blocks={ blocks }
/>
</div>
</Spacer>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { getBlockType } from '@wordpress/blocks';
import { __experimentalSpacer as Spacer } from '@wordpress/components';

/**
* Internal dependencies
Expand All @@ -17,9 +16,7 @@ function ScreenBlock( { name } ) {
return (
<>
<ScreenHeader title={ blockType.title } />
<Spacer paddingX={ 4 }>
<BlockPreviewPanel name={ name } />
</Spacer>
<BlockPreviewPanel name={ name } />
<ContextMenu
parentMenu={ '/blocks/' + encodeURIComponent( name ) }
name={ name }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { __ } from '@wordpress/i18n';
*/
import ScreenHeader from './header';
import BorderPanel, { useHasBorderPanel } from './border-panel';
import BlockPreviewPanel from './block-preview-panel';

function ScreenBorder( { name } ) {
const hasBorderPanel = useHasBorderPanel( name );

return (
<>
<ScreenHeader title={ __( 'Border' ) } />
<BlockPreviewPanel name={ name } />
{ hasBorderPanel && <BorderPanel name={ name } /> }
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { NavigationButtonAsItem } from './navigation-button';
import { getSupportedGlobalStylesPanels, useStyle } from './hooks';
import Subtitle from './subtitle';
import ColorIndicatorWrapper from './color-indicator-wrapper';
import BlockPreviewPanel from './block-preview-panel';

function BackgroundColorItem( { name, parentMenu } ) {
const supports = getSupportedGlobalStylesPanels( name );
Expand Down Expand Up @@ -186,6 +187,8 @@ function ScreenColors( { name } ) {
) }
/>

<BlockPreviewPanel name={ name } />

<div className="edit-site-global-styles-screen-colors">
<VStack spacing={ 10 }>
<Palette name={ name } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { __ } from '@wordpress/i18n';
*/
import DimensionsPanel, { useHasDimensionsPanel } from './dimensions-panel';
import ScreenHeader from './header';
import BlockPreviewPanel from './block-preview-panel';

function ScreenLayout( { name } ) {
const hasDimensionsPanel = useHasDimensionsPanel( name );

return (
<>
<ScreenHeader title={ __( 'Layout' ) } />
<BlockPreviewPanel name={ name } />
{ hasDimensionsPanel && <DimensionsPanel name={ name } /> }
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { NavigationButtonAsItem } from './navigation-button';
import { useStyle } from './hooks';
import Subtitle from './subtitle';
import TypographyPanel from './typography-panel';
import BlockPreviewPanel from './block-preview-panel';

function Item( { name, parentMenu, element, label } ) {
const hasSupport = ! name;
Expand Down Expand Up @@ -87,6 +88,8 @@ function ScreenTypography( { name } ) {
) }
/>

<BlockPreviewPanel name={ name } />

{ ! name && (
<div className="edit-site-global-styles-screen-typography">
<VStack spacing={ 3 }>
Expand Down