Skip to content
Closed
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: 13 additions & 18 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
$style .= '}';

$style .= "$selector > .alignwide { max-width: " . esc_html( $wide_max_width_value ) . ';}';
$style .= "$selector .alignfull { max-width: none; }";
}

$style .= "$selector .alignleft { float: left; margin-right: 2em; }";
$style .= "$selector .alignright { float: right; margin-left: 2em; }";
if ( $has_block_gap_support ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This dynamic part can potentially be transformed to a static style if we introduce some kind of global .has-block-gap-support classname to the root of the block editor and the frontend. Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Extracting these sort of styles to its own file (also from lib/class-wp-theme-json-gutenberg.php) will help with #35736 (comment)

$style .= "$selector > * { margin-top: 0; margin-bottom: 0; }";
$style .= "$selector > * + * { margin-top: var( --wp--style--block-gap ); margin-bottom: 0; }";
Expand All @@ -76,15 +73,12 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
'space-between' => 'space-between',
);

$style = "$selector {";
$style .= 'display: flex;';
$style = "$selector {";
if ( $has_block_gap_support ) {
$style .= 'gap: var( --wp--style--block-gap, 0.5em );';
} else {
$style .= 'gap: 0.5em;';
}
$style .= 'flex-wrap: wrap;';
$style .= 'align-items: center;';
/**
* Add this style only if is not empty for backwards compatibility,
* since we intend to convert blocks that had flex layout implemented
Expand All @@ -94,8 +88,6 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
$style .= "justify-content: {$justify_content_options[ $layout['justifyContent'] ]};";
}
$style .= '}';

$style .= "$selector > * { margin: 0; }";
}

return $style;
Expand Down Expand Up @@ -128,26 +120,29 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
$used_layout = $default_layout;
}

$id = uniqid();
$style = gutenberg_get_layout_style( ".wp-container-$id", $used_layout, $has_block_gap_support );
$id = uniqid();
$style = gutenberg_get_layout_style( ".wp-container-$id", $used_layout, $has_block_gap_support );
$layout_type = isset( $used_layout['type'] ) ? $used_layout['type'] : 'default';
// This assumes the hook only applies to blocks with a single wrapper.
// I think this is a reasonable limitation for that particular hook.
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="wp-container-' . $id . ' ',
'class="wp-layout-' . $layout_type . ' wp-container-' . $id . ' ',
$block_content,
1
);

// Ideally styles should be loaded in the head, but blocks may be parsed
// after that, so loading in the footer for now.
// See https://core.trac.wordpress.org/ticket/53494.
add_action(
'wp_footer',
function () use ( $style ) {
echo '<style>' . $style . '</style>';
}
);
if ( $style ) {
add_action(
'wp_footer',
function () use ( $style ) {
echo '<style>' . $style . '</style>';
}
);
}

return $content;
}
Expand Down
6 changes: 1 addition & 5 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,11 +997,7 @@ private function get_block_classes( $style_nodes ) {
}

if ( self::ROOT_BLOCK_SELECTOR === $selector ) {
$block_rules .= 'body { margin: 0; }';
$block_rules .= '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }';
$block_rules .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }';
$block_rules .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }';

$block_rules .= 'body { margin: 0; }';
$has_block_gap_support = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'blockGap' ) ) !== null;
if ( $has_block_gap_support ) {
$block_rules .= '.wp-site-blocks > * { margin-top: 0; margin-bottom: 0; }';
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ export const withLayoutStyles = createHigherOrderComponent(
const usedLayout = layout?.inherit
? defaultThemeLayout
: layout || defaultBlockLayout || {};
const layoutType = usedLayout.type || 'default';
const className = classnames( props?.className, {
[ `wp-layout-${ layoutType }` ]: shouldRenderLayoutStyles,
[ `wp-container-${ id }` ]: shouldRenderLayoutStyles,
} );

Expand Down
8 changes: 0 additions & 8 deletions packages/block-editor/src/layouts/flex.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,13 @@ export default {
return (
<style>{ `
${ appendSelectors( selector ) } {
display: flex;
gap: ${
hasBlockGapStylesSupport
? 'var( --wp--style--block-gap, 0.5em )'
: '0.5em'
};
flex-wrap: wrap;
align-items: center;
flex-direction: row;
justify-content: ${ justifyContent };
}

${ appendSelectors( selector, '> *' ) } {
margin: 0;
}
` }</style>
);
},
Expand Down
17 changes: 0 additions & 17 deletions packages/block-editor/src/layouts/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,9 @@ export default {
${ appendSelectors( selector, '> [data-align="wide"]' ) } {
max-width: ${ wideSize ?? contentSize };
}

${ appendSelectors( selector, '> [data-align="full"]' ) } {
max-width: none;
}
`
: '';

style += `
${ appendSelectors( selector, '> [data-align="left"]' ) } {
float: left;
margin-right: 2em;
}

${ appendSelectors( selector, '> [data-align="right"]' ) } {
float: right;
margin-left: 2em;
}

`;

if ( hasBlockGapStylesSupport ) {
style += `
${ appendSelectors( selector, '> *' ) } {
Expand Down
31 changes: 31 additions & 0 deletions packages/block-library/src/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,34 @@
width: auto;
z-index: 100000;
}

// Default Flow Layout
.wp-site-blocks > .alignfull,
.wp-layout-default > .alignfull {
max-width: none;
}

.wp-site-blocks > .alignleft,
.wp-layout-default > .alignleft {
float: left;
margin-right: 2em;
}

.wp-site-blocks > .alignright,
.wp-layout-default > .alignright {
float: right;
margin-left: 2em;
}

// Default Flex Layout
.wp-layout-flex {
display: flex;
gap: 0.5em;
flex-wrap: wrap;
align-items: center;
flex-direction: row;
}

.wp-layout-flex > * {
margin: 0;
}
24 changes: 18 additions & 6 deletions packages/block-library/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,21 @@
font-size: 42px;
}

/**
* Editor Normalization Styles
*
* These are only output in the editor, but styles here are NOT prefixed .editor-styles-wrapper.
* This allows us to create normalization styles that are easily overridden by editor styles.
*/
// Ideally these shouldn't be needed and it should rely on the
// .alignleft... classnames that are also available on the frontend.
.wp-site-blocks > [data-align="full"],
.wp-layout-default > [data-align="full"] {
max-width: none !important;
}

.wp-site-blocks > [data-align="left"],
.wp-layout-default > [data-align="left"] {
float: left;
margin-right: 2em !important;
}

.wp-site-blocks > [data-align="right"],
.wp-layout-default > [data-align="right"] {
float: right;
margin-left: 2em !important;
}
4 changes: 2 additions & 2 deletions packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export default function VisualEditor( { styles } ) {
/>
) }
{ ! isTemplateMode && (
<div className="edit-post-visual-editor__post-title-wrapper">
<div className="edit-post-visual-editor__post-title-wrapper wp-layout-default">
<PostTitle />
</div>
) }
Expand All @@ -236,7 +236,7 @@ export default function VisualEditor( { styles } ) {
className={
isTemplateMode
? 'wp-site-blocks'
: undefined
: 'wp-layout-default'
}
__experimentalLayout={ layout }
/>
Expand Down