Skip to content

Commit

Permalink
add block preview in nested styles panels
Browse files Browse the repository at this point in the history
  • Loading branch information
madhusudhand committed Dec 19, 2022
1 parent ded9354 commit c6aa163
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
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 }>
<div className="edit-site-global-styles__block-preview-panel">
{ containerResizeListener }

<BlockPreview
viewportWidth={ viewportWidth }
__experimentalMinHeight={ containerHeight }
blocks={ getBlockFromExample( name, blockExample ) }
/>
</div>
<BlockPreview
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

0 comments on commit c6aa163

Please sign in to comment.