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

Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json #40875

Conversation

andrewserong
Copy link
Contributor

@andrewserong andrewserong commented May 6, 2022

What?

Refactor the Layout block support to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.

Note: This is intentionally a complex PR that attempts to make many changes to the Layout support all at once. Unfortunately, for the changes proposed, I don't think there's an easy way to split it out into smaller changes, as it rethinks how the Layout block support works internally, by shifting a fair bit of logic to global styles.


Fixes #39789, part of #39336, and is a step forward toward addressing duplicate layout styles as in #41434 and using semantic classnames to link Layout styles as raised in #38719. This PR explores an idea from #39870 (comment) as a (potential) alternative to #39870:

  • Consolidate Layout definitions in a central place in theme.json, used for generating styles in both the editor and on the site front-end
  • Render base Layout styles in global styles for each of the layout types (default/flow and flex layouts)
  • Inject a semantic classname for the Layout type (is-layout-flow and is-layout-flex)
  • Update the Layout block support to include outputting blockGap values at the block level within theme.json

The original goal of this PR is to see if we can support setting blockGap at the block level in theme.json and output styles that correctly target blocks that opt-in to the layout block support. While we're at it, we can progress a few different ideas we'd like to address in the layout support:

  • Use classnames and direct values for blockGap support at the block level in global styles / theme.json, reducing the reliance upon the block gap CSS variable.
  • Centralise the definitions of each layout type, the base styling rules, and the gap rules, so that the definitions can be shared between the editor and site frontend.
  • Begin to add in semantic classnames that group the styling rules for layout.
  • Begin to de-dupe wp-container-$id classnames and style tags — in this PR we start to do this by outputting base styles at the root .is-layout-flow and .is-layout-flex rules, so that the individual layout rules that we generate are just for those blocks that deviate from the base styling / settings for each layout type.

Why?

The problem this PR initially set out to solve was to allow defining blockGap values at the block level within theme.json. However, since this involves a decently sized change to how the layout support works, it's a good opportunity to re-think a few foundational things about how the layout support works, and progress some of those areas forward to address some long-standing issues.

The overall goal is to reduce duplication, consolidate how things work, and set us up for follow-up PRs where we can add in remaining semantic classnames, and hopefully make it easier to add additional layout types in the future.

How?

  • Add a set of layout definitions in theme.json so that we can start to define layouts declaratively
  • Add a set out base styling rules that are output only once, in global styles
  • Add classnames to the layout support to link the particular layout type ( .is-layout-flow, .is-layout-flex )
  • In the theme JSON class (and in global styles output in the site editor), add logic that iterates over the layout definitions for each individual block that is handled, and use those definitions to construct the appropriate blockGap rule, for example:
  • When rendering the Layout block support at the individual block level, only inject a wp-container-$id classname and style tag if the block contains non-default Layout values (this results in fewer style tags being output)
.wp-block-buttons.is-layout-flex {
  gap: 5px 20px;
}

Testing Instructions

Update the theme.json for your theme to specify a blockGap value for a particular block. E.g.

	"styles": {
		"blocks": {
			"core/buttons": {
				"spacing": {
					"blockGap": "6px"
				}
			}
		}
	},

On the front end of your site, the block-level styles should take effect.

Open up the site editor, and within global styles, go to update the block-level block spacing for blocks like Buttons and Social Icons. It should work as expected.

Check that tests pass, e.g.:

npm run test-unit -- --testPathPattern packages/block-editor/src/layouts/test
npm run test-unit -- --testPathPattern packages/edit-site/src/components/global-styles
npm run test-unit-php /var/www/html/wp-content/plugins/gutenberg/phpunit/class-wp-theme-json-test.php

Examples to test that the Layout support functions as expected

  1. In a blocks-based theme, set settings.spacing.blockGap to true in theme.json, and in global styles in the site editor, you should be able to now set a gap value for blocks such as Social Icons or Columns.
  2. In a blocks-based theme, set settings.spacing.blockGap to null in theme.json, and ensure that the default flex gap styles for blocks like Social Icons and Columns works as expected.
  3. In a Classic theme that (such as Twenty Twenty One), the flex gap styles for blocks like Social Icons and Columns should work as expected (e.g. 0.5em default gap for Social Icons, 2em default gap for Columns).

Screenshots or screencast

Source view Front end of the site
image image
Before (duplicate style tags for blocks that only use default settings) After (note the majority of duplicate style tags have been removed — there is still more to go, to be addressed in follow-ups)
image image

@andrewserong andrewserong added [Status] In Progress Tracking issues with work in progress [Type] Experimental Experimental feature or API. labels May 6, 2022
@andrewserong andrewserong self-assigned this May 6, 2022
@andrewserong andrewserong added the Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json label May 6, 2022
@andrewserong
Copy link
Contributor Author

andrewserong commented May 9, 2022

In 69893f5 I've started to explore implementing this in the editor and quickly ran into an issue with specificity — currently the .wp-block-buttons.is-layout-flex wins over .wp-container-1, so that the block-level in theme.json styles take precedence. However, we want the real value at the individual block level to win out.

If we then up the specificity of the block level so that it also includes the block className (e.g. .wp-block-buttons.wp-container-1), that superficially fixes things when there's a style set for the individual block. However, if we remove that value, then the fallback styles for gap win out.

So, I think this means that for this implementation to work, we might need to explore one of the following options:

  1. The higher specificity of the individual block only occurs if it really has a value (e.g. we could force !important to be added — however this could be a deal breaker given that folks are wanting to remove usage of !important elsewhere).
  2. Or, perhaps we look at replacing how the CSS variable approach works for the fallback root level value. Maybe the block-level class based approach we're going with is enough so that we can have that fallback generated elsewhere (e.g. in theme.json via the presets-like approach).

So, this solution / approach might need to incorporate the some of the ideas from the presets approach in #39708 in order to be viable (it might be a neater approach overall if we can consolidate a bit).

I'll continue hacking away at this over the coming days.

@github-actions
Copy link

github-actions bot commented May 9, 2022

Size Change: +883 B (0%)

Total Size: 1.25 MB

Filename Size Change
build/block-editor/index.min.js 153 kB +193 B (0%)
build/block-editor/style-rtl.css 14.5 kB -7 B (0%)
build/block-editor/style.css 14.5 kB -6 B (0%)
build/block-library/index.min.js 183 kB -285 B (0%)
build/blocks/index.min.js 47.1 kB +2 B (0%)
build/components/index.min.js 230 kB +62 B (0%)
build/components/style-rtl.css 14 kB +38 B (0%)
build/components/style.css 14 kB +39 B (0%)
build/edit-navigation/style-rtl.css 4.02 kB -9 B (0%)
build/edit-navigation/style.css 4.03 kB -9 B (0%)
build/edit-post/index.min.js 30.5 kB +29 B (0%)
build/edit-post/style-rtl.css 6.97 kB -58 B (-1%)
build/edit-post/style.css 6.97 kB -58 B (-1%)
build/edit-site/index.min.js 52.9 kB +876 B (+2%)
build/edit-site/style-rtl.css 8.23 kB -51 B (-1%)
build/edit-site/style.css 8.21 kB -50 B (-1%)
build/edit-widgets/style-rtl.css 4.35 kB -11 B (0%)
build/edit-widgets/style.css 4.35 kB -11 B (0%)
build/editor/index.min.js 39.4 kB +178 B (0%)
build/media-utils/index.min.js 2.93 kB +21 B (+1%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 982 B
build/annotations/index.min.js 2.76 kB
build/api-fetch/index.min.js 2.26 kB
build/autop/index.min.js 2.14 kB
build/blob/index.min.js 475 B
build/block-directory/index.min.js 6.58 kB
build/block-directory/style-rtl.css 990 B
build/block-directory/style.css 991 B
build/block-editor/default-editor-styles-rtl.css 378 B
build/block-editor/default-editor-styles.css 378 B
build/block-library/blocks/archives/editor-rtl.css 61 B
build/block-library/blocks/archives/editor.css 60 B
build/block-library/blocks/archives/style-rtl.css 65 B
build/block-library/blocks/archives/style.css 65 B
build/block-library/blocks/audio/editor-rtl.css 150 B
build/block-library/blocks/audio/editor.css 150 B
build/block-library/blocks/audio/style-rtl.css 103 B
build/block-library/blocks/audio/style.css 103 B
build/block-library/blocks/audio/theme-rtl.css 110 B
build/block-library/blocks/audio/theme.css 110 B
build/block-library/blocks/avatar/editor-rtl.css 116 B
build/block-library/blocks/avatar/editor.css 116 B
build/block-library/blocks/avatar/style-rtl.css 59 B
build/block-library/blocks/avatar/style.css 59 B
build/block-library/blocks/block/editor-rtl.css 161 B
build/block-library/blocks/block/editor.css 161 B
build/block-library/blocks/button/editor-rtl.css 441 B
build/block-library/blocks/button/editor.css 441 B
build/block-library/blocks/button/style-rtl.css 543 B
build/block-library/blocks/button/style.css 543 B
build/block-library/blocks/buttons/editor-rtl.css 292 B
build/block-library/blocks/buttons/editor.css 292 B
build/block-library/blocks/buttons/style-rtl.css 275 B
build/block-library/blocks/buttons/style.css 275 B
build/block-library/blocks/calendar/style-rtl.css 207 B
build/block-library/blocks/calendar/style.css 207 B
build/block-library/blocks/categories/editor-rtl.css 84 B
build/block-library/blocks/categories/editor.css 83 B
build/block-library/blocks/categories/style-rtl.css 79 B
build/block-library/blocks/categories/style.css 79 B
build/block-library/blocks/code/style-rtl.css 103 B
build/block-library/blocks/code/style.css 103 B
build/block-library/blocks/code/theme-rtl.css 124 B
build/block-library/blocks/code/theme.css 124 B
build/block-library/blocks/columns/editor-rtl.css 108 B
build/block-library/blocks/columns/editor.css 108 B
build/block-library/blocks/columns/style-rtl.css 406 B
build/block-library/blocks/columns/style.css 406 B
build/block-library/blocks/comment-author-avatar/editor-rtl.css 125 B
build/block-library/blocks/comment-author-avatar/editor.css 125 B
build/block-library/blocks/comment-content/style-rtl.css 92 B
build/block-library/blocks/comment-content/style.css 92 B
build/block-library/blocks/comment-template/style-rtl.css 187 B
build/block-library/blocks/comment-template/style.css 185 B
build/block-library/blocks/comments-pagination-numbers/editor-rtl.css 123 B
build/block-library/blocks/comments-pagination-numbers/editor.css 121 B
build/block-library/blocks/comments-pagination/editor-rtl.css 222 B
build/block-library/blocks/comments-pagination/editor.css 209 B
build/block-library/blocks/comments-pagination/style-rtl.css 235 B
build/block-library/blocks/comments-pagination/style.css 231 B
build/block-library/blocks/comments-title/editor-rtl.css 75 B
build/block-library/blocks/comments-title/editor.css 75 B
build/block-library/blocks/comments/editor-rtl.css 95 B
build/block-library/blocks/comments/editor.css 95 B
build/block-library/blocks/cover/editor-rtl.css 615 B
build/block-library/blocks/cover/editor.css 616 B
build/block-library/blocks/cover/style-rtl.css 1.55 kB
build/block-library/blocks/cover/style.css 1.55 kB
build/block-library/blocks/embed/editor-rtl.css 293 B
build/block-library/blocks/embed/editor.css 293 B
build/block-library/blocks/embed/style-rtl.css 410 B
build/block-library/blocks/embed/style.css 410 B
build/block-library/blocks/embed/theme-rtl.css 110 B
build/block-library/blocks/embed/theme.css 110 B
build/block-library/blocks/file/editor-rtl.css 300 B
build/block-library/blocks/file/editor.css 300 B
build/block-library/blocks/file/style-rtl.css 253 B
build/block-library/blocks/file/style.css 254 B
build/block-library/blocks/file/view.min.js 346 B
build/block-library/blocks/freeform/editor-rtl.css 2.44 kB
build/block-library/blocks/freeform/editor.css 2.44 kB
build/block-library/blocks/gallery/editor-rtl.css 948 B
build/block-library/blocks/gallery/editor.css 950 B
build/block-library/blocks/gallery/style-rtl.css 1.5 kB
build/block-library/blocks/gallery/style.css 1.49 kB
build/block-library/blocks/gallery/theme-rtl.css 108 B
build/block-library/blocks/gallery/theme.css 108 B
build/block-library/blocks/group/editor-rtl.css 333 B
build/block-library/blocks/group/editor.css 333 B
build/block-library/blocks/group/style-rtl.css 57 B
build/block-library/blocks/group/style.css 57 B
build/block-library/blocks/group/theme-rtl.css 78 B
build/block-library/blocks/group/theme.css 78 B
build/block-library/blocks/heading/style-rtl.css 76 B
build/block-library/blocks/heading/style.css 76 B
build/block-library/blocks/html/editor-rtl.css 327 B
build/block-library/blocks/html/editor.css 329 B
build/block-library/blocks/image/editor-rtl.css 738 B
build/block-library/blocks/image/editor.css 737 B
build/block-library/blocks/image/style-rtl.css 524 B
build/block-library/blocks/image/style.css 530 B
build/block-library/blocks/image/theme-rtl.css 110 B
build/block-library/blocks/image/theme.css 110 B
build/block-library/blocks/latest-comments/style-rtl.css 284 B
build/block-library/blocks/latest-comments/style.css 284 B
build/block-library/blocks/latest-posts/editor-rtl.css 199 B
build/block-library/blocks/latest-posts/editor.css 198 B
build/block-library/blocks/latest-posts/style-rtl.css 463 B
build/block-library/blocks/latest-posts/style.css 462 B
build/block-library/blocks/list/style-rtl.css 88 B
build/block-library/blocks/list/style.css 88 B
build/block-library/blocks/media-text/editor-rtl.css 266 B
build/block-library/blocks/media-text/editor.css 263 B
build/block-library/blocks/media-text/style-rtl.css 493 B
build/block-library/blocks/media-text/style.css 490 B
build/block-library/blocks/more/editor-rtl.css 431 B
build/block-library/blocks/more/editor.css 431 B
build/block-library/blocks/navigation-link/editor-rtl.css 705 B
build/block-library/blocks/navigation-link/editor.css 703 B
build/block-library/blocks/navigation-link/style-rtl.css 115 B
build/block-library/blocks/navigation-link/style.css 115 B
build/block-library/blocks/navigation-submenu/editor-rtl.css 296 B
build/block-library/blocks/navigation-submenu/editor.css 295 B
build/block-library/blocks/navigation-submenu/view.min.js 402 B
build/block-library/blocks/navigation/editor-rtl.css 2.03 kB
build/block-library/blocks/navigation/editor.css 2.04 kB
build/block-library/blocks/navigation/style-rtl.css 1.96 kB
build/block-library/blocks/navigation/style.css 1.95 kB
build/block-library/blocks/navigation/view-modal.min.js 2.78 kB
build/block-library/blocks/navigation/view.min.js 423 B
build/block-library/blocks/nextpage/editor-rtl.css 395 B
build/block-library/blocks/nextpage/editor.css 395 B
build/block-library/blocks/page-list/editor-rtl.css 363 B
build/block-library/blocks/page-list/editor.css 363 B
build/block-library/blocks/page-list/style-rtl.css 175 B
build/block-library/blocks/page-list/style.css 175 B
build/block-library/blocks/paragraph/editor-rtl.css 157 B
build/block-library/blocks/paragraph/editor.css 157 B
build/block-library/blocks/paragraph/style-rtl.css 260 B
build/block-library/blocks/paragraph/style.css 260 B
build/block-library/blocks/post-author/style-rtl.css 175 B
build/block-library/blocks/post-author/style.css 176 B
build/block-library/blocks/post-comments-form/editor-rtl.css 96 B
build/block-library/blocks/post-comments-form/editor.css 96 B
build/block-library/blocks/post-comments-form/style-rtl.css 495 B
build/block-library/blocks/post-comments-form/style.css 495 B
build/block-library/blocks/post-comments/editor-rtl.css 77 B
build/block-library/blocks/post-comments/editor.css 77 B
build/block-library/blocks/post-comments/style-rtl.css 632 B
build/block-library/blocks/post-comments/style.css 630 B
build/block-library/blocks/post-excerpt/editor-rtl.css 73 B
build/block-library/blocks/post-excerpt/editor.css 73 B
build/block-library/blocks/post-excerpt/style-rtl.css 69 B
build/block-library/blocks/post-excerpt/style.css 69 B
build/block-library/blocks/post-featured-image/editor-rtl.css 605 B
build/block-library/blocks/post-featured-image/editor.css 605 B
build/block-library/blocks/post-featured-image/style-rtl.css 153 B
build/block-library/blocks/post-featured-image/style.css 153 B
build/block-library/blocks/post-template/editor-rtl.css 99 B
build/block-library/blocks/post-template/editor.css 98 B
build/block-library/blocks/post-template/style-rtl.css 282 B
build/block-library/blocks/post-template/style.css 282 B
build/block-library/blocks/post-terms/style-rtl.css 73 B
build/block-library/blocks/post-terms/style.css 73 B
build/block-library/blocks/post-title/style-rtl.css 80 B
build/block-library/blocks/post-title/style.css 80 B
build/block-library/blocks/preformatted/style-rtl.css 103 B
build/block-library/blocks/preformatted/style.css 103 B
build/block-library/blocks/pullquote/editor-rtl.css 198 B
build/block-library/blocks/pullquote/editor.css 198 B
build/block-library/blocks/pullquote/style-rtl.css 370 B
build/block-library/blocks/pullquote/style.css 370 B
build/block-library/blocks/pullquote/theme-rtl.css 167 B
build/block-library/blocks/pullquote/theme.css 167 B
build/block-library/blocks/query-pagination-numbers/editor-rtl.css 122 B
build/block-library/blocks/query-pagination-numbers/editor.css 121 B
build/block-library/blocks/query-pagination/editor-rtl.css 221 B
build/block-library/blocks/query-pagination/editor.css 211 B
build/block-library/blocks/query-pagination/style-rtl.css 234 B
build/block-library/blocks/query-pagination/style.css 231 B
build/block-library/blocks/query/editor-rtl.css 369 B
build/block-library/blocks/query/editor.css 369 B
build/block-library/blocks/quote/style-rtl.css 213 B
build/block-library/blocks/quote/style.css 213 B
build/block-library/blocks/quote/theme-rtl.css 223 B
build/block-library/blocks/quote/theme.css 226 B
build/block-library/blocks/read-more/style-rtl.css 132 B
build/block-library/blocks/read-more/style.css 132 B
build/block-library/blocks/rss/editor-rtl.css 202 B
build/block-library/blocks/rss/editor.css 204 B
build/block-library/blocks/rss/style-rtl.css 289 B
build/block-library/blocks/rss/style.css 288 B
build/block-library/blocks/search/editor-rtl.css 165 B
build/block-library/blocks/search/editor.css 165 B
build/block-library/blocks/search/style-rtl.css 385 B
build/block-library/blocks/search/style.css 386 B
build/block-library/blocks/search/theme-rtl.css 114 B
build/block-library/blocks/search/theme.css 114 B
build/block-library/blocks/separator/editor-rtl.css 146 B
build/block-library/blocks/separator/editor.css 146 B
build/block-library/blocks/separator/style-rtl.css 233 B
build/block-library/blocks/separator/style.css 233 B
build/block-library/blocks/separator/theme-rtl.css 194 B
build/block-library/blocks/separator/theme.css 194 B
build/block-library/blocks/shortcode/editor-rtl.css 464 B
build/block-library/blocks/shortcode/editor.css 464 B
build/block-library/blocks/site-logo/editor-rtl.css 708 B
build/block-library/blocks/site-logo/editor.css 708 B
build/block-library/blocks/site-logo/style-rtl.css 192 B
build/block-library/blocks/site-logo/style.css 192 B
build/block-library/blocks/site-tagline/editor-rtl.css 86 B
build/block-library/blocks/site-tagline/editor.css 86 B
build/block-library/blocks/site-title/editor-rtl.css 84 B
build/block-library/blocks/site-title/editor.css 84 B
build/block-library/blocks/social-link/editor-rtl.css 177 B
build/block-library/blocks/social-link/editor.css 177 B
build/block-library/blocks/social-links/editor-rtl.css 674 B
build/block-library/blocks/social-links/editor.css 673 B
build/block-library/blocks/social-links/style-rtl.css 1.37 kB
build/block-library/blocks/social-links/style.css 1.36 kB
build/block-library/blocks/spacer/editor-rtl.css 322 B
build/block-library/blocks/spacer/editor.css 322 B
build/block-library/blocks/spacer/style-rtl.css 48 B
build/block-library/blocks/spacer/style.css 48 B
build/block-library/blocks/table/editor-rtl.css 494 B
build/block-library/blocks/table/editor.css 494 B
build/block-library/blocks/table/style-rtl.css 611 B
build/block-library/blocks/table/style.css 609 B
build/block-library/blocks/table/theme-rtl.css 175 B
build/block-library/blocks/table/theme.css 175 B
build/block-library/blocks/tag-cloud/style-rtl.css 226 B
build/block-library/blocks/tag-cloud/style.css 227 B
build/block-library/blocks/template-part/editor-rtl.css 149 B
build/block-library/blocks/template-part/editor.css 149 B
build/block-library/blocks/template-part/theme-rtl.css 101 B
build/block-library/blocks/template-part/theme.css 101 B
build/block-library/blocks/text-columns/editor-rtl.css 95 B
build/block-library/blocks/text-columns/editor.css 95 B
build/block-library/blocks/text-columns/style-rtl.css 166 B
build/block-library/blocks/text-columns/style.css 166 B
build/block-library/blocks/verse/style-rtl.css 87 B
build/block-library/blocks/verse/style.css 87 B
build/block-library/blocks/video/editor-rtl.css 561 B
build/block-library/blocks/video/editor.css 563 B
build/block-library/blocks/video/style-rtl.css 159 B
build/block-library/blocks/video/style.css 159 B
build/block-library/blocks/video/theme-rtl.css 110 B
build/block-library/blocks/video/theme.css 110 B
build/block-library/common-rtl.css 987 B
build/block-library/common.css 984 B
build/block-library/editor-rtl.css 10.2 kB
build/block-library/editor.css 10.2 kB
build/block-library/elements-rtl.css 54 B
build/block-library/elements.css 54 B
build/block-library/reset-rtl.css 478 B
build/block-library/reset.css 478 B
build/block-library/style-rtl.css 11.5 kB
build/block-library/style.css 11.5 kB
build/block-library/theme-rtl.css 695 B
build/block-library/theme.css 700 B
build/block-serialization-default-parser/index.min.js 1.11 kB
build/block-serialization-spec-parser/index.min.js 2.83 kB
build/compose/index.min.js 11.7 kB
build/core-data/index.min.js 14.7 kB
build/customize-widgets/index.min.js 11.2 kB
build/customize-widgets/style-rtl.css 1.4 kB
build/customize-widgets/style.css 1.4 kB
build/data-controls/index.min.js 653 B
build/data/index.min.js 7.95 kB
build/date/index.min.js 32 kB
build/deprecated/index.min.js 507 B
build/dom-ready/index.min.js 324 B
build/dom/index.min.js 4.65 kB
build/edit-navigation/index.min.js 16 kB
build/edit-post/classic-rtl.css 546 B
build/edit-post/classic.css 547 B
build/edit-widgets/index.min.js 16.5 kB
build/editor/style-rtl.css 3.65 kB
build/editor/style.css 3.65 kB
build/element/index.min.js 4.27 kB
build/escape-html/index.min.js 537 B
build/format-library/index.min.js 6.75 kB
build/format-library/style-rtl.css 571 B
build/format-library/style.css 571 B
build/hooks/index.min.js 1.64 kB
build/html-entities/index.min.js 448 B
build/i18n/index.min.js 3.77 kB
build/is-shallow-equal/index.min.js 527 B
build/keyboard-shortcuts/index.min.js 1.78 kB
build/keycodes/index.min.js 1.38 kB
build/list-reusable-blocks/index.min.js 1.74 kB
build/list-reusable-blocks/style-rtl.css 835 B
build/list-reusable-blocks/style.css 835 B
build/notices/index.min.js 953 B
build/nux/index.min.js 2.05 kB
build/nux/style-rtl.css 732 B
build/nux/style.css 728 B
build/plugins/index.min.js 1.94 kB
build/preferences-persistence/index.min.js 2.22 kB
build/preferences/index.min.js 1.3 kB
build/primitives/index.min.js 933 B
build/priority-queue/index.min.js 612 B
build/react-i18n/index.min.js 696 B
build/react-refresh-entry/index.min.js 8.44 kB
build/react-refresh-runtime/index.min.js 7.31 kB
build/redux-routine/index.min.js 2.69 kB
build/reusable-blocks/index.min.js 2.22 kB
build/reusable-blocks/style-rtl.css 256 B
build/reusable-blocks/style.css 256 B
build/rich-text/index.min.js 11.1 kB
build/server-side-render/index.min.js 1.61 kB
build/shortcode/index.min.js 1.53 kB
build/token-list/index.min.js 644 B
build/url/index.min.js 3.61 kB
build/vendors/react-dom.min.js 38.5 kB
build/vendors/react.min.js 4.34 kB
build/viewport/index.min.js 1.08 kB
build/warning/index.min.js 268 B
build/widgets/index.min.js 7.19 kB
build/widgets/style-rtl.css 1.16 kB
build/widgets/style.css 1.16 kB
build/wordcount/index.min.js 1.06 kB

compressed-size-action

@andrewserong
Copy link
Contributor Author

I've taken a tiny step forward in b2bc04a to experiment with getting the classname for the block and combining that with the container-id classname to increase specificity. If we also remove the fallback styles, then this (at least superficially) seems to solve the specificity issue (on the frontend, at least): 😀

image

Next up, to see if this is viable, some more to-dos (I'm jotting them down here so that I remember!):

  • In the theme JSON output, we need to now generate the fallback styles (preferably without relying on a CSS variable) — perhaps this could be a rule like body .is-layout-flex so that its specificity is the lowest of the three ways we generate styles.
  • Try replicating these rules in the editor, too
  • See if we need to factor in blocks that don't use a classname (or opt-out of generating custom gap styles for blocks that don't support a classname, as I think we do need a classname for this approach to work)

@ramonjd
Copy link
Member

ramonjd commented May 13, 2022

I like this idea @andrewserong Thanks for working on it.

Just for the record, I tried experimenting with splitting the CSS vars, one for root and another for layout over in #41040

But I think I like the classname/CSS approach better, especially if we can tie it into any meaningful semantic classnames work we do.

@andrewserong
Copy link
Contributor Author

Just for the record, I tried experimenting with splitting the CSS vars

Oh, nice, thanks for digging in! I think the more we experiment the better, that we way can compare concrete examples 😀

@andrewserong
Copy link
Contributor Author

andrewserong commented May 13, 2022

In the theme JSON output, we need to now generate the fallback styles (preferably without relying on a CSS variable) — perhaps this could be a rule like body .is-layout-flex so that its specificity is the lowest of the three ways we generate styles.

I think I've figured out how to get this working, hacked in 896c46e, which appears to mean we now have styles rendered out with the correct specificity so that it goes (from highest specificity to least):

  1. Individual block level attributes
  2. Block level blockGap in theme.json
  3. Fallback gap value set in global styles, attached to body and descendant .is-layout-{type} selector

Now that that's hacked in there and seems to be working, next week I'll:

  • Give this a rebase and move the code to the appropriate files in the 6.1 directory
  • Tidy up to remove some of the hacks
  • Try implementing in the editor

@andrewserong andrewserong force-pushed the try/setting-layout-class-names-in-theme-json-for-blockgap-support-at-block-level branch from 896c46e to e4bdd01 Compare May 16, 2022 06:50
@andrewserong
Copy link
Contributor Author

Still very much a hack, but in 4492cd7, I've now got the approach working in the site editor, so adjusting spacing at the block level in global styles now appears to be working. Here's a not exciting screenshot of the styles output:

image

Here's a slightly more exciting screengrab of adjusting block spacing in global styles for the social icons block:

2022-05-17 13 10 52

Next up: work out a better place to put everything, and perhaps see if we can consolidate with ideas from #39708.

);

if ( isset( static::LAYOUT_STYLES[ $css_property ] ) ) {
$declaration['selectors'] = static::LAYOUT_STYLES[ $css_property ];
Copy link
Member

Choose a reason for hiding this comment

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

Nice trick! I really like how the output CSS works.

I'd be interested to learn whether layout could be abstracted from this class somehow. I'm only mentioning since layout is growing and probably will grow more tendrils in the form of exceptions and further rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely! Same here — my goal was to hack the code in wherever I could find to get this working. Now that it seems to mostly be working, the next step is to find a better and more logical home for the code changes 🙂

Let me know if you have any strong ideas on where anything should live!

@ramonjd
Copy link
Member

ramonjd commented May 17, 2022

This is coming along really well so far. Thanks for running with it. I'm feeling positive about the direction, especially if we look into the use of CSS vars and/or classnames to drive the layout styles. Referring to your previous work in #38974

@andrewserong andrewserong force-pushed the try/setting-layout-class-names-in-theme-json-for-blockgap-support-at-block-level branch 2 times, most recently from 784b457 to 46704cf Compare May 23, 2022 02:22
@ramonjd
Copy link
Member

ramonjd commented May 26, 2022

Would this PR also resolve #40516?

@andrewserong
Copy link
Contributor Author

Would this PR also resolve #40516?

Oh, thanks for linking that one @ramonjd! I think so — so far with this exploration, it looks like it should be possible for us to handle all the blockGap settings without using the CSS variable at all, if I'm not mistaken. I've done that in the server-side implementation, but still need to update the in editor styling to see if it's viable, but I've been a bit distracted by trying to land some bug fixes elsewhere this week 🙂

@andrewserong andrewserong force-pushed the try/setting-layout-class-names-in-theme-json-for-blockgap-support-at-block-level branch from 2c23f89 to 2d0ff82 Compare May 27, 2022 06:11
@andrewserong
Copy link
Contributor Author

I've given this a rebase and reworked part of the PHP implementation slightly to move the layout gap styles generation to its own function. This way it plays nicely with the changes from: #41160 so that the gap styles per block are output in the individual style tags for each of those blocks.

In the process, I've removed the CSS variable generation in PHP. For backwards compatibility, we'll probably want to add it back in again as a preset, outside of the PROPERTIES_METADATA process. So, some tasks for next week:

  • Add comparable layout gap style generation logic to the editor
  • Add back in CSS variable generation potentially, via a preset
  • A bit more consolidation so that it can neatened up for review

* Each element is a direct mapping from the CSS property name to the
* path to the value in theme.json & block attributes.
*/
const PROPERTIES_METADATA = array(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is added to the 6-1 file primarily to remove the --wp--style--block-gap CSS variable from being output. Styles at each level (as of this PR) should be output directly, without need of the CSS variable. For example, for Flex layouts, each level is now handled via classnames and specificity:

image

'wideSize' => null,
),
'spacing' => array(
'blockGap' => null,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Valid settings are added so that blockGap can be set to null instead of top — as of this PR, the blockGap value will be valid at any level.

@andrewserong
Copy link
Contributor Author

andrewserong commented May 30, 2022

There's still lots more that needs to be tidied up, but in dca159d I now have the gap value in global styles in the site editor being generated from the definitions in theme.json.

Top-of-mind things to implement next:

  • Remove duplication / erroneous addition of layout classes in the editor where there should be any (e.g. on the Button and Social Icon (singular) blocks)
  • Restore outputting the base CSS variable for backwards compatibility (and support for the ad hoc implementation of the Gallery block)
  • See how we can include fallback gap styles that are aware of the particular block being rendered — essentially, ensure that we don't break Layout: Add default fallback gap value in block.json, and set to 2em for Columns blocks #41098
  • When rendering out the block-level layout styles, can we do a look up if the block name is provided, so that we only output the relevant styles? (It's probably not a huge issue right now, but currently styles for each of the layout types are output)
  • Fix rendering of root flow gap styles in the post editor

pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Sep 21, 2022
This commit merges the remaining changes from [WordPress/gutenberg#40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.

Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.

Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.

Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].

Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.

git-svn-id: https://develop.svn.wordpress.org/trunk@54274 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Sep 21, 2022
This commit merges the remaining changes from [WordPress/gutenberg#40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.

Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.

Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.

Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].

Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274


git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Sep 21, 2022
This commit merges the remaining changes from [WordPress/gutenberg#40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.

Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.

Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.

Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].

Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274


git-svn-id: https://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@juanmaguitar
Copy link
Contributor

@juanmaguitar can we check what new classes are being outputted to communicate to developers? There's also #41993. Would be good to have a clear list of what is new in 6.1.

@mtias, @justintadlock is already working on bringing visibility to this

@exstheme
Copy link

There is no default 'display:flex' in the 6.1 beta 1 WordPress version for a buttons block. As a result - flex justify classes don't work.
Maybe this is the case for another flex blocks as well.

In the WordPress 6.0:
https://i.imgur.com/vCsbLLq.png

In the 6.1 beta 1:
https://i.imgur.com/UP2JVS0.png

Is this a bug or a new feature?
Thank you!

@andrewserong
Copy link
Contributor Author

@exstheme thanks for reporting the issue you've run into. display:flex rules continue to be output in 6.1, however instead of being attached to the wp-container-$id based rule, they are attached to the is-layout-flex classname. In my local environment, testing with blocks-based and classic themes, these rules appear to still be output in 6.1. E.g. here's the rule in TwentyTwentyOne theme:

image

Is your theme dequeueing the global styles inline CSS 'global-styles-inline-css'? Since this PR is now closed, it might be worth opening a new issue if you believe you've found a bug that needs investigating.

@exstheme
Copy link

Hello, dear @andrewserong !

Thank you very much for your answer and your time!

And thank you for your pointing me to 'global-styles-inline-css' styles.
In deed, I have dequeued this styles:

wp_dequeue_style('global-styles');

because we're not using them in our theme. I have found that 14kB of inline styles that are printed in the head and cannot be cached as a normal CSS file is a little bit overkilling.

Maybe it would be better to add this super important styles to the wp-includes/css/dist/block-library/style.min.css:
https://i.imgur.com/C4zIkQb.png
instead of printing them inline?

body .is-layout-flex{
    display: flex;
}
body .is-layout-flex{
    flex-wrap: wrap;
    align-items: center;
}

And why they are printed separately? The selector is the same.

For now I will add these CSS rules to my theme's CSS file to be compatible with WP 6.1.

Once again thank you for your attention!

Best regards!

@Mamaduka
Copy link
Member

A byproduct of the layout refactor is that blockGap can now be set at the block level in theme.json and via the global styles UI, as gap styles are now also output via the above layout type classnames, using direct CSS values, instead of the blockGap CSS variable --wp--style--block-gap.

I noticed this breaking change if the theme tries to override the CSS variable on the block level. I'm experimenting with re-building my site with Beaumont and this CSS rule no longer works - https://themes.trac.wordpress.org/browser/beaumont/0.1.5/style.css#L352.

I just wanted to mention this issue. But I think my example here can be solved by adding blockGap support to the Post Template block.

@andrewserong
Copy link
Contributor Author

Thanks for the update @exstheme, glad you could find a workaround. If you're looking into adopting theme.json at any point in the future, you might be able to reduce the output by removing color presets, etc, so that you can then take advantage of the generated layout styles.

And why they are printed separately? The selector is the same.

Most of the styles are grouped together, but the display rule has special handling because it isn't handled by the usual safe CSS filter, so it's currently output separately. I believe the plan is to consolidate / group together rules in global styles for 6.2 — the consolidation in this PR was at the block level. Although there is an addition of a small amount of duplication in this particular rule, on balance, we still have a reduction in the Layout CSS output for 6.1

@andrewserong
Copy link
Contributor Author

andrewserong commented Sep 27, 2022

I noticed this breaking change if the theme tries to override the CSS variable on the block level. I'm experimenting with re-building my site with Beaumont and this CSS rule no longer works - https://themes.trac.wordpress.org/browser/beaumont/0.1.5/style.css#L352.

Thanks for the heads-up @Mamaduka! I believe block-level gap control wasn't previously available in global styles, so yes, it does sound like for some themes there'll be a breaking change if folks have been overriding the CSS variable like that. The Post Template block is a bit of a weird case because it currently partially opts-in to the layout support, and it also has its own existing gap rule for the grid layout in its style.scss file — I think that might have been partly the reason why it hasn't formally opted-in for block spacing controls (along with the unusual relationship between Query Loop and Post Template blocks).

Do you know what the desired result is from that existing rule? (If we have a concrete use case, we can add a suggested fix for themes like this in the dev note if it's a common thing in themes — they might be able to target the desired CSS properties now instead of relying on the CSS variable?). It might also be worth opening up a separate issue for the problem of overriding post template spacing so we can investigate further, if it isn't already covered in the request in #39380

@Mamaduka
Copy link
Member

Sounds good. I'll have a look at #39380.

Do you know what the desired result is from that existing rule?

I think mentioning a simple upgrade path for similar cases in the DevNote would be great.

Cc. @andersnoren, any thoughts on desired results here?

@andersnoren
Copy link

@Mamaduka Beaumont has separators between each item in the Query Loop, and the --wp--style--block-gap is set to 0 so the spacing between the separators could be handled entirely in the Site Editor prior to 6.1. It's the only one of my block themes that does this, and I haven't seen any others do it either, so it might be an edge case.

This is how it looks with the CSS variable set to 0 in style.css (running 6.0 without Gutenberg):
image

And without the CSS variable set to 0 (also how it looks running Gutenberg right now):
image

@andrewserong
Copy link
Contributor Author

Awesome, thank you for confirming George and Anders, I'll download Beaumont and have a play to see what the best fix might be.

@andrewserong
Copy link
Contributor Author

It looks like you just recently added a styling rule to account for this one @andersnoren, but from testing locally, I think you might also be able to add the following to styles.blocks in your theme.json file to set that particular gap to 0 (no need to, though, the rule you've added looks good to me):

			"core/post-template": {
				"spacing": {
					"blockGap": "0"
				}
			},

Here's how adding that looks in my local environment:

image

I'd be slightly hesitant about suggesting that as a general fix, though, because the Post Template block doesn't properly support switching between flow and flex layouts — switching to grid adds the block's ad hoc is-flex-container classname rather than using the is-layout-flex logic from the layout support, so blockGap output doesn't work correctly there (it still outputs the * + * rule instead of a gap rule). Refactoring that logic is a blocker for opting in that block to blockGap more explicitly. I'll open a separate issue for that if one doesn't already exist, because it'd be great to improve layout support for Query Loop / Post Template for 6.2.

It's the only one of my block themes that does this, and I haven't seen any others do it either, so it might be an edge case.

Good to know. If we encounter any other cases of this, I'm happy to write something up for the dev notes. If not, it might be better to leave it off since it appears to be quite a specific case particular to that block.

As an aside, that's a very nice looking theme!

@andersnoren
Copy link

@andrewserong Oh, I managed to completely miss that – I must have had the Gutenberg plugin inactive when I tested it. I'm always happy to remove a little bit of CSS, so thanks for the tip (and the kind words)!

@andrewserong andrewserong added the [Feature] Layout Layout block support, its UI controls, and style output. label Sep 29, 2022
ootwch pushed a commit to ootwch/wordpress-develop that referenced this pull request Nov 4, 2022
This change backports the following changes from Gutenberg repository:

- [WordPress/gutenberg#40875 gutenberg#40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
- [WordPress/gutenberg#42544 gutenberg#42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg#42544
- [WordPress/gutenberg#42087 gutenberg#42087] Theme.json: Add block support feature level selectors for blocks gutenberg#42087
- [WordPress/gutenberg#43792 gutenberg#43792] Global Styles: Split root layout rules into a different function gutenberg#43792
- [WordPress/gutenberg#42544 gutenberg#42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg#42544
- [WordPress/gutenberg#42665 gutenberg#42665] Layout: Reduce specificity of fallback blockGap styles gutenberg#42665
- [WordPress/gutenberg#42085 gutenberg#42085] Core CSS support for root padding and alignfull blocks gutenberg#42085

Note that it doesn't entirely port over PR40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting `layout.php`.

Props andrewserong, aaronrobertshaw, isabel_brison.
See #56467.


git-svn-id: https://develop.svn.wordpress.org/trunk@54159 602fd350-edb4-49c9-b593-d223f7449a82
ootwch pushed a commit to ootwch/wordpress-develop that referenced this pull request Nov 4, 2022
This commit merges the remaining changes from [WordPress/gutenberg#40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.

Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.

Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.

Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].

Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.

git-svn-id: https://develop.svn.wordpress.org/trunk@54274 602fd350-edb4-49c9-b593-d223f7449a82
return <layoutType.save layout={ layout } { ...props } />;
if ( css ) {
return <style>{ css }</style>;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this is old, but I'm not sure I understand why we have a css prop in this component, why not just use <style>{css}</style> instead of <LayoutStyle css={css} />? am I missing anything? It seems it has nothing to do with layout.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could totally be refactored. If I remember correctly, the thinking at the time was to have a single entry point for outputting layout styles via the LayoutStyle component, as it includes a check that the layout type is valid and made for a little more consistency in the consuming code... the thinking at the time was also that we might further refactor the innards of LayoutStyle to not necessarily output a style tag, so in theory, having that abstracted into the LayoutStyle component could be beneficial.

There's been a fair few changes since this PR was merged, though, and with the recent work on #52888 and I see #54466 has just landed to refactor some of the layout style output, I don't think we need to hold onto any of the decisions in this older PR 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Layout Layout block support, its UI controls, and style output. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] Experimental Experimental feature or API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Layout blockGap / spacing: Add support for gap set at the block level in theme.json and global styles