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

Stabilise BlockPreview props #47231

Merged
merged 4 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

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

That's not from this PR, but is there a case that we don't have styles but we do have additionalStyles?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes in theory there could be no styles attribute in the block editor settings which would mean styles is undefined here. This likely happens when the block editor is used outside of WordPress.

{
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',
noisysocks marked this conversation as resolved.
Show resolved Hide resolved
alternative: 'minHeight',
} );
}
if ( __experimentalStyles ) {
additionalStyles = __experimentalStyles;
deprecated( 'The __experimentalStyles prop of BlockPreview', {
noisysocks marked this conversation as resolved.
Show resolved Hide resolved
plugin: 'Gutenberg',
since: '15.0',
version: '15.2',
alternative: 'additionalStyles',
} );
}
if ( __experimentalPadding ) {
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
additionalStyles = [
...additionalStyles,
{ css: `body { padding: ${ __experimentalPadding }px; }` },
];
deprecated( 'The __experimentalPadding prop of BlockPreview', {
since: '6.2',
alternative: 'additionalStyles',
} );
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we follow-up and remove these props now? If you think it's still too early, consider increasing the version.


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