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

Global Styles: Add block preview component in global styles #45719

Merged
merged 4 commits into from
Dec 7, 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
2 changes: 1 addition & 1 deletion docs/reference-guides/block-api/block-registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ attributes: {

- **Type:** `Object`

Example provides structured example data for the block. This data is used to construct a preview for the block to be shown in the Inspector Help Panel when the user mouses over the block.
Example provides structured example data for the block. This data is used to construct a preview for the block to be shown in the Inspector Help Panel when the user mouses over the block and in the Styles panel when the block is selected.

The data provided in the example object should match the attributes defined. For example:

Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const settings = {
attributes: {
src: 'https://upload.wikimedia.org/wikipedia/commons/d/dd/Armstrong_Small_Step.ogg',
},
viewportWidth: 350,
madhusudhand marked this conversation as resolved.
Show resolved Hide resolved
},
transforms,
deprecated,
Expand Down
6 changes: 0 additions & 6 deletions packages/block-library/src/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ export const settings = {
content: __(
'In a village of La Mancha, the name of which I have no desire to call to mind, there lived not long since one of those gentlemen that keep a lance in the lance-rack, an old buckler, a lean hack, and a greyhound for coursing.'
),
style: {
typography: {
fontSize: 28,
},
},
dropCap: true,
},
},
__experimentalLabel( attributes, { context } ) {
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/search/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { search as icon } from '@wordpress/icons';

/**
Expand All @@ -17,7 +18,10 @@ export { metadata, name };

export const settings = {
icon,
example: {},
example: {
attributes: { buttonText: __( 'Search' ), label: __( 'Search' ) },
viewportWidth: 400,
},
variations,
edit,
};
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/site-logo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export { metadata, name };

export const settings = {
icon,
example: {},
edit,
transforms,
};
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/site-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { metadata, name };

export const settings = {
icon,
example: {},
edit,
transforms,
deprecated,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const settings = {
},
],
},
viewportWidth: 450,
},
transforms,
edit,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* WordPress dependencies
*/
import { BlockPreview } from '@wordpress/block-editor';
import { getBlockType, getBlockFromExample } from '@wordpress/blocks';
import { useResizeObserver } from '@wordpress/compose';

const BlockPreviewPanel = ( { name } ) => {
const blockExample = getBlockType( name )?.example;
const [
containerResizeListener,
{ width: containerWidth, height: containerHeight },
] = useResizeObserver();
const viewportWidth = blockExample?.viewportWidth || containerWidth;

return ! blockExample ? null : (
<div className="edit-site-global-styles__block-preview-panel">
{ containerResizeListener }

<BlockPreview
viewportWidth={ viewportWidth }
__experimentalMinHeight={ containerHeight }
blocks={ getBlockFromExample( name, blockExample ) }
/>
</div>
);
};

export default BlockPreviewPanel;
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
* WordPress dependencies
*/
import { getBlockType } from '@wordpress/blocks';
import { __experimentalSpacer as Spacer } from '@wordpress/components';

/**
* Internal dependencies
*/
import ContextMenu from './context-menu';
import ScreenHeader from './header';
import BlockPreviewPanel from './block-preview-panel';

function ScreenBlock( { name } ) {
const blockType = getBlockType( name );

return (
<>
<ScreenHeader title={ blockType.title } />
<Spacer paddingX={ 4 }>
<BlockPreviewPanel name={ name } />
</Spacer>
<ContextMenu parentMenu={ '/blocks/' + name } name={ name } />
</>
);
Expand Down
12 changes: 12 additions & 0 deletions packages/edit-site/src/components/global-styles/style.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Variables
$block-preview-height: 150px;

.edit-site-global-styles-preview {
display: flex;
align-items: center;
Expand Down Expand Up @@ -121,3 +124,12 @@
overflow: hidden;
text-overflow: ellipsis;
}

.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;
}