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

Try using variable for root padding value #40926

Closed
wants to merge 8 commits into from
Closed

Conversation

scruffian
Copy link
Contributor

@scruffian scruffian commented May 9, 2022

What?

This is a new version of #39926. I squashed all the commits to make the rebase easier, but I wanted to keep the history, so I opened a new PR instead. Props to @tellthemachines for all the work on the original PR.

This:

  • Adds a setting to theme.json so themes can opt in to the new behaviour of root padding
  • Outputs that value as a custom property on the root element
  • Allows full-width blocks to compensate for that value with negative margins

Why?

Partly fixes #35607 - only addresses the root block part of the equation. #39871 deals with full width in nested blocks.
Fixes #35884 for themes that opt into this change.

Themes shouldn't have to add their own rules to make full width alignments work properly.

How?

This allows users to set a global body padding in the global styles interface.

Then, on the server side, we set top and bottom padding to the root container, but left and right padding to the direct children of root. This is so that those children still stretch full width as expected, but their children get the effects of the padding.

On the site editor side, the direct children of root container get the left/right padding applied, so they still appear as full-width. (If we wanted to only apply full-width to some of the root children, maybe we could add padding to the root and enable the align control on them so they could be set manually? And add negative margins to alignfull blocks)

On the post editor side, the root container also gets its left/right padding, and any alignfull children are fixed with negative margins.

This is achieved using the alignment rules that are currently used by block themes.

Testing Instructions

  1. Add "useRootVariables": true to the settings of your theme's theme.json to enable these changes.
  2. Add a few layout blocks (e.g. Group) to the post and the site editor
  3. Make some of those blocks full width
  4. Check that there's a default padding value in both editors and front end, and full width blocks behave as expected
  5. Change root padding value in the site editor by going to Global Styles > Layout > Padding
  6. Check that new padding value works in both editors and front end.
  7. Check that removing "useRootVariables": true from theme.json reverts to the previous behaviour.

Screenshots or screencast

ezgif com-gif-maker (5)

You can also use this block markup to test: https://gist.github.com/scruffian/df0ce92c85cffdfee0d69fbe2bcb8995

cc @WordPress/block-themers

@scruffian scruffian force-pushed the add/root-padding branch 2 times, most recently from 705dad3 to a768407 Compare May 9, 2022 12:02
@github-actions
Copy link

github-actions bot commented May 9, 2022

Size Change: +867 B (0%)

Total Size: 1.24 MB

Filename Size Change
build/block-editor/index.min.js 151 kB +15 B (0%)
build/block-library/blocks/columns/style-rtl.css 453 B +47 B (+12%) ⚠️
build/block-library/blocks/columns/style.css 453 B +47 B (+12%) ⚠️
build/block-library/blocks/cover/style-rtl.css 1.58 kB +48 B (+3%)
build/block-library/blocks/cover/style.css 1.58 kB +48 B (+3%)
build/block-library/blocks/group/theme-rtl.css 134 B +56 B (+72%) 🆘
build/block-library/blocks/group/theme.css 133 B +55 B (+71%) 🆘
build/block-library/style-rtl.css 11.6 kB +50 B (0%)
build/block-library/style.css 11.6 kB +48 B (0%)
build/block-library/theme-rtl.css 723 B +34 B (+5%) 🔍
build/block-library/theme.css 728 B +34 B (+5%) 🔍
build/blocks/index.min.js 47.1 kB +55 B (0%)
build/edit-site/index.min.js 48.2 kB +330 B (+1%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 993 B
build/annotations/index.min.js 2.77 kB
build/api-fetch/index.min.js 2.27 kB
build/autop/index.min.js 2.15 kB
build/blob/index.min.js 487 B
build/block-directory/index.min.js 6.51 kB
build/block-directory/style-rtl.css 1.01 kB
build/block-directory/style.css 1.01 kB
build/block-editor/default-editor-styles-rtl.css 378 B
build/block-editor/default-editor-styles.css 378 B
build/block-editor/style-rtl.css 14.6 kB
build/block-editor/style.css 14.6 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 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 111 B
build/block-library/blocks/audio/style.css 111 B
build/block-library/blocks/audio/theme-rtl.css 125 B
build/block-library/blocks/audio/theme.css 125 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 445 B
build/block-library/blocks/button/editor.css 445 B
build/block-library/blocks/button/style-rtl.css 560 B
build/block-library/blocks/button/style.css 560 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/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 127 B
build/block-library/blocks/comment-template/style.css 127 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 546 B
build/block-library/blocks/cover/editor.css 547 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 417 B
build/block-library/blocks/embed/style.css 417 B
build/block-library/blocks/embed/theme-rtl.css 124 B
build/block-library/blocks/embed/theme.css 124 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 255 B
build/block-library/blocks/file/style.css 255 B
build/block-library/blocks/file/view.min.js 353 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 961 B
build/block-library/blocks/gallery/editor.css 964 B
build/block-library/blocks/gallery/style-rtl.css 1.51 kB
build/block-library/blocks/gallery/style.css 1.51 kB
build/block-library/blocks/gallery/theme-rtl.css 122 B
build/block-library/blocks/gallery/theme.css 122 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/heading/style-rtl.css 76 B
build/block-library/blocks/heading/style.css 76 B
build/block-library/blocks/html/editor-rtl.css 332 B
build/block-library/blocks/html/editor.css 333 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 529 B
build/block-library/blocks/image/style.css 535 B
build/block-library/blocks/image/theme-rtl.css 124 B
build/block-library/blocks/image/theme.css 124 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 708 B
build/block-library/blocks/navigation-link/editor.css 706 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 299 B
build/block-library/blocks/navigation-submenu/editor.css 299 B
build/block-library/blocks/navigation-submenu/view.min.js 375 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.95 kB
build/block-library/blocks/navigation/style.css 1.94 kB
build/block-library/blocks/navigation/view-modal.min.js 2.78 kB
build/block-library/blocks/navigation/view.min.js 395 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 69 B
build/block-library/blocks/post-comments-form/editor.css 69 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 628 B
build/block-library/blocks/post-comments/style.css 628 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 721 B
build/block-library/blocks/post-featured-image/editor.css 721 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 323 B
build/block-library/blocks/post-template/style.css 323 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 397 B
build/block-library/blocks/search/style.css 398 B
build/block-library/blocks/search/theme-rtl.css 64 B
build/block-library/blocks/search/theme.css 64 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 474 B
build/block-library/blocks/shortcode/editor.css 474 B
build/block-library/blocks/site-logo/editor-rtl.css 759 B
build/block-library/blocks/site-logo/editor.css 759 B
build/block-library/blocks/site-logo/style-rtl.css 181 B
build/block-library/blocks/site-logo/style.css 181 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 332 B
build/block-library/blocks/spacer/editor.css 332 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 504 B
build/block-library/blocks/table/editor.css 504 B
build/block-library/blocks/table/style-rtl.css 625 B
build/block-library/blocks/table/style.css 625 B
build/block-library/blocks/table/theme-rtl.css 188 B
build/block-library/blocks/table/theme.css 188 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 173 B
build/block-library/blocks/video/style.css 173 B
build/block-library/blocks/video/theme-rtl.css 124 B
build/block-library/blocks/video/theme.css 124 B
build/block-library/common-rtl.css 993 B
build/block-library/common.css 990 B
build/block-library/editor-rtl.css 10.2 kB
build/block-library/editor.css 10.3 kB
build/block-library/index.min.js 180 kB
build/block-library/reset-rtl.css 478 B
build/block-library/reset.css 478 B
build/block-serialization-default-parser/index.min.js 1.12 kB
build/block-serialization-spec-parser/index.min.js 2.83 kB
build/components/index.min.js 227 kB
build/components/style-rtl.css 14.5 kB
build/components/style.css 14.5 kB
build/compose/index.min.js 11.7 kB
build/core-data/index.min.js 14.6 kB
build/customize-widgets/index.min.js 11.2 kB
build/customize-widgets/style-rtl.css 1.39 kB
build/customize-widgets/style.css 1.39 kB
build/data-controls/index.min.js 663 B
build/data/index.min.js 7.98 kB
build/date/index.min.js 32 kB
build/deprecated/index.min.js 518 B
build/dom-ready/index.min.js 336 B
build/dom/index.min.js 4.69 kB
build/edit-navigation/index.min.js 16 kB
build/edit-navigation/style-rtl.css 4.05 kB
build/edit-navigation/style.css 4.05 kB
build/edit-post/classic-rtl.css 546 B
build/edit-post/classic.css 547 B
build/edit-post/index.min.js 30.4 kB
build/edit-post/style-rtl.css 7.08 kB
build/edit-post/style.css 7.08 kB
build/edit-site/style-rtl.css 7.73 kB
build/edit-site/style.css 7.71 kB
build/edit-widgets/index.min.js 16.4 kB
build/edit-widgets/style-rtl.css 4.4 kB
build/edit-widgets/style.css 4.4 kB
build/editor/index.min.js 38.5 kB
build/editor/style-rtl.css 3.71 kB
build/editor/style.css 3.7 kB
build/element/index.min.js 4.3 kB
build/escape-html/index.min.js 548 B
build/format-library/index.min.js 6.62 kB
build/format-library/style-rtl.css 571 B
build/format-library/style.css 571 B
build/hooks/index.min.js 1.66 kB
build/html-entities/index.min.js 454 B
build/i18n/index.min.js 3.79 kB
build/is-shallow-equal/index.min.js 535 B
build/keyboard-shortcuts/index.min.js 1.83 kB
build/keycodes/index.min.js 1.41 kB
build/list-reusable-blocks/index.min.js 1.75 kB
build/list-reusable-blocks/style-rtl.css 835 B
build/list-reusable-blocks/style.css 835 B
build/media-utils/index.min.js 2.9 kB
build/notices/index.min.js 957 B
build/nux/index.min.js 2.1 kB
build/nux/style-rtl.css 744 B
build/nux/style.css 741 B
build/plugins/index.min.js 1.98 kB
build/preferences-persistence/index.min.js 2.23 kB
build/preferences/index.min.js 1.32 kB
build/primitives/index.min.js 949 B
build/priority-queue/index.min.js 628 B
build/react-i18n/index.min.js 704 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.24 kB
build/reusable-blocks/style-rtl.css 256 B
build/reusable-blocks/style.css 256 B
build/rich-text/index.min.js 11.2 kB
build/server-side-render/index.min.js 1.61 kB
build/shortcode/index.min.js 1.52 kB
build/token-list/index.min.js 668 B
build/url/index.min.js 1.99 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 280 B
build/widgets/index.min.js 7.2 kB
build/widgets/style-rtl.css 1.16 kB
build/widgets/style.css 1.16 kB
build/wordcount/index.min.js 1.07 kB

compressed-size-action

@scruffian scruffian force-pushed the add/root-padding branch from 30a66a6 to a536b12 Compare May 9, 2022 12:39
@scruffian scruffian marked this pull request as ready for review May 10, 2022 15:15
Copy link
Contributor

@andrewserong andrewserong 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 splitting this out to a separate PR @scruffian! I really appreciated the testing gist, too, that made it much easier to play around with quickly 👍

It's looking quite close to me, but I ran into a few issues:

1. Body padding

Is the root body still meant to be applying padding with this setting enabled? It looks like I'm getting a double padding happening (so padding is applied directly on body as well as the CSS variable-based left/right padding on .wp-site-blocks). In the following screenshot, there should be a single 30px padding, but I see two sets of padding here, so the root blocks in my site effectively are 60px padded and I can't get it to go full width:

image

If I manually disable padding on body in DevTools then everything renders as I'd expect:

image

I'm testing using emptytheme and spacing.padding set to 30px in my theme.json file's styles object.

2. Variable output when flag is switched off

With the useRootVariables flag switched off the CSS variables are still output in the front-end of the site (it's just the styles that use these CSS variables that aren't being generated). Is that intentional?

3. How the variable is output when there is no value

If there is no value for spacing.padding then the variables appear to be generated incorrectly (no value is output):

image

Should there either be a default value, or if there's no value, then don't output the variable? (If we don't output the variable, then I suppose the styles that use that variable should have a fallback value?)

4. How this feature works in TwentyTwentyTwo (or existing themes)

In TwentyTwentyTwo, switching on the setting results in a horizontal scrollbar in the site editor. I'm guessing this is because the theme will needed to be updated to factor in the new setting? (This isn't a blocker, just wanted to mention it in case it's an issue — given that this new feature is guarded behind a flag, it seems fine to me if themes don't immediately support it once the feature lands 👍)

2022-05-11 16 02 09


Thanks again for continuing on with this feature, it'll be really great to land it. Please let me know if you need any more info from my testing, or if I missed a step! 🙇

phpunit/class-wp-theme-json-test.php Outdated Show resolved Hide resolved
lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php Outdated Show resolved Hide resolved
@scruffian
Copy link
Contributor Author

scruffian commented May 11, 2022

Thanks for testing @andrewserong.

  1. Body padding

I can't reproduce this when I don't have the "useRootVariables": true setting in your theme.json file, so this is the behaviour I'd expect in that case. Are you seeing it when you add that setting in?
Edit: I worked out the problem here - you were setting padding as a string not as an object. I didn't know we supported that! I've added a fix for that case.

@scruffian
Copy link
Contributor Author

With the useRootVariables flag switched off the CSS variables are still output in the front-end of the site (it's just the styles that use these CSS variables that aren't being generated). Is that intentional?

No! I have disabled this.

@scruffian
Copy link
Contributor Author

Should there either be a default value, or if there's no value,

Yeah I think so, I've made it 0

@scruffian
Copy link
Contributor Author

scruffian commented May 11, 2022

In TwentyTwentyTwo, switching on the setting results in a horizontal scrollbar in the site editor. I'm guessing this is because the theme will needed to be updated to factor in the new setting? (This isn't a blocker, just wanted to mention it in case it's an issue — given that this new feature is guarded behind a flag, it seems fine to me if themes don't immediately support it once the feature lands 👍)

Yeah I think that's because we also need to remove the custom CSS from the theme that deals with this, so this is expected. @jffng made this PR for TT2: WordPress/wordpress-develop#2578

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Wonderful, thanks for implementing all those follow-ups @scruffian! This is testing nicely for me now:

✅ Root variables are not output when useRootVariables is set to false
✅ If there is no padding value, a default value of 0 is used
✅ Duplicate padding issue has been resolved
✅ Confirmed that for existing themes there is no change with this PR applied if it does not opt-in to the new useRootVariables setting

Padding on site blocks looks good now Negative left/right margins applies correctly to full width items
image image
0 fallback value is output correctly Variables are not output if setting is false
image image

Good to know that the fix for TwentyTwentyTwo is already in the works 👍, and given that this is an opt-in feature guarded behind a flag that defaults to false, and it's testing well, I think this PR looks in a good shape to merge. Then, folks can test it out in their themes by opt-ing in and having a play around to see if there are other tweaks we need to make in follow-ups. Thanks again for finishing this off, and kudos again to @tellthemachines!

LGTM! 🎉

@@ -445,7 +447,7 @@ protected static function compute_style_properties( $styles, $settings = array()
// If a variable value is added to the root, the corresponding property should be removed.
foreach ( $root_variable_duplicates as $duplicate ) {
$discard = array_search( $duplicate, array_column( $declarations, 'name' ), true );
if ( $discard ) {
if ( $discard !== false ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for fixing this up: such an interesting edge case here that if the shorthand padding was the first item in the array, then $discard would be 0 (the found "key" for the first item in the array) which is a falsy value, so this would never be reached. Nice catch! 🙇

@andrewserong andrewserong added Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Enhancement A suggestion for improvement. labels May 12, 2022
@ramonjd
Copy link
Member

ramonjd commented May 12, 2022

I've been testing this in a few themes and it works as described for me.

Full width blocks receive the appropriate margins

Screen Shot 2022-05-12 at 3 40 32 pm

"useRootVariables" toggles the behaviour on and off.

Thanks for persevering with this @scruffian

@scruffian
Copy link
Contributor Author

@youknowriad do you want to review this before I merge?

@youknowriad
Copy link
Contributor

To be completely honest, I'm not a fan of this PR. The reason is simple:

I feel we're not addressing this conceptually by bringing a meaningful solution, instead, we're adding some CSS to all themes that target blocks randomly. It feels like hacks to me just to make the things people "usually" do in their themes look as they expect.

The problem is in the "usually", here, if the theme is a bit creative, these styles will quickly become more of a burden than a real solution.

They also don't take third-party blocks into consideration.

--
I also understand the reasons for the existence of the PR and that it's a common pain point for theme developers, and I also don't like to be the blocker that's why I preferred not to engage so far. The reality is that I don't have an alternative solution but I have a feeling that the solution here is just going to bite us in the future.

@scruffian
Copy link
Contributor Author

I think if we want a more generic solution we need to add some kind of attribute to some blocks so that we know to treat them differently. That was the intent of this issue: #26407

I'm happy to look into this more if you think that direction makes senses.

@scruffian
Copy link
Contributor Author

Or perhaps another approach is to simplify things as suggested here: #36082

@ramonjd
Copy link
Member

ramonjd commented May 12, 2022

I think if we want a more generic solution we need to add some kind of attribute to some blocks so that we know to treat them differently. That was the intent of this issue: #26407

I'm also thinking this is a layout problem.

For me it's somehow related to the conceptual nut I've been trying to crack in relation to blockGap.

blockGap also operates as a pseudo-"global/root" variable but we've yet to find a neat way to reconcile its existence with subsequent styles down the chain, most notably block-level global styles, for which blockGap values don't work at all right now.

I see a few challenges:

  1. "Root" global styles (in the ::root/body tag) contain some values that may be required by layout implementation or individual blocks.
  2. To make these values consumable they need to be defined as CSS vars, which is currently done in the Theme_JSON class.
  3. Theme_JSON shouldn't concern itself with layout implementation (I'm assuming)
  4. We don't know and can't control how elements further down the theme.json/DOM tree will use these values (do we care?)

Maybe the approach lies in defining a canonical approach to such "global" styles. They function as subset of presets that future layout implementations can use. I don't know. 🤷

One thing I like about this PR is the --wp--style--root--* nomenclature.

If we decide to expose a set of root CSS vars globally, then I think they should all follow the same naming conventions and share similar assumptions.

::root {
	--wp--style--root--block-gap: 24px;
	--wp--style--root--block-padding-top: 10px;
	...
}

body {
	gap: var(--wp--style--root--block-gap);
	padding-top: var(--wp--style--root--block-padding-top);
  ... 
}

At any rate, they're always defined and need no theme.json flag to turn them on.

Rather, the features that this PR achieves could be a related to an available layout option. See @shaunandrews's mockups here: Suggestion: Simplify the layout concept · Issue #36082 · WordPress/gutenberg · GitHub

@youknowriad
Copy link
Contributor

To be clear the root CSS variables is not what bothers me in this PR. I think it's fine to have root settings to be used by blocks...

We don't know and can't control how elements further down the theme.json/DOM tree will use these values (do we care?)

I think this is where the problem is. In a block theme world:

1- either the the theme.json framework should know how to apply these values consistently and this should work for all themes.
2- either the blocks themselves (block supports and such) should know what to do with these values without any ad-hoc change.

The current PR tries to do 1 (mostly) and a bit of 2 but there is not a convincing story about it. The way it tries to do 1 is not "universal", the way 2 is done is ad-hoc. I think we need to define clearly what we're doing in both cases and make sure that definition translates to all themes and blocks consistently.

@andrewserong
Copy link
Contributor

Thanks for the feedback @youknowriad, you raise great points!

Just confirming that the main concerns here are surrounding the styles that we're outputting in additional_root_selector_rules, that contain a bunch of rules specific to particular blocks? Aside from that, I think the logic looks mostly to be adding the additional CSS vars.

I'm not too sure how we'd do it, but I wonder if a potential direction forward could be related to the idea of outputting layout styles via classname-based presets (like @tellthemachines was exploring in #39708). Basically, if instead of outputting the hard-coded rules in additional_root_selector_rules we had logic that went something like:

  • Output a set of presets for alignment rules (the settings for which could be in theme.json under settings.layout)
  • When looping over styles for particular blocks, look up the block's settings to see if it opts-in to the full-width alignment, and if so, output the preset for full-width alignment, using the classname for that block (or if it disables classname, then using its custom selector) — that might be one way for us to ensure we support 3rd party blocks, if they've opted-in to the full width alignment 🤔

I also don't like to be the blocker that's why I preferred not to engage so far. The reality is that I don't have an alternative solution but I have a feeling that the solution here is just going to bite us in the future.

I know the feeling of not wanting to be a blocker, but thank you for the push-back here, it's very much appreciated! It's always really valuable to flag things that we think might bite us further down the track, even if we don't have an immediate alternative — it's a good sign for us to experiment laterally, perhaps, to see how else we might implement things. 🙇

For me it's somehow related to the conceptual nut I've been trying to crack in relation to blockGap.
blockGap also operates as a pseudo-"global/root" variable but we've yet to find a neat way to reconcile its existence with subsequent styles down the chain, most notably block-level global styles, for which blockGap values don't work at all right now.

Good points @ramonjd — I'm cautiously optimistic that we might be able to take an approach for blockGap where the style generation goes something like:

  • In the theme JSON class, output the fallback styles with lower specificity, using a classname to guard the rule (e.g. is-layout-flex.
  • Also, output the block specific gap styles using the block classname, as we loop over each block in theme.json, so something like .wp-block-group.is-layout-flex if the block in theme.json has a gap value set.
  • Then, in layout.php we only output a gap value if there is a gap value set in the individual block attributes in post content when the block is rendered.

Not sure if that helps here, but just thought I'd share my thinking there (that I'm hacking away at over in #40875) in case it sparks any ideas 😊

@ramonjd
Copy link
Member

ramonjd commented May 13, 2022

When looping over styles for particular blocks, look up the block's settings to see if it opts-in to the full-width alignment, and if so, output the preset for full-width alignment, using the classname for that block (or if it disables classname, then using its custom selector) — that might be one way for us to ensure we support 3rd party blocks, if they've opted-in to the full width alignment

This is an interesting idea. I wonder if we could do most of the work in layout, provided that the block/theme opts into fullWidthShouldStretch or something more inspiring than that.

Words are failing me right now, so I have code: https://github.com/WordPress/gutenberg/pull/41042/files#diff-324697e41855298e2f2c74b078f174e0cbc9075cef736ce9c1e2c169bf64652eR144

@andrewserong
Copy link
Contributor

This is an interesting idea. I wonder if we could do most of the work in layout, provided that the block/theme opts into fullWidthShouldStretch or something more inspiring than that.

Nice! The code example shows that idea well — that way of implementing it looks pretty consistent with the current way we do things with the layout support 👍. Longer-term, for these rules that aren't tied to a value at the individual block attributes, it'd be great to move the generation to global styles so that the style rule is always available (and doesn't fall into the issue we have of rendering container styles in async contexts, etc). But that's probably more part of the work in the exploration in #39708

Thanks for trying the idea in code!

@scruffian scruffian requested a review from ajitbohra as a code owner May 17, 2022 09:42
@scruffian
Copy link
Contributor Author

I started to look into how. we might achieve this by adding a block attribute as suggested above, but in the end I landed on a solution which is simpler. Instead of adding the block specific CSS to the root padding styles, we can just define it in the block itself. @youknowriad What do you think about this approach? Other blocks can opt in by adding that CSS...

@@ -3,3 +3,10 @@
// Matches paragraph block padding.
padding: $block-bg-padding--v $block-bg-padding--h;
}

.wp-block-group.alignfull {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, so I think now I see what bothers me in these styles (same for cover and columns).

Basically, the style here assumes that a "full" aligned group block is necessarily a block that stretches from the left to the right of the window. Meaning it can use the "root" padding value to position itself.

The reality is that it's not the cause. "full" alignment doesn't have any relationship with "root". In other words, we can't use "root" padding values here because we have no idea what the result will be.

For instance:

  • I can use two layouts in my template: one that stretched to the edge of the screen (in which root padding would make sense), but under it I might have another section that is more "centered" but inside it, I can still have "full" blocks, it's just that these "full width" blocks are "full width" within their container and not the whole screen, meaning using "root" padding for these don't make any sense.
  • I can imagine having a "two columns" layout where each column of my site has its own "layout" with its own "widths", meaning there's no relationship here as well between "full width groups" and "root padding".

The only way I can see this PR moving forward is for us willing to accept this:

"full" width alignment means a block stretches from the left edge to the right edge of the window. I think it's a big assumption and a big breaking change from what we've been doing/allowing so far.

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 don't believe that's the case with this PR as it is. Here's a screenshot from Remote, which is a two column theme:
localhost_4759__p=12

Copy link
Contributor

Choose a reason for hiding this comment

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


<!-- wp:columns {"align":"full"} -->
<div class="wp-block-columns alignfull"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:group {"backgroundColor":"vivid-red"} -->
<div class="wp-block-group has-vivid-red-background-color has-background"><!-- wp:paragraph -->
<p>test</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>tes</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>test</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group --></div>
<!-- /wp:column -->

<!-- wp:column {"layout":{"contentSize":"","wideSize":""}} -->
<div class="wp-block-column"><!-- wp:group {"layout":{"contentSize":"200px","wideSize":"300px"}} -->
<div class="wp-block-group"><!-- wp:group {"align":"full","backgroundColor":"luminous-vivid-orange"} -->
<div class="wp-block-group alignfull has-luminous-vivid-orange-background-color has-background"><!-- wp:paragraph -->
<p>test</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>tes</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>test</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group --></div>
<!-- /wp:group --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->

I'm using the empty theme, I just defined a padding for the root and enabled the "root padding" rule. With the content above, you can see some weird overflows happening on the second column.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please could you post a screenshot? This is what I see:
Screenshot 2022-05-18 at 09 55 04

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, if you inspect the "orange" column. you'll see that it has a margin/padding that make it overflow on top of the first column. Basically, it's on top of the "column" block gap and a bit on top of the first column as well. It's more noticeable if you use a big padding on the root.

tellthemachines and others added 8 commits May 18, 2022 09:34
Root vars shouldn't apply to block global styles

Fix issue with shorthand padding on server side.

Temp fix for file path error

Fix site editor top padding not updating

Support shorthand properties in site editor.

Fix full widths in the post editor

Check selector inside function.

Fix kebab-casing of CSS variables.

Fix borked merge conflict solving

Move post editor full width styles to edit-post package.

Set default padding value to 0.

Update test string.

Fix PHP unit tests

Fix PHP lint

Fix failing PHP tests.

Use block gap variable as default root padding value.

Fix linting errors.

Add opt-in setting via package.json

Generate correct block editor styles

Fix tests and add prop to core theme.json

Fix unit tests properly this time

Improve the logic for warnings for Post Comments Form placeholder (#40563)

* Improve the warning for Comments Form placeholder

* Fix typo on showPlaceholder variable name

Co-authored-by: Luis Herranz <luisherranz@gmail.com>

Add optional chaining in case taxonomy visibility is not defined (#40532)

Co-authored-by: Glen Davies <glen.davies@a8c.com>

Merge remote-tracking branch 'origin/trunk' into try/root-padding-fix

Added missing doc comment for parameter "$use_root_vars"

Add alignments rules for blocks within post content

Remove default padding

match alignment rules to those used in blockbase

move root padding rules to a new method

revert unconnected change

revert spacing change

add docblock for new method

remove file'

fix merge mess

formatting

fix merge mess

fix alignment

append to ruleset

fix issue in site editor

match the styles for the site editor to those used in the front end

formatting change

move new code to 6.1 compat files

fix linter
@scruffian
Copy link
Contributor Author

@youknowriad I have been thinking more about this, and I'm struggling to think of a real world use case for nested alignfull blocks. What do you think about simply disabling this feature?

@scruffian
Copy link
Contributor Author

@youknowriad I have been thinking more about this, and I'm struggling to think of a real world use case for nested alignfull blocks. What do you think about simply disabling this feature?

I thought about it some more and there are some use cases, for example:

<!-- wp:group {"align":"full","layout":{"contentSize":"500px"}} -->
<div class="wp-block-group alignfull"><!-- wp:paragraph -->
<p>text</p>
<!-- /wp:paragraph -->

<!-- wp:group {"align":"full"} -->
<div class="wp-block-group alignfull"></div>
<!-- /wp:group --></div>
<!-- /wp:group -->

@scruffian
Copy link
Contributor Author

What makes this hard is that when you set a block to be alignfull you need the block to be aware of the padding value of its parent. There's no simple CSS solution for this, so I think we might need to use CSS variables...

@youknowriad
Copy link
Contributor

What makes this hard is that when you set a block to be alignfull you need the block to be aware of the padding value of its parent. There's no simple CSS solution for this, so I think we might need to use CSS variables...

This is why I think #39871 is a better approach to solve this holistically no matter which context (and no need for CSS variables because these are faulty in flow layout)

@scruffian scruffian closed this May 26, 2022
@scruffian scruffian deleted the add/root-padding branch May 26, 2022 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Enhancement A suggestion for improvement.
Projects
None yet
5 participants