Skip to content

Commit

Permalink
Stabilise BlockPreview props
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jan 18, 2023
1 parent f7052d3 commit 6655011
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ function BlockPatternSlide( { className, pattern, minHeight } ) {
aria-label={ title }
aria-describedby={ description ? descriptionId : undefined }
>
<BlockPreview
blocks={ blocks }
__experimentalMinHeight={ minHeight }
/>
<BlockPreview blocks={ blocks } minHeight={ minHeight } />
{ !! description && (
<VisuallyHidden id={ descriptionId }>
{ description }
Expand Down
18 changes: 9 additions & 9 deletions packages/block-editor/src/components/block-preview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ Width of the preview container in pixels. Controls at what size the blocks will

Set `viewportWidth` to `0` to make the viewport the same width as the container.

### `__experimentalPadding`
### minHeight

- **Type** `Int`
- **Default** `undefined`
Minimum height of the preview iframe in pixels.

Padding for the preview container body.
- **Type:** `Int`
- **Default:** `undefined`

### `__experimentalStyles`
### `additionalStyles`

List of additional editor styles to load into the preview iframe. Each object
should contain a `css` attribute. See `EditorStyles` for more info.

```jsx
<BlockPreview
blocks={ blocks }
__experimentalStyles={ [
{ css: '.wp-block { margin: 16px; }' },
blocks={ blocks }
additionalStyles={ [
{ css: 'body { padding: 16px; }' },
] }
/>
```

- **Type** `Int`
- **Type** `Array`
- **Default** `[]`
18 changes: 8 additions & 10 deletions packages/block-editor/src/components/block-preview/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ const MAX_HEIGHT = 2000;
function ScaledBlockPreview( {
viewportWidth,
containerWidth,
__experimentalPadding,
__experimentalMinHeight,
__experimentalStyles,
minHeight,
additionalStyles = [],
} ) {
if ( ! viewportWidth ) {
viewportWidth = containerWidth;
Expand All @@ -46,7 +45,7 @@ function ScaledBlockPreview( {
if ( styles ) {
return [
...styles,
...__experimentalStyles,
...additionalStyles,
{
css: 'body{height:auto;overflow:hidden;border:none;}',
__unstableType: 'presets',
Expand All @@ -55,7 +54,7 @@ function ScaledBlockPreview( {
}

return styles;
}, [ styles, __experimentalStyles ] );
}, [ styles, additionalStyles ] );

const svgFilters = useMemo( () => {
return [ ...( duotone?.default ?? [] ), ...( duotone?.theme ?? [] ) ];
Expand All @@ -73,7 +72,7 @@ function ScaledBlockPreview( {
height: contentHeight * scale,
maxHeight:
contentHeight > MAX_HEIGHT ? MAX_HEIGHT * scale : undefined,
minHeight: __experimentalMinHeight,
minHeight,
} }
>
<Iframe
Expand All @@ -87,7 +86,6 @@ function ScaledBlockPreview( {
);
documentElement.style.position = 'absolute';
documentElement.style.width = '100%';
bodyElement.style.padding = __experimentalPadding + 'px';

// Necessary for contentResizeListener to work.
bodyElement.style.boxSizing = 'border-box';
Expand All @@ -105,9 +103,9 @@ function ScaledBlockPreview( {
// See: https://github.com/WordPress/gutenberg/pull/38175.
maxHeight: MAX_HEIGHT,
minHeight:
scale !== 0 && scale < 1 && __experimentalMinHeight
? __experimentalMinHeight / scale
: __experimentalMinHeight,
scale !== 0 && scale < 1 && minHeight
? minHeight / scale
: minHeight,
} }
>
{ contentResizeListener }
Expand Down
42 changes: 37 additions & 5 deletions packages/block-editor/src/components/block-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import classnames from 'classnames';
import { useDisabled, useMergeRefs } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { memo, useMemo } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
Expand All @@ -20,11 +21,41 @@ import { BlockListItems } from '../block-list';

export function BlockPreview( {
blocks,
__experimentalPadding = 0,
viewportWidth = 1200,
minHeight,
additionalStyles = [],
// Deprecated props:
__experimentalMinHeight,
__experimentalStyles = [],
__experimentalStyles,
__experimentalPadding,
} ) {
if ( __experimentalMinHeight ) {
minHeight = __experimentalMinHeight;
deprecated( 'The __experimentalMinHeight prop', {
since: '6.2',
alternative: 'minHeight',
} );
}
if ( __experimentalStyles ) {
additionalStyles = __experimentalStyles;
deprecated( 'The __experimentalStyles prop of BlockPreview', {
plugin: 'Gutenberg',
since: '15.0',
version: '15.2',
alternative: 'additionalStyles',
} );
}
if ( __experimentalPadding ) {
additionalStyles = [
...additionalStyles,
{ css: `body { padding: ${ __experimentalPadding }px; }` },
];
deprecated( 'The __experimentalPadding prop of BlockPreview', {
since: '6.2',
alternative: 'additionalStyles',
} );
}

const originalSettings = useSelect(
( select ) => select( blockEditorStore ).getSettings(),
[]
Expand All @@ -37,16 +68,17 @@ export function BlockPreview( {
() => ( Array.isArray( blocks ) ? blocks : [ blocks ] ),
[ blocks ]
);

if ( ! blocks || blocks.length === 0 ) {
return null;
}

return (
<BlockEditorProvider value={ renderedBlocks } settings={ settings }>
<AutoHeightBlockPreview
viewportWidth={ viewportWidth }
__experimentalPadding={ __experimentalPadding }
__experimentalMinHeight={ __experimentalMinHeight }
__experimentalStyles={ __experimentalStyles }
minHeight={ minHeight }
additionalStyles={ additionalStyles }
/>
</BlockEditorProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ function InserterPreviewPanel( { item } ) {
{ isReusable || example ? (
<div className="block-editor-inserter__preview-content">
<BlockPreview
__experimentalPadding={ 16 }
viewportWidth={ example?.viewportWidth ?? 500 }
blocks={
example
? getBlockFromExample( name, {
Expand All @@ -36,6 +34,10 @@ function InserterPreviewPanel( { item } ) {
} )
: createBlock( name, initialAttributes )
}
viewportWidth={ example?.viewportWidth ?? 500 }
additionalStyles={ [
{ css: 'body { padding: 16px; }' },
] }
/>
</div>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const BlockPreviewPanel = ( { name, variation = '' } ) => {
<BlockPreview
blocks={ blocks }
viewportWidth={ viewportWidth }
__experimentalMinHeight={ minHeight }
__experimentalStyles={ [
minHeight={ minHeight }
additionalStyles={ [
{
css: `
body{
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/style-book/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const Example = memo( ( { title, blocks, isSelected, onClick } ) => (
<BlockPreview
blocks={ blocks }
viewportWidth={ 0 }
__experimentalStyles={ [
additionalStyles={ [
{
css:
'.wp-block:first-child { margin-top: 0; }' +
Expand Down

0 comments on commit 6655011

Please sign in to comment.