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

Apply same styles to block previews on inserter and Global Styles #63177

Merged
merged 14 commits into from
Jul 11, 2024
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
31 changes: 27 additions & 4 deletions packages/block-editor/src/components/inserter/preview-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ function InserterPreviewPanel( { item } ) {
innerBlocks: example.innerBlocks,
} );
}, [ name, example, initialAttributes ] );
// Same as height of BlockPreviewPanel.
const previewHeight = 144;
const sidebarWidth = 280;
const viewportWidth = example?.viewportWidth ?? 500;
const scale = sidebarWidth / viewportWidth;
const minHeight =
scale !== 0 && scale < 1 && previewHeight
? previewHeight / scale
: previewHeight;

return (
<div className="block-editor-inserter__preview-container">
Expand All @@ -38,10 +47,24 @@ function InserterPreviewPanel( { item } ) {
<div className="block-editor-inserter__preview-content">
<BlockPreview
blocks={ blocks }
viewportWidth={ example?.viewportWidth ?? 500 }
additionalStyles={ [
{ css: 'body { padding: 24px; }' },
] }
viewportWidth={ viewportWidth }
minHeight={ previewHeight }
additionalStyles={
//We want this CSS to be in sync with the one in BlockPreviewPanel.
[
{
css: `
body {
padding: 24px;
min-height:${ Math.round( minHeight ) }px;
display:flex;
align-items:center;
}
MaggieCabrera marked this conversation as resolved.
Show resolved Hide resolved
.is-root-container { width: 100%; }
`,
},
]
}
/>
</div>
) : (
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/src/components/inserter/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ $block-inserter-tabs-height: 44px;
}

.block-editor-inserter__preview-content {
min-height: $grid-unit-60 * 3;
background: $gray-100;
display: grid;
flex-grow: 1;
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/buttons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export { metadata, name };
export const settings = {
icon,
example: {
attributes: {
layout: {
type: 'flex',
justifyContent: 'center',
},
},
innerBlocks: [
{
name: 'core/button',
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const settings = {
attributes: {
content: __( 'Code is Poetry' ),
level: 2,
textAlign: 'center',
},
},
__experimentalLabel( attributes, { context } ) {
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/site-tagline/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
"default": 0
}
},
"example": {},
"example": {
"viewportWidth": 350,
"attributes": {
"textAlign": "center"
}
},
"supports": {
"align": [ "wide", "full" ],
"html": false,
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/site-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export { metadata, name };

export const settings = {
icon,
example: {},
example: {
viewportWidth: 350,
attributes: {
textAlign: 'center',
},
},
edit,
transforms,
deprecated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ const BlockPreviewPanel = ( { name, variation = '' } ) => {
return getBlockFromExample( name, example );
}, [ name, blockExample, variation ] );

const viewportWidth = blockExample?.viewportWidth ?? null;
const previewHeight = 150;
const viewportWidth = blockExample?.viewportWidth ?? 500;
// Same as height of InserterPreviewPanel.
const previewHeight = 144;

if ( ! blockExample ) {
return null;
Expand All @@ -49,16 +50,22 @@ const BlockPreviewPanel = ( { name, variation = '' } ) => {
blocks={ blocks }
viewportWidth={ viewportWidth }
minHeight={ previewHeight }
additionalStyles={ [
{
css: `
additionalStyles={
//We want this CSS to be in sync with the one in InserterPreviewPanel.
[
{
css: `
body{
min-height:${ previewHeight }px;
display:flex;align-items:center;justify-content:center;
padding: 24px;
MaggieCabrera marked this conversation as resolved.
Show resolved Hide resolved
min-height:100%;
display:flex;
align-items:center;
}
.is-root-container { width: 100%; }
`,
},
] }
},
]
}
/>
</div>
</Spacer>
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/global-styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
}

.edit-site-global-styles__shadow-preview-panel {
height: 150px;
height: $grid-unit-60 * 3;
border: $gray-200 $border-width solid;
border-radius: $radius-block-ui;
overflow: auto;
Expand Down
Loading