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: Move layout definitions out of theme.json #50621

Conversation

andrewserong
Copy link
Contributor

@andrewserong andrewserong commented May 15, 2023

What?

This is an experimental PR digging into an idea from #50268 (comment) to remove the layout definitions from theme.json.

The approach in this PR is to try to generally retain the same shape and behaviour of the layout definitions, but switch where we retrieve the definitions from. They are now sourced from a PHP function where they're hard-coded, and in JS, they're stored in a const that is imported where appropriate.

This PR also looks to try to resolve #50011 by calling getLayoutStyles from the post editor.

Why?

As raised in #50268 (comment), the layout definitions belong to core and are not intended to be changed or overridden by consumers. It is still useful to have central places where those definitions are stored. This PR initially proposed storing the layout definitions in a single PHP location, but has since evolved to include a corresponding JS constant.

How?

  • Move layout definitions from theme.json to a gutenberg_get_layout_definitions function, and to a const in JS.
  • Call gutenberg_get_layout_definitions wherever we need the definitions:
    • In the Theme JSON class when outputting layout rules
    • In layout.php when outputting classnames and rules at the individual block level
  • In the block editor code, add a JS const for the layout definitions, and import it where appropriate / use it as a default value, and remove lookups from theme.json settings
  • Export getLayoutStyles as a private export
  • Import it in the post editor, and use it to output base layout styles when the user has opted out of displaying theme styles in the editor

Testing Instructions

This PR refactors both PHP and JS output for layout, so it'd be good to test all of the following to make sure nothing was missed:

  • Each Group block variation (Group, Row, Stack, Grid) in the post editor, site editor, and site frontend. Test that default block spacing is output correctly, and that block spacing output is correct when set at the individual block level.
  • Adjust global block spacing in the site site editor, adjust block-level spacing in global styles (e.g. block spacing for all social icons blocks), ensure output is consistent in site editor and on site frontend.
  • Check that default layout styles are still output correctly in the post editor and site frontend for Classic themes (e.g. the social icons and columns blocks still visually have spacing)

Screenshots or screencast

@andrewserong andrewserong added [Type] Bug An existing feature does not function as intended [Type] Code Quality Issues or PRs that relate to code quality [Feature] Layout Layout block support, its UI controls, and style output. labels May 15, 2023
@andrewserong andrewserong self-assigned this May 15, 2023
@andrewserong andrewserong changed the title Layout: Try moving layout definitions out of theme.json, but retain in block editor settings Layout: Move layout definitions out of theme.json, but retain in block editor settings May 15, 2023
@github-actions
Copy link

github-actions bot commented May 15, 2023

Size Change: +1.05 kB (0%)

Total Size: 1.4 MB

Filename Size Change
build/block-editor/index.min.js 195 kB +349 B (0%)
build/block-library/blocks/avatar/style-rtl.css 104 B +13 B (+14%) ⚠️
build/block-library/blocks/avatar/style.css 104 B +13 B (+14%) ⚠️
build/block-library/index.min.js 201 kB +44 B (0%)
build/block-library/style-rtl.css 13.3 kB +11 B (0%)
build/block-library/style.css 13.3 kB +9 B (0%)
build/edit-post/index.min.js 34 kB +52 B (0%)
build/edit-site/index.min.js 69.1 kB +60 B (0%)
build/edit-site/style-rtl.css 11.6 kB +251 B (+2%)
build/edit-site/style.css 11.6 kB +249 B (+2%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 955 B
build/annotations/index.min.js 2.69 kB
build/api-fetch/index.min.js 2.29 kB
build/autop/index.min.js 2.1 kB
build/blob/index.min.js 451 B
build/block-directory/index.min.js 7.05 kB
build/block-directory/style-rtl.css 1.02 kB
build/block-directory/style.css 1.02 kB
build/block-editor/content-rtl.css 4.22 kB
build/block-editor/content.css 4.22 kB
build/block-editor/default-editor-styles-rtl.css 381 B
build/block-editor/default-editor-styles.css 381 B
build/block-editor/style-rtl.css 14.9 kB
build/block-editor/style.css 14.9 kB
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 90 B
build/block-library/blocks/archives/style.css 90 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 122 B
build/block-library/blocks/audio/style.css 122 B
build/block-library/blocks/audio/theme-rtl.css 126 B
build/block-library/blocks/audio/theme.css 126 B
build/block-library/blocks/avatar/editor-rtl.css 116 B
build/block-library/blocks/avatar/editor.css 116 B
build/block-library/blocks/block/editor-rtl.css 305 B
build/block-library/blocks/block/editor.css 305 B
build/block-library/blocks/button/editor-rtl.css 584 B
build/block-library/blocks/button/editor.css 582 B
build/block-library/blocks/button/style-rtl.css 624 B
build/block-library/blocks/button/style.css 623 B
build/block-library/blocks/buttons/editor-rtl.css 337 B
build/block-library/blocks/buttons/editor.css 337 B
build/block-library/blocks/buttons/style-rtl.css 332 B
build/block-library/blocks/buttons/style.css 332 B
build/block-library/blocks/calendar/style-rtl.css 239 B
build/block-library/blocks/calendar/style.css 239 B
build/block-library/blocks/categories/editor-rtl.css 113 B
build/block-library/blocks/categories/editor.css 112 B
build/block-library/blocks/categories/style-rtl.css 124 B
build/block-library/blocks/categories/style.css 124 B
build/block-library/blocks/code/editor-rtl.css 53 B
build/block-library/blocks/code/editor.css 53 B
build/block-library/blocks/code/style-rtl.css 121 B
build/block-library/blocks/code/style.css 121 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 409 B
build/block-library/blocks/columns/style.css 409 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 199 B
build/block-library/blocks/comment-template/style.css 198 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 840 B
build/block-library/blocks/comments/editor.css 839 B
build/block-library/blocks/comments/style-rtl.css 637 B
build/block-library/blocks/comments/style.css 636 B
build/block-library/blocks/cover/editor-rtl.css 647 B
build/block-library/blocks/cover/editor.css 650 B
build/block-library/blocks/cover/style-rtl.css 1.61 kB
build/block-library/blocks/cover/style.css 1.6 kB
build/block-library/blocks/details/editor-rtl.css 65 B
build/block-library/blocks/details/editor.css 65 B
build/block-library/blocks/details/style-rtl.css 159 B
build/block-library/blocks/details/style.css 159 B
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 126 B
build/block-library/blocks/embed/theme.css 126 B
build/block-library/blocks/file/editor-rtl.css 316 B
build/block-library/blocks/file/editor.css 316 B
build/block-library/blocks/file/interactivity.min.js 395 B
build/block-library/blocks/file/style-rtl.css 269 B
build/block-library/blocks/file/style.css 270 B
build/block-library/blocks/freeform/editor-rtl.css 2.58 kB
build/block-library/blocks/freeform/editor.css 2.58 kB
build/block-library/blocks/gallery/editor-rtl.css 947 B
build/block-library/blocks/gallery/editor.css 952 B
build/block-library/blocks/gallery/style-rtl.css 1.53 kB
build/block-library/blocks/gallery/style.css 1.53 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 654 B
build/block-library/blocks/group/editor.css 654 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 336 B
build/block-library/blocks/html/editor.css 337 B
build/block-library/blocks/image/editor-rtl.css 834 B
build/block-library/blocks/image/editor.css 833 B
build/block-library/blocks/image/interactivity.min.js 799 B
build/block-library/blocks/image/style-rtl.css 1.11 kB
build/block-library/blocks/image/style.css 1.11 kB
build/block-library/blocks/image/theme-rtl.css 126 B
build/block-library/blocks/image/theme.css 126 B
build/block-library/blocks/latest-comments/style-rtl.css 357 B
build/block-library/blocks/latest-comments/style.css 357 B
build/block-library/blocks/latest-posts/editor-rtl.css 213 B
build/block-library/blocks/latest-posts/editor.css 212 B
build/block-library/blocks/latest-posts/style-rtl.css 478 B
build/block-library/blocks/latest-posts/style.css 478 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 507 B
build/block-library/blocks/media-text/style.css 505 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 712 B
build/block-library/blocks/navigation-link/editor.css 711 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/editor-rtl.css 2.35 kB
build/block-library/blocks/navigation/editor.css 2.36 kB
build/block-library/blocks/navigation/interactivity.min.js 978 B
build/block-library/blocks/navigation/style-rtl.css 2.21 kB
build/block-library/blocks/navigation/style.css 2.2 kB
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 401 B
build/block-library/blocks/page-list/editor.css 401 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 174 B
build/block-library/blocks/paragraph/editor.css 174 B
build/block-library/blocks/paragraph/style-rtl.css 279 B
build/block-library/blocks/paragraph/style.css 281 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 508 B
build/block-library/blocks/post-comments-form/style.css 508 B
build/block-library/blocks/post-date/style-rtl.css 61 B
build/block-library/blocks/post-date/style.css 61 B
build/block-library/blocks/post-excerpt/editor-rtl.css 71 B
build/block-library/blocks/post-excerpt/editor.css 71 B
build/block-library/blocks/post-excerpt/style-rtl.css 141 B
build/block-library/blocks/post-excerpt/style.css 141 B
build/block-library/blocks/post-featured-image/editor-rtl.css 588 B
build/block-library/blocks/post-featured-image/editor.css 586 B
build/block-library/blocks/post-featured-image/style-rtl.css 319 B
build/block-library/blocks/post-featured-image/style.css 319 B
build/block-library/blocks/post-navigation-link/style-rtl.css 153 B
build/block-library/blocks/post-navigation-link/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 314 B
build/block-library/blocks/post-template/style.css 314 B
build/block-library/blocks/post-terms/style-rtl.css 96 B
build/block-library/blocks/post-terms/style.css 96 B
build/block-library/blocks/post-time-to-read/style-rtl.css 69 B
build/block-library/blocks/post-time-to-read/style.css 69 B
build/block-library/blocks/post-title/style-rtl.css 100 B
build/block-library/blocks/post-title/style.css 100 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 135 B
build/block-library/blocks/pullquote/editor.css 135 B
build/block-library/blocks/pullquote/style-rtl.css 335 B
build/block-library/blocks/pullquote/style.css 335 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 288 B
build/block-library/blocks/query-pagination/style.css 284 B
build/block-library/blocks/query-title/style-rtl.css 63 B
build/block-library/blocks/query-title/style.css 63 B
build/block-library/blocks/query/editor-rtl.css 450 B
build/block-library/blocks/query/editor.css 449 B
build/block-library/blocks/quote/style-rtl.css 222 B
build/block-library/blocks/quote/style.css 222 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 149 B
build/block-library/blocks/rss/editor.css 149 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 178 B
build/block-library/blocks/search/editor.css 178 B
build/block-library/blocks/search/style-rtl.css 587 B
build/block-library/blocks/search/style.css 584 B
build/block-library/blocks/search/theme-rtl.css 114 B
build/block-library/blocks/search/theme.css 114 B
build/block-library/blocks/search/view.min.js 531 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 234 B
build/block-library/blocks/separator/style.css 234 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 323 B
build/block-library/blocks/shortcode/editor.css 323 B
build/block-library/blocks/site-logo/editor-rtl.css 754 B
build/block-library/blocks/site-logo/editor.css 754 B
build/block-library/blocks/site-logo/style-rtl.css 203 B
build/block-library/blocks/site-logo/style.css 203 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 116 B
build/block-library/blocks/site-title/editor.css 116 B
build/block-library/blocks/site-title/style-rtl.css 57 B
build/block-library/blocks/site-title/style.css 57 B
build/block-library/blocks/social-link/editor-rtl.css 184 B
build/block-library/blocks/social-link/editor.css 184 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.43 kB
build/block-library/blocks/social-links/style.css 1.42 kB
build/block-library/blocks/spacer/editor-rtl.css 348 B
build/block-library/blocks/spacer/editor.css 348 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 433 B
build/block-library/blocks/table/editor.css 433 B
build/block-library/blocks/table/style-rtl.css 645 B
build/block-library/blocks/table/style.css 644 B
build/block-library/blocks/table/theme-rtl.css 146 B
build/block-library/blocks/table/theme.css 146 B
build/block-library/blocks/tag-cloud/style-rtl.css 251 B
build/block-library/blocks/tag-cloud/style.css 253 B
build/block-library/blocks/template-part/editor-rtl.css 403 B
build/block-library/blocks/template-part/editor.css 403 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/term-description/style-rtl.css 111 B
build/block-library/blocks/term-description/style.css 111 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 99 B
build/block-library/blocks/verse/style.css 99 B
build/block-library/blocks/video/editor-rtl.css 552 B
build/block-library/blocks/video/editor.css 555 B
build/block-library/blocks/video/style-rtl.css 174 B
build/block-library/blocks/video/style.css 174 B
build/block-library/blocks/video/theme-rtl.css 126 B
build/block-library/blocks/video/theme.css 126 B
build/block-library/classic-rtl.css 179 B
build/block-library/classic.css 179 B
build/block-library/common-rtl.css 1.1 kB
build/block-library/common.css 1.1 kB
build/block-library/editor-elements-rtl.css 75 B
build/block-library/editor-elements.css 75 B
build/block-library/editor-rtl.css 12.2 kB
build/block-library/editor.css 12.1 kB
build/block-library/elements-rtl.css 54 B
build/block-library/elements.css 54 B
build/block-library/interactivity/runtime.min.js 2.71 kB
build/block-library/interactivity/vendors.min.js 8.2 kB
build/block-library/reset-rtl.css 478 B
build/block-library/reset.css 478 B
build/block-library/theme-rtl.css 686 B
build/block-library/theme.css 691 B
build/block-serialization-default-parser/index.min.js 1.12 kB
build/block-serialization-spec-parser/index.min.js 2.87 kB
build/blocks/index.min.js 50.3 kB
build/commands/index.min.js 14.9 kB
build/commands/style-rtl.css 827 B
build/commands/style.css 827 B
build/components/index.min.js 231 kB
build/components/style-rtl.css 11.8 kB
build/components/style.css 11.8 kB
build/compose/index.min.js 12.1 kB
build/core-commands/index.min.js 1.77 kB
build/core-data/index.min.js 15.7 kB
build/customize-widgets/index.min.js 12 kB
build/customize-widgets/style-rtl.css 1.38 kB
build/customize-widgets/style.css 1.38 kB
build/data-controls/index.min.js 640 B
build/data/index.min.js 8.3 kB
build/date/index.min.js 40.4 kB
build/deprecated/index.min.js 451 B
build/dom-ready/index.min.js 324 B
build/dom/index.min.js 4.63 kB
build/edit-post/classic-rtl.css 544 B
build/edit-post/classic.css 545 B
build/edit-post/style-rtl.css 7.58 kB
build/edit-post/style.css 7.57 kB
build/edit-widgets/index.min.js 16.8 kB
build/edit-widgets/style-rtl.css 4.53 kB
build/edit-widgets/style.css 4.53 kB
build/editor/index.min.js 44.7 kB
build/editor/style-rtl.css 3.54 kB
build/editor/style.css 3.53 kB
build/element/index.min.js 4.8 kB
build/escape-html/index.min.js 537 B
build/format-library/index.min.js 7.57 kB
build/format-library/style-rtl.css 554 B
build/format-library/style.css 553 B
build/hooks/index.min.js 1.55 kB
build/html-entities/index.min.js 448 B
build/i18n/index.min.js 3.58 kB
build/is-shallow-equal/index.min.js 527 B
build/keyboard-shortcuts/index.min.js 1.71 kB
build/keycodes/index.min.js 1.84 kB
build/list-reusable-blocks/index.min.js 2.13 kB
build/list-reusable-blocks/style-rtl.css 836 B
build/list-reusable-blocks/style.css 836 B
build/media-utils/index.min.js 2.9 kB
build/notices/index.min.js 948 B
build/plugins/index.min.js 1.84 kB
build/preferences-persistence/index.min.js 1.84 kB
build/preferences/index.min.js 1.24 kB
build/primitives/index.min.js 941 B
build/priority-queue/index.min.js 1.52 kB
build/private-apis/index.min.js 939 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.7 kB
build/reusable-blocks/index.min.js 2.21 kB
build/reusable-blocks/style-rtl.css 243 B
build/reusable-blocks/style.css 243 B
build/rich-text/index.min.js 10.7 kB
build/router/index.min.js 1.77 kB
build/server-side-render/index.min.js 2.02 kB
build/shortcode/index.min.js 1.39 kB
build/style-engine/index.min.js 1.42 kB
build/token-list/index.min.js 582 B
build/url/index.min.js 3.57 kB
build/vendors/inert-polyfill.min.js 2.48 kB
build/vendors/react-dom.min.js 41.8 kB
build/vendors/react.min.js 4.02 kB
build/viewport/index.min.js 1.04 kB
build/warning/index.min.js 268 B
build/widgets/index.min.js 7.16 kB
build/widgets/style-rtl.css 1.15 kB
build/widgets/style.css 1.16 kB
build/wordcount/index.min.js 1.02 kB

compressed-size-action

@github-actions
Copy link

github-actions bot commented May 15, 2023

Flaky tests detected in fde9a8d.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5261343223
📝 Reported issues:

@andrewserong
Copy link
Contributor Author

Just pinging a few folks for early(ish) feedback on the approach in this PR. @youknowriad @oandregal and @tellthemachines, does this seem like a reasonable direction for moving the layout definitions out of theme.json? I'm trying to find the least disruptive way to do it code-wise, and this seems like a way to do it technically, while not changing too much about how we construct and output layout rules in JS.

I'll be back on Tuesday, but just thought I'd do an early ping while this is still in draft in case anyone has a particular direction in mind for the refactor, and if there are any other considerations to keep in mind (e.g. caching etc).

@@ -77,6 +77,8 @@ function gutenberg_get_block_editor_settings( $settings ) {
// Copied from get_block_editor_settings() at wordpress-develop/block-editor.php.
$settings['__experimentalFeatures'] = gutenberg_get_global_settings();

$settings['__experimentalFeatures']['layout']['definitions'] = gutenberg_get_layout_definitions();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this still means plugins can edit these (at least in the frontend). So why don't we avoid the setting at all and just hard code them in the client too. Personally I don't mind the duplication (client/server) but potentially if you don't want duplication you can put them in a json file and have a build step or something to generate the php and the JS?

Copy link
Contributor

Choose a reason for hiding this comment

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

Given layout styles are a work in progress (with new layout types such as grid still being worked on), it would be easier for development and less error-prone if these definitions were stored in a single place. A json file might be the best approach if we don't want to add them as block editor settings, though I'm not too concerned about it - the settings being editable by plugins doesn't bother me as much as these definitions being output in generated theme.json files. I assume any plugin author that were to deliberately edit these settings would know and be responsible for what they were doing 😅

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 turned out not to be too bad copying and pasting to a JS file. Very happy for feedback on it, but since we don't change the definitions too frequently, now that I look at it, I don't think it's too much of an issue if we have to update in two places, so long as it's clear that it has to happen in both places.

I've updated to try out a separate JS LAYOUT_DEFINITIONS const.

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 perfect for me :)

@andrewserong andrewserong force-pushed the try/fix-layout-definitions-and-layout-styles-in-editor-when-theme-styles-are-not-in-use branch from 44f8bd7 to deb1fb7 Compare June 13, 2023 04:48
Copy link
Contributor

@tellthemachines tellthemachines left a comment

Choose a reason for hiding this comment

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

A quick test shows everything working as expected, but I'll do some more testing on this later. Just a couple of questions/comments for now!

@@ -77,6 +77,8 @@ function gutenberg_get_block_editor_settings( $settings ) {
// Copied from get_block_editor_settings() at wordpress-develop/block-editor.php.
$settings['__experimentalFeatures'] = gutenberg_get_global_settings();

$settings['__experimentalFeatures']['layout']['definitions'] = gutenberg_get_layout_definitions();
Copy link
Contributor

Choose a reason for hiding this comment

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

Given layout styles are a work in progress (with new layout types such as grid still being worked on), it would be easier for development and less error-prone if these definitions were stored in a single place. A json file might be the best approach if we don't want to add them as block editor settings, though I'm not too concerned about it - the settings being editable by plugins doesn't bother me as much as these definitions being output in generated theme.json files. I assume any plugin author that were to deliberately edit these settings would know and be responsible for what they were doing 😅

@@ -596,7 +596,7 @@ describe( 'global styles renderer', () => {
};

expect( toStyles( Object.freeze( tree ), blockSelectors ) ).toEqual(
'body {margin: 0;}' +
'body {margin: 0;}body .is-layout-flow > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }body .is-layout-flow > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }body .is-layout-flow > .aligncenter { margin-left: auto !important; margin-right: auto !important; }body .is-layout-constrained > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }body .is-layout-constrained > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }body .is-layout-constrained > .aligncenter { margin-left: auto !important; margin-right: auto !important; }body .is-layout-constrained > :where(:not(.alignleft):not(.alignright):not(.alignfull)) { max-width: var(--wp--style--global--content-size); margin-left: auto !important; margin-right: auto !important; }body .is-layout-constrained > .alignwide { max-width: var(--wp--style--global--wide-size); }body .is-layout-flex { display:flex; }body .is-layout-flex { flex-wrap: wrap; align-items: center; }body .is-layout-flex > * { margin: 0; }body .is-layout-grid { display:grid; }body .is-layout-grid > * { margin: 0; }' +
'.wp-image img{filter: blur(5px);}' +
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the output of toStyles be changing? 🤔

Copy link
Contributor Author

@andrewserong andrewserong Jun 13, 2023

Choose a reason for hiding this comment

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

Yes — previously the test was artificial in that it arbitrarily didn't include layout definitions, but real usage of toStyles would always include layout styles because it would wind up calling getLayoutStyles internally and include the real base theme.json's layout definitions. The updated tests in this PR are now a more accurate representation of the real output.

It is a little jarring when first looking at it, though!

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, got it, thanks for explaining!

@andrewserong andrewserong marked this pull request as ready for review June 13, 2023 07:10
@andrewserong
Copy link
Contributor Author

Thanks for the feedback, folks! I think this is ready for review now.

I've pushed some updates, exploring the idea of having a comparable hard-coded list of layout definitions in JS. I think it's slightly less neat, and it also means it'll be slightly more challenging for backporting because the JS isn't indifferent to what's happening on the PHP-side anymore, but I also don't mind the approach overall. When it comes to backporting to wordpress-develop, I think the JS side might need to be merged to core around the same time as the PHP side, otherwise if the PHP side is merged first, the JS will no longer be receiving layout definitions in settings.

Very happy for any feedback on the direction here, and happy to roll back the "layout definitions in JS" side of things if it makes this PR too complex / unwieldy!

@andrewserong andrewserong changed the title Layout: Move layout definitions out of theme.json, but retain in block editor settings Layout: Move layout definitions out of theme.json Jun 13, 2023
@@ -409,7 +592,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
}

$class_names = array();
$layout_definitions = _wp_array_get( $global_layout_settings, array( 'definitions' ), array() );
$layout_definitions = gutenberg_get_layout_definitions();
Copy link
Member

Choose a reason for hiding this comment

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

A few lines above, there's some code to query for the data stored in theme.json via gutenberg_get_global_settings. Now that the definitions would be stored elsewhere, does that code need to be updated as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, good catch! Yes, there's no longer a need to check for $global_layout_settings so I've removed a few lines.

We still need the overall $global_settings query for root padding aware alignments, and to grab the root block spacing value (blockGap) if available, so the main query is preserved.

@@ -1272,7 +1272,7 @@ protected function get_layout_styles( $block_metadata ) {
$has_block_gap_support = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'blockGap' ) ) !== null;
$has_fallback_gap_support = ! $has_block_gap_support; // This setting isn't useful yet: it exists as a placeholder for a future explicit fallback gap styles support.
$node = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );
$layout_definitions = _wp_array_get( $this->theme_json, array( 'settings', 'layout', 'definitions' ), array() );
$layout_definitions = gutenberg_get_layout_definitions();
Copy link
Member

Choose a reason for hiding this comment

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

We also need to remove layout.definitions from the VALID_SETTINGS in this class.

Copy link
Member

Choose a reason for hiding this comment

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

What happened if 3rd parties provided layout.definitions via theme.json? The potential scenarios are: the theme via its theme.json and plugins via the existing filters.

Copy link
Member

Choose a reason for hiding this comment

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

I found that layout.definitions were merged in core as of 6.1. PR WordPress/wordpress-develop@72380e3 and trac ticket https://core.trac.wordpress.org/ticket/56467 If there wasn't any code to prevent them from being overridden by themes/plugins, we probably need to communicate this via a devnote stating it is a bug that others were able to override the definitions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I've removed it from layout.definitions. If and when this PR lands, I'm happy to draft a dev note for 6.3 to flag the change 🙂

@oandregal
Copy link
Member

Thanks for doing this. I wonder what parts of the layout styles logic can be removed from the theme.json class to its own standalone class/function/point of reference. Anything that is not processing data coming from any theme.json source should not live in this class. Not for this PR, though. I'm more than happy to help investigate this.

@andrewserong
Copy link
Contributor Author

Thanks for taking a look!

I wonder what parts of the layout styles logic can be removed from the theme.json class to its own standalone class/function/point of reference.

I think the main opportunity (for a follow-up, likely beyond 6.3) could be to extract the body of the get_layout_styles method into a more generalised function in layout.php. There's also the potential of looking into whether, with all the different functions we have in layout, it'd be worth switching the functions in layout.php to a class, possibly?

But in general, I've been wondering if using a similar approach to typography functions in typography.php might work — that the Theme JSON class would essentially call out to functions that live in block supports to do whatever layout logic is required. For now, I think it's probably fine to leave the functions where they are for 6.3, but I think it'd certainly be neater for the Theme JSON class if this quite specific layout logic lived elsewhere.

Copy link
Contributor

@tellthemachines tellthemachines left a comment

Choose a reason for hiding this comment

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

I've tested this with all the core layout blocks in both classic and block themes, checked that Post Content layout is still reflected correctly in the post editor, and that layouts are still working correctly in the site editor too.

The code's looking good! Just a couple minor, very non-blocking comments below.

*
* Provides a common definition of slugs, classnames, base styles, and spacing styles for each layout type.
* When making changes or additions to layout definitions, the corresponding JavaScript definitions should
* also be updated.
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be nice to add the file path for the JS definitions here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking of adding that in originally, but once we backport the changes, any JS path won't quite make sense from the perspective of wordpress-develop, so I thought I'd leave it off for now. Happy to add it in once we've done the backports, though!

@@ -706,7 +706,8 @@ describe( 'global styles renderer', () => {
const style = { spacing: { blockGap: '12px' } };

const layoutStyles = getLayoutStyles( {
tree: layoutDefinitionsTree,
layoutDefinitions:
layoutDefinitionsTree.settings.layout.definitions,
Copy link
Contributor

Choose a reason for hiding this comment

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

Would there be any advantage to removing the hardcoded definitions in the test and using the now-hardcoded JS ones? I guess if we leave the test ones we won't have to update the tests every time the real ones change 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess if we leave the test ones we won't have to update the tests every time the real ones change 😅

Yeah, for the getLayoutStyles tests, I thought I'd leave it with the hardcoded definitions in the test, since it'll still accept a layoutDefinitions object being passed in, as it saves us a few copy/pastes when we need to update in the future.

I reckon we could probably tidy up these tests a little more in follow-ups, but since we don't need to do it just yet, I thought I'd try to keep the changeset slightly smaller for now.

Happy to update these if anyone feels strongly about it, though!

@andrewserong
Copy link
Contributor Author

The code's looking good! Just a couple minor, very non-blocking comments below.

Wonderful, thank you for the approving review @tellthemachines!

I thought I'd just wait overnight to see if there's any final feedback from @youknowriad or @oandregal, and if not, I'll merge this in first thing tomorrow 🙂

@oandregal
Copy link
Member

I thought I'd just wait overnight to see if there's any final feedback from @youknowriad or @oandregal, and if not, I'll merge this in first thing tomorrow slightly_smiling_face

Code-wise, it looks fine to me.

Copy link
Contributor

@apeatling apeatling left a comment

Choose a reason for hiding this comment

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

Nice work Andrew! I went through the testing instructions:

✅ Each Group block variation (Group, Row, Stack, Grid) in the post editor, site editor, and site frontend.
--- Default block spacing is output correctly
--- Block is spacing output correctly when set at the individual block level

✅ Adjust global block spacing in the site site editor, adjust block-level spacing in global styles (e.g. block spacing for all social icons blocks), output is consistent in site editor and on site frontend.

✅ Default layout styles are still output correctly in the post editor and site frontend for Classic themes (e.g. the social icons and columns blocks still visually have spacing)

@andrewserong
Copy link
Contributor Author

Thanks folks! 🙇 I'll merge this in now.

@andrewserong andrewserong merged commit f05cf6b into trunk Jun 14, 2023
@andrewserong andrewserong deleted the try/fix-layout-definitions-and-layout-styles-in-editor-when-theme-styles-are-not-in-use branch June 14, 2023 23:26
@github-actions github-actions bot added this to the Gutenberg 16.1 milestone Jun 14, 2023
@andrewserong andrewserong added the Needs PHP backport Needs PHP backport to Core label Jun 15, 2023
@andrewserong andrewserong added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Jun 16, 2023
@ramonjd ramonjd removed the Needs PHP backport Needs PHP backport to Core label Jun 30, 2023
@tellthemachines
Copy link
Contributor

Dev note

Layout definitions removed from core theme.json

The layout definitions object, which stores base styles for the layout block support, has been removed from the core theme.json (settings.layout.definitions) and moved into the internal layout support files. Extending or overriding core layout definitions was never officially supported, and including those definitions in a theme theme.json file resulted in bugs such as #49914.

Create Block Theme plugin generated theme.json files that included the layout definitions object before the bug was fixed in #50268, so theme authors are advised to remove any layout definitions that may have been inadvertently included in their theme.json files in order to prevent buggy behaviour.

@andrewserong
Copy link
Contributor Author

Thank you for adding the dev note @tellthemachines! 🙇

sethrubenstein pushed a commit to pewresearch/gutenberg that referenced this pull request Jul 13, 2023
* Layout: Try moving layout definitions out of theme.json, but retain in block editor settings

* Update comment

* Add duplicate layout definitions in JS.

* Fix some of the JS tests

* Update PHP tests

* Remove layout definitions from settings

* A couple more removals that I previously missed
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. Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] Bug An existing feature does not function as intended [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.is-layout-flex css is empty when Editor Preferences: Use theme style is disabled
6 participants