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

Block Supports: Add background image support to Group block #53934

Merged
merged 14 commits into from
Sep 8, 2023

Conversation

andrewserong
Copy link
Contributor

@andrewserong andrewserong commented Aug 25, 2023

What?

Fixes #14744, part of #16479. Previous attempt: #39243

This PR explores adding a Background block support, with a single backgroundImage property to begin with. The Group block is opted-in to this support.

Note: this PR originally proposed a Media block support as the container for this sub-feature. The PR currently reflects a Background block support, and the final naming is not (yet) decided — this is currently in discussion in the comments.

Why?

As discussed in #14744, it is useful for more container blocks to have background image support, rather than just the Cover block. The approach in this PR adds in background image support (within Background) as a generalised block support so that more blocks could use the feature in the future, too. Of note: the approach in this PR dynamically injects the background image and sizing at render time, which gives the block support flexibility, and means we don't need to worry about block deprecations.

How?

  • Add a Background block support and panel to the block inspector
  • Add a Background Image control and option for the background block support (based on the Media controls in the Site Logo block)
  • Opt the Group block in to using this block support
  • Dynamically render an inline style for the background image when an image is set, and default to a background size of cover (with the ability for us to customise this value in follow-ups)

Not covered

  • If the approach here becomes viable, then in follow-ups we could add in additional controls and properties for things like:
    • Background position (as in the Cover block)
    • Background repeat
    • Background size for repeating (to control how large or small repeated background images are)

Testing Instructions

  1. In a blocks theme that uses appearanceTools (e.g. TT3) add a Group block to a post or page, with a few blocks within it, and maybe some padding.
  2. In the block inspector at the right hand side, you should now see a Media panel.
  3. Explore adding an image from the media library.
  4. Save the post and load the site frontend, and ensure the background image renders.

To-do

  • Fix skipSerialization (currently the styles are being serialized when they shouldn't be)

Screenshots or screencast

image

The following screengrab demos adding a background image with a transparent background, and then selecting a background image that has no transparency:

2023-08-30.15.04.23.mp4

@andrewserong andrewserong added [Type] Enhancement A suggestion for improvement. [Block] Group Affects the Group Block (and row, stack and grid variants) [Feature] Design Tools Tools that impact the appearance of blocks both to expand the number of tools and improve the experi [Package] Style Engine /packages/style-engine labels Aug 25, 2023
@andrewserong andrewserong self-assigned this Aug 25, 2023
@github-actions
Copy link

github-actions bot commented Aug 25, 2023

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/block-supports/background.php
❔ lib/class-wp-theme-json-gutenberg.php
❔ lib/load.php
❔ phpunit/style-engine/style-engine-test.php

@github-actions
Copy link

github-actions bot commented Aug 25, 2023

Size Change: +9.74 kB (+1%)

Total Size: 1.53 MB

Filename Size Change
build/block-editor/index.min.js 216 kB +1.05 kB (0%)
build/block-editor/style-rtl.css 15.3 kB +202 B (+1%)
build/block-editor/style.css 15.3 kB +200 B (+1%)
build/block-library/index.min.js 204 kB +95 B (0%)
build/components/index.min.js 255 kB +7.68 kB (+3%)
build/components/style-rtl.css 11.7 kB -69 B (-1%)
build/components/style.css 11.7 kB -68 B (-1%)
build/data/index.min.js 8.84 kB +456 B (+5%) 🔍
build/edit-site/index.min.js 90.9 kB +54 B (0%)
build/edit-site/style.css 13.2 kB +1 B (0%)
build/style-engine/index.min.js 1.97 kB +126 B (+7%) 🔍
ℹ️ 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.28 kB
build/autop/index.min.js 2.1 kB
build/blob/index.min.js 451 B
build/block-directory/index.min.js 7.01 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.25 kB
build/block-editor/content.css 4.24 kB
build/block-editor/default-editor-styles-rtl.css 381 B
build/block-editor/default-editor-styles.css 381 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 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/avatar/style-rtl.css 104 B
build/block-library/blocks/avatar/style.css 104 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 629 B
build/block-library/blocks/button/style.css 628 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 421 B
build/block-library/blocks/columns/style.css 421 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.63 kB
build/block-library/blocks/cover/style.css 1.62 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 98 B
build/block-library/blocks/details/style.css 98 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/style-rtl.css 280 B
build/block-library/blocks/file/style.css 281 B
build/block-library/blocks/file/view-interactivity.min.js 317 B
build/block-library/blocks/file/view.min.js 375 B
build/block-library/blocks/footnotes/style-rtl.css 201 B
build/block-library/blocks/footnotes/style.css 199 B
build/block-library/blocks/freeform/editor-rtl.css 2.61 kB
build/block-library/blocks/freeform/editor.css 2.61 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/style-rtl.css 1.42 kB
build/block-library/blocks/image/style.css 1.41 kB
build/block-library/blocks/image/theme-rtl.css 126 B
build/block-library/blocks/image/theme.css 126 B
build/block-library/blocks/image/view-interactivity.min.js 1.83 kB
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 505 B
build/block-library/blocks/media-text/style.css 503 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.26 kB
build/block-library/blocks/navigation/editor.css 2.26 kB
build/block-library/blocks/navigation/style-rtl.css 2.23 kB
build/block-library/blocks/navigation/style.css 2.22 kB
build/block-library/blocks/navigation/view-interactivity.min.js 988 B
build/block-library/blocks/navigation/view-modal.min.js 2.85 kB
build/block-library/blocks/navigation/view.min.js 469 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 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 235 B
build/block-library/blocks/paragraph/editor.css 235 B
build/block-library/blocks/paragraph/style-rtl.css 335 B
build/block-library/blocks/paragraph/style.css 335 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 215 B
build/block-library/blocks/post-navigation-link/style.css 214 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 125 B
build/block-library/blocks/preformatted/style.css 125 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 168 B
build/block-library/blocks/pullquote/theme.css 168 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 302 B
build/block-library/blocks/query-pagination/style.css 299 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/query/style-rtl.css 370 B
build/block-library/blocks/query/style.css 368 B
build/block-library/blocks/query/view.min.js 559 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 607 B
build/block-library/blocks/search/style.css 607 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 631 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 204 B
build/block-library/blocks/site-logo/style.css 204 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 682 B
build/block-library/blocks/social-links/editor.css 681 B
build/block-library/blocks/social-links/style-rtl.css 1.44 kB
build/block-library/blocks/social-links/style.css 1.43 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 432 B
build/block-library/blocks/table/editor.css 432 B
build/block-library/blocks/table/style-rtl.css 639 B
build/block-library/blocks/table/style.css 639 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 185 B
build/block-library/blocks/video/style.css 185 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/reset-rtl.css 478 B
build/block-library/reset.css 478 B
build/block-library/style-rtl.css 13.8 kB
build/block-library/style.css 13.8 kB
build/block-library/theme-rtl.css 688 B
build/block-library/theme.css 693 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 51.4 kB
build/commands/index.min.js 15.5 kB
build/commands/style-rtl.css 932 B
build/commands/style.css 929 B
build/compose/index.min.js 12.1 kB
build/core-commands/index.min.js 2.6 kB
build/core-data/index.min.js 16.8 kB
build/customize-widgets/index.min.js 12 kB
build/customize-widgets/style-rtl.css 1.46 kB
build/customize-widgets/style.css 1.45 kB
build/data-controls/index.min.js 640 B
build/date/index.min.js 17.8 kB
build/deprecated/index.min.js 451 B
build/dom-ready/index.min.js 324 B
build/dom/index.min.js 4.64 kB
build/edit-post/classic-rtl.css 544 B
build/edit-post/classic.css 545 B
build/edit-post/index.min.js 35.3 kB
build/edit-post/style-rtl.css 7.6 kB
build/edit-post/style.css 7.59 kB
build/edit-site/style-rtl.css 13.2 kB
build/edit-widgets/index.min.js 16.9 kB
build/edit-widgets/style-rtl.css 4.52 kB
build/edit-widgets/style.css 4.52 kB
build/editor/index.min.js 45.5 kB
build/editor/style-rtl.css 3.53 kB
build/editor/style.css 3.52 kB
build/element/index.min.js 4.82 kB
build/escape-html/index.min.js 537 B
build/format-library/index.min.js 7.71 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/interactivity/index.min.js 11.3 kB
build/is-shallow-equal/index.min.js 527 B
build/keyboard-shortcuts/index.min.js 1.72 kB
build/keycodes/index.min.js 1.87 kB
build/list-reusable-blocks/index.min.js 2.2 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/nux/index.min.js 1.99 kB
build/nux/style-rtl.css 735 B
build/nux/style.css 732 B
build/patterns/index.min.js 2.71 kB
build/patterns/style-rtl.css 240 B
build/patterns/style.css 240 B
build/plugins/index.min.js 1.79 kB
build/preferences-persistence/index.min.js 1.84 kB
build/preferences/index.min.js 1.24 kB
build/primitives/index.min.js 943 B
build/priority-queue/index.min.js 1.52 kB
build/private-apis/index.min.js 958 B
build/react-i18n/index.min.js 615 B
build/react-refresh-entry/index.min.js 9.47 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.7 kB
build/reusable-blocks/style-rtl.css 243 B
build/reusable-blocks/style.css 243 B
build/rich-text/index.min.js 11 kB
build/router/index.min.js 1.78 kB
build/server-side-render/index.min.js 1.94 kB
build/shortcode/index.min.js 1.39 kB
build/sync/index.min.js 53.8 kB
build/token-list/index.min.js 582 B
build/url/index.min.js 3.73 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 958 B
build/warning/index.min.js 249 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

return $block_content;
}

// TODO: should we get the style engine to handle `backgroundImage`?
Copy link
Contributor

Choose a reason for hiding this comment

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

That would make sense to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great! I made a start on looking at adding it to the style engine but ran into a couple of edge cases. I might leave things here for now and split out a separate PR for looking at adding in backgroundImage / backgroundSize to the style engine.

Copy link
Member

Choose a reason for hiding this comment

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

Out of interest, what were the edge cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Out of interest, what were the edge cases?

It turns out the edge cases were me doing things wrong 😄

I think I've sorted it out now, I've pushed a commit in b836572 that seems to work for now. Since this PR is getting pretty big, though, if the approach in this PR winds up being viable, I can split out those changes in a separate PR to make it easier for review.

@scruffian
Copy link
Contributor

Once I have uploaded an image, if I try to change it I can open the media library and select a video, which doesn't work....


.block-editor-hooks__media__inspector-upload-container,
.block-editor-hooks__media__inspector-media-replace-container {
button.components-button {
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like we should make this a variation of the button component rather than having a custom rule for it here. That should be a different PR though.

@youknowriad
Copy link
Contributor

  • I've noticed that the styles are duplicated in the frontend.
  • I like that the style engine (JS one) also supports this.
  • If I'm not wrong, the cover block uses a separate div for this, so I wonder if it's possible to actually unify the cover with this behavior at some point (I prefer to avoid extra DOM nodes here)
  • Any thoughts on global styles support for this?

@youknowriad
Copy link
Contributor

I can't seem to make both "color background" and "image background" work at the same time? what about with gradients?

@jameskoster
Copy link
Contributor

Excited to see this under way!

I know you mentioned it wont be covered in this PR, but in terms of design it would be good to understand the broader vision for this. Is the intention to replicate the Cover experience, which is quite prescriptive? Or should we aim for something more flexible with options like background-size/position/repeat (as suggested), stacked backgrounds (images and colors), and so on?

cc @WordPress/theme-team for thoughts.

@draganescu
Copy link
Contributor

I am definitely missing a lot of context and knowledge but as I have explored a supports icon idea in the Icon/Vector registration API #53510 discussion and the test plugin - Demo of WordPress Icon API - I think a media supports should not mean background styles.

Why is support media = background media? The block should decide what to do with the media. The support opts in into a standard way of picking media.

@paaljoachim
Copy link
Contributor

paaljoachim commented Aug 26, 2023

It would actually be great if we could create a standarized Media settings panel.
For media, URL (Youtube, Vimeo etc) or something else.

Having a focal picker would also be nice. Having a Media settings panel for any block that can use media.
Screenshot 2023-08-25 at 21 45 41

I do see the Media settings evolve which can be used for various blocks.

@github-actions
Copy link

github-actions bot commented Aug 28, 2023

Flaky tests detected in d501bd6.
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/6105917677
📝 Reported issues:

@andrewserong
Copy link
Contributor Author

andrewserong commented Aug 28, 2023

Thanks for the early reviewing and testing, everyone! There were lots of comments, so I'll reply here, a little out of sequence. Apologies if I missed anything!

I've noticed that the styles are duplicated in the frontend.

I've fixed this up — styles should only be output once, and they shouldn't be serialized to post content. This allows us to look at more dynamic behaviour in follow-ups such as setting background to Featured Image.

Any thoughts on global styles support for this?

Global styles is an interesting case. I imagine blocks that could use it could include Post Content. But personally I'd be very interested in a root-global styles control for media/background image, particularly to set site-wide patterned backgrounds (like those in https://www.transparenttextures.com/). Since there would be lots of decisions to be made about how we'd implement it, I'm leaning toward us starting with just the block level for now, and build it out in global styles in follow-ups. This is another reason to keep all styles injection dynamic, so that we can change things in the future.

I can't seem to make both "color background" and "image background" work at the same time? what about with gradients?

In contrast to Cover block, this block support doesn't do layering / overlays, so background color will only work with transparent images, but it does appear to work. There is a styling conflict with gradients, though, as gradients use the background property, which overrides background-image. If / when this PR winds up landing, I reckon adding in mixed gradient / background image support (via multiple values output within a single background-image property) would be good to explore. I'd leave mixed gradient + transparent background images to follow-ups, though. Here's a screengrab with a transparent image:

2023-08-28.12.23.39.mp4

Or should we aim for something more flexible with options like background-size/position/repeat (as suggested), stacked backgrounds (images and colors), and so on?

My intention with this PR is for us to start out as simple as possible, and then add more advanced controls in follow-ups (potentially hidden by default in the ToolsPanel). I'd love to see control over background size within repeat, so that folks can scale up/down their repeating images as desired. But again, for follow-ups — would be happy to see some design explorations for how the current PR might scale, though!

I am definitely missing a lot of context and knowledge but as I have explored a supports icon idea in the Icon/Vector registration API #53510 discussion and the test plugin - Demo of WordPress Icon API - I think a media supports should not mean background styles. Why is support media = background media?

Thanks for the feedback! The intention wasn't for media === background media, but rather to create a Media block support, and for the first feature within it, to add a background image support. In follow-ups, we might create all kinds of other media controls, but I think media could make for a good and consistent bucket for it. To make the hierarchy clearer, I've refactored the code a bit, so that the background image controls are in a separate sub-component, and I've updated the button label for clarity:

image

I do see the Media settings evolve which can be used for various blocks.

Thanks! Yes, my hope is that if we settle on Media as a panel, it could help with consistency across blocks, too 👍

Once I have uploaded an image, if I try to change it I can open the media library and select a video, which doesn't work....

This should be fixed now, and I've added a notice for if the media type is incorrect on upload. Let me know if you're still encountering it, though:

image


Thanks again for all the feedback, folks! Let me know if there's other changes anyone would like to see. My objective for now is to try to focus on a decent MVP, and hopefully defer further features to follow-ups. I still have a little cleaning up to do with the PHP code, which I ran out of time to do today. Once that's done, and pending any design feedback / ideas, I'll switch this over to ready for review.

@youknowriad
Copy link
Contributor

I've fixed this up — styles should only be output once, and they shouldn't be serialized to post content. This allows us to look at more dynamic behaviour in follow-ups such as setting background to Featured Image.

This is I think not the first place where we favor server rendering over post content serialization for styles. The downside of this approach is that it breaks these blocks for third-party block editors. These customization can in theory be easily supported for server less block editors but we don't have the functions that render the right HTML for that (including all these dynamic styles that are static in reality). It would be good to keep that in mind and maybe keep track of all these static/dynamic styles to implement the wp.blocks.render function properly.

@andrewserong
Copy link
Contributor Author

andrewserong commented Aug 29, 2023

This is I think not the first place where we favor server rendering over post content serialization for styles. The downside of this approach is that it breaks these blocks for third-party block editors.

Thanks for raising this! I've added a comment over on the Gutenberg as Framework issue, too (#53874 (comment)). Up until this point, I'd assumed that it was desirable for future block supports to be dynamically output via the style engine in both JS and PHP for the reasons discussed back in #37495, so I'm keen to better understand if there's been a shift in direction there (or if I've misunderstood, of course 🙂).

In the case of this PR, the main reasons for dynamic output (even of static styles) are:

  • To support filterable / dynamic images in follow-ups. Examples include potentially retrieving image url via id, or switching to a featured image, or one day images that form part a theme's assets.
  • To allow themes or plugins to filter or switch off output, without breaking markup.
  • To allow flexibility in changing output in follow-ups without requiring deprecations.
  • To ensure that dependent static rules are only output when needed (so in this case background-size is only output when background-image is output).

It would be good to keep that in mind and maybe keep track of all these static/dynamic styles to implement the wp.blocks.render function properly.

Yes, it'd be good to ensure all features can be rendered in the same way in JS or PHP contexts. For this one, since we're adding the logic to the JS version of the style engine, depending on how we might want dynamic JS injection to work in the future (possibly at block render time?), I think overall we're heading in the right direction for that? Please do let me know if I'm missing something though, as I might have missed some of the context behind the thinking in #53874.

Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

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

Thanks for getting this up to test. I reckon it'll open up a bunch of pattern possibilities!

Behold my creation!

2023-08-29.12.27.52.mp4

@@ -17,4 +18,5 @@ export const styleDefinitions = [
...spacing,
...typography,
...shadow,
...media,
Copy link
Member

Choose a reason for hiding this comment

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

At first I questioned the media top-level grouping, wondering if we could use background, given it has a lot of related properties.

I think it's a good decision however since most, probably all, of those properties manipulate a background element most likely to be an image, and then we'd also have a neat home for stuff like object-fit and anything related to replaced elements (videos, canvas, etc)

return $block_content;
}

// TODO: should we get the style engine to handle `backgroundImage`?
Copy link
Member

Choose a reason for hiding this comment

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

Out of interest, what were the edge cases?

@@ -300,6 +300,10 @@ const BlockInspectorSingleBlock = ( { clientId, blockName } ) => {
label={ __( 'Border' ) }
/>
<InspectorControls.Slot group="styles" />
<InspectorControls.Slot
group="media"
label={ __( 'Media' ) }
Copy link
Member

Choose a reason for hiding this comment

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

This is most likely overthinking it, but I was wondering if this might be confused with the other "Media", the library? Since it's specific to backgrounds for now, what about "Background media"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question! For the panel title, I was thinking of trying to keep it generic to Media for consistency with the Site Logo block that already has a Media panel, and leave the specifics to the individual block support within it. The idea being that we then have a consistent panel across any block that has media, irrespective of where that media winds up being output.

That was just my initial thinking though, happy to change things around! For now, I might leave it as-is while we await further design feedback 🙂

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me

@andrewserong andrewserong marked this pull request as ready for review August 29, 2023 02:53
@draganescu draganescu merged commit 9c56c67 into trunk Sep 8, 2023
@draganescu draganescu deleted the add/media-block-support-with-background-image branch September 8, 2023 08:50
@github-actions github-actions bot added this to the Gutenberg 16.7 milestone Sep 8, 2023
@andrewserong
Copy link
Contributor Author

Thank you for all the collaboration here, everyone! 🙇

@andrewserong
Copy link
Contributor Author

I've opened up a follow-up issue for ideas for future enhancements over in #54336. Feel free to add any ideas there! I imagine most enhancements other than #54319 (add Reset option) will likely be for beyond 6.4.

@andrewserong
Copy link
Contributor Author

I've opened a backport PR for the PHP parts of this feature over in WordPress/wordpress-develop#5209

@andrewserong
Copy link
Contributor Author

Related to the backport PR, I've opened up a separate Gutenberg PR to add in some more PHP tests over in #54489

@westonruter
Copy link
Member

westonruter commented Oct 10, 2023

Re: #53934 (comment)

But I wanted to add that another benefit of skipping serialization (at least for now) is that we can easily swap out the background-image CSS for an img element that supports srcset and SVG filters.

@ajlende Another benefit of an img element is that WordPress can optimize its loading by adding the fetchpriority="high" attribute. WordPress doesn't have any such optimizations currently for background-image, although this is something I'm exploring.

Example comparing background images in Group block versus Cover block, where only latter gets fetchpriority=high correctly: https://gist.github.com/westonruter/107f0c827bbee12341441c527ea2ed70

@andrewserong
Copy link
Contributor Author

andrewserong commented Oct 16, 2023

Draft dev note:


Background image block support

In WordPress 6.4, a new background image block support has been added, with the Group block opted-in by default. For themes using the appearanceTools feature in theme.json, the control will be available in the inspector controls, under Background.

The feature allows users to set a background image for a Group block. The selected image will be output at render-time for the block via an inline style applied to the block's wrapper. When a background image is set, the rule background-size: cover is output along with the background-image property. This ensures that any background images adequately cover the block. Ideas for enhancements in future releases are being tracked in this Github issue: #54336

How to add backgroundImage support to a theme

There are two ways to add support for backgroundImage to a block theme. The simplest is to opt in to the appearanceTools setting, which automatically enables a number of design tools (read more in the developer handbook).

For themes that wish to have more granular control over which UI tools are enabled, the backgroundImage support can be opted into by setting settings.background.backgroundImage to true in theme.json. For example:

{
	"settings": {
		"background": {
			"backgroundImage": true

Note that as of WP 6.4, the backgroundImage support is only available at the individual block level, and not in global styles or at the site root. These features will be explored in subsequent releases, with progress tracked in this Github issue: #54336.

@ramonjd
Copy link
Member

ramonjd commented Apr 23, 2024

This is I think not the first place where we favor server rendering over post content serialization for styles. The downside of this approach is that it breaks these blocks for third-party block editors.

In the case of this PR, the main reasons for dynamic output (even of static styles) are:

To allow flexibility in changing output in follow-ups without requiring deprecations.

I'm looking at trying to prevent color.gradient, which outputs its value using the background shorthand CSS property, from overwriting all background-* CSS properties in #60739.

Gutenberg has backed itself into a pretty tight corner by using background in this way. Ideally background-image should be used for images and linear gradients and combined where both exist.

Because we're skipping serialization for background block supports however, to achieve the above means that some
complicated deduplication and string manipulation needs to occur on the server where a block has both a gradient and background image:

  1. When the incoming block has a background gradient applied in the inline CSS - here I want to remove that CSS and use background-image only.
  2. Where a background gradient preset is applied via classname (which all have !important in the corresponding CSS rule) - the classname should be removed in favour of a combined background-image value.

Just flagging that changing output is not as flexible or convenient as it one might assume.

To ensure that dependent static rules are only output when needed (so in this case background-size is only output when background-image is output).

Relative to background-image, is this such a bad thing? If an image has been reset in the editor, Gutenberg can remove the style properties at that point. If background-size is set elsewhere, e.g., in theme.json and there's not image, I don't see why Gutenberg should care.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Group Affects the Group Block (and row, stack and grid variants) [Feature] Design Tools Tools that impact the appearance of blocks both to expand the number of tools and improve the experi Needs Design Feedback Needs general design feedback. Needs User Documentation Needs new user documentation [Package] Style Engine /packages/style-engine [Type] Enhancement A suggestion for improvement.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Group Block - add support for Background Images