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

[Buttons block]: Add gap and vertical margin support #34546

Merged
merged 14 commits into from Oct 14, 2021

Conversation

stacimc
Copy link
Contributor

@stacimc stacimc commented Sep 3, 2021

Fixes: #29098

Related/Depends on: #28365

  • In particular, pending the discussion here about how to display the gap controls in the UI

Description

Makes use of the new Gap block support (#33991) in the Buttons block to allow control over the space between buttons. At the moment horizontal and vertical gaps are not handled separately. It also adds vertical margin support to allow adjusting the margin above and below the Buttons container.

As with Columns in #34456, this is intended as a stop-gap to get the Gap block support used quickly in Buttons, while we separately look into making the block use the flex layout. My reasoning is that this approach works with vertically oriented buttons, while flex layouts currently only support horizontal layouts.

It also fixes a few pre-existing buttons bugs:

  • Vertically oriented buttons should not factor the gap into their width calculation
  • Vertically oriented center-aligned buttons that set 100% width were not filling the row; this is now fixed

Major Tradeoffs

As we move from using top & bottom margins to gap for controlling vertical spacing, there will be some styling regressions in themes that are currently tweaking those margin values.

  • Vertical spacing between the Buttons container and adjacent blocks may be affected. This is because individual buttons no longer have top/bottom margin, which previously would have 'padded' the Buttons container.
  • Row gap may be affected. Themes may have previously increased the row-gap by adding extra margin to buttons. Now this margin is overwritten to 0, themes will need to tweak the row-gap value instead.

In particular here are two known bugs:

  • Twenty Twenty One forces margin: 0 on the Buttons container and adds extra margin to the :first-child button. With this PR applied, this causes all space between the Buttons container and adjacent blocks to disappear (unless the other block has its own margin), and vertically offsets the first button. https://core.trac.wordpress.org/ticket/54250
  • Twenty Twenty uses margins to increase the row-spacing of buttons on the frontend. With this PR applied, the margins are overwritten and this vertical spacing shrinks. https://core.trac.wordpress.org/ticket/54251

Notes

  • Button widths take the gap into consideration. This means if you have two buttons in a horizontal orientation, both set to 50% width, they will fit on the same line and each take up 50% of the row minus the gap in between them. This can look odd if you set a huge gap because a button set to 50% width could end up smaller than expected, but is intuitive with more "normal" gap selections.

Screen Shot 2021-09-08 at 11 20 26 AM

  • When the "Space between items" alignment option is selected, buttons will be spaced equally across their row as expected -- so effectively, the column gap is at least as big as the selected gap, but may be bigger if needed to make the buttons fit to the row. The row gap is exactly what is specified in the user selection. Ie in this screenshot I set a 3px gap: The horizontal gap between buttons is larger than 3px in order to evenly space them on the row; the vertical gap is exactly 3px.

Screen Shot 2021-09-08 at 11 23 14 AM

  • If you choose the vertically oriented variation of Buttons, Gap is still supported but of course only affects the vertical spacing between buttons

Screen Shot 2021-09-08 at 2 04 55 PM

How has this been tested?

Manually. Some areas to focus:

  • Changing button widths
  • Changing items justification
  • Vertical as well as horizontal orientation
  • Full/Wide/Normal widths
  • Buttons inside a container with a flex layout, like the Group block
  • Verifying all styling applies on the frontend
  • Set a gap value through theme.json
  • Set a gap value through global styles

Screenshots

button-gap

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@stacimc stacimc added [Status] In Progress Tracking issues with work in progress [Block] Buttons Affects the Buttons Block labels Sep 3, 2021
@stacimc stacimc self-assigned this Sep 3, 2021
@github-actions
Copy link

github-actions bot commented Sep 4, 2021

Size Change: -386 B (0%)

Total Size: 1.07 MB

Filename Size Change
build/block-library/blocks/button/editor-rtl.css 470 B -4 B (-1%)
build/block-library/blocks/button/editor.css 470 B -4 B (-1%)
build/block-library/blocks/button/style-rtl.css 549 B -51 B (-8%)
build/block-library/blocks/button/style.css 549 B -51 B (-8%)
build/block-library/blocks/buttons/editor-rtl.css 309 B -6 B (-2%)
build/block-library/blocks/buttons/editor.css 309 B -6 B (-2%)
build/block-library/blocks/buttons/style-rtl.css 317 B -53 B (-14%) 👏
build/block-library/blocks/buttons/style.css 317 B -53 B (-14%) 👏
build/block-library/editor-rtl.css 9.8 kB +6 B (0%)
build/block-library/editor.css 9.79 kB +7 B (0%)
build/block-library/index.min.js 148 kB +9 B (0%)
build/block-library/style-rtl.css 10.3 kB -91 B (-1%)
build/block-library/style.css 10.3 kB -89 B (-1%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 931 B
build/admin-manifest/index.min.js 1.09 kB
build/annotations/index.min.js 2.7 kB
build/api-fetch/index.min.js 2.21 kB
build/autop/index.min.js 2.08 kB
build/blob/index.min.js 459 B
build/block-directory/index.min.js 6.2 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/index.min.js 134 kB
build/block-editor/style-rtl.css 13.9 kB
build/block-editor/style.css 13.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 65 B
build/block-library/blocks/archives/style.css 65 B
build/block-library/blocks/audio/editor-rtl.css 58 B
build/block-library/blocks/audio/editor.css 58 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/block/editor-rtl.css 161 B
build/block-library/blocks/block/editor.css 161 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 90 B
build/block-library/blocks/code/style.css 90 B
build/block-library/blocks/code/theme-rtl.css 131 B
build/block-library/blocks/code/theme.css 131 B
build/block-library/blocks/columns/editor-rtl.css 206 B
build/block-library/blocks/columns/editor.css 205 B
build/block-library/blocks/columns/style-rtl.css 497 B
build/block-library/blocks/columns/style.css 496 B
build/block-library/blocks/cover/editor-rtl.css 546 B
build/block-library/blocks/cover/editor.css 547 B
build/block-library/blocks/cover/style-rtl.css 1.17 kB
build/block-library/blocks/cover/style.css 1.17 kB
build/block-library/blocks/embed/editor-rtl.css 488 B
build/block-library/blocks/embed/editor.css 488 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 322 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 977 B
build/block-library/blocks/gallery/editor.css 982 B
build/block-library/blocks/gallery/style-rtl.css 1.6 kB
build/block-library/blocks/gallery/style.css 1.59 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 159 B
build/block-library/blocks/group/editor.css 159 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 114 B
build/block-library/blocks/heading/style.css 114 B
build/block-library/blocks/home-link/style-rtl.css 247 B
build/block-library/blocks/home-link/style.css 247 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 731 B
build/block-library/blocks/image/editor.css 730 B
build/block-library/blocks/image/style-rtl.css 502 B
build/block-library/blocks/image/style.css 505 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 137 B
build/block-library/blocks/latest-posts/editor.css 137 B
build/block-library/blocks/latest-posts/style-rtl.css 528 B
build/block-library/blocks/latest-posts/style.css 527 B
build/block-library/blocks/list/style-rtl.css 94 B
build/block-library/blocks/list/style.css 94 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 568 B
build/block-library/blocks/navigation-link/editor.css 570 B
build/block-library/blocks/navigation-link/style-rtl.css 94 B
build/block-library/blocks/navigation-link/style.css 94 B
build/block-library/blocks/navigation-submenu/editor-rtl.css 300 B
build/block-library/blocks/navigation-submenu/editor.css 299 B
build/block-library/blocks/navigation-submenu/style-rtl.css 195 B
build/block-library/blocks/navigation-submenu/style.css 195 B
build/block-library/blocks/navigation-submenu/view.min.js 343 B
build/block-library/blocks/navigation/editor-rtl.css 1.71 kB
build/block-library/blocks/navigation/editor.css 1.71 kB
build/block-library/blocks/navigation/style-rtl.css 1.64 kB
build/block-library/blocks/navigation/style.css 1.64 kB
build/block-library/blocks/navigation/view.min.js 2.74 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 377 B
build/block-library/blocks/page-list/editor.css 377 B
build/block-library/blocks/page-list/style-rtl.css 198 B
build/block-library/blocks/page-list/style.css 198 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 273 B
build/block-library/blocks/paragraph/style.css 273 B
build/block-library/blocks/post-author/editor-rtl.css 210 B
build/block-library/blocks/post-author/editor.css 210 B
build/block-library/blocks/post-author/style-rtl.css 182 B
build/block-library/blocks/post-author/style.css 181 B
build/block-library/blocks/post-comments-form/style-rtl.css 140 B
build/block-library/blocks/post-comments-form/style.css 140 B
build/block-library/blocks/post-comments/style-rtl.css 360 B
build/block-library/blocks/post-comments/style.css 359 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 396 B
build/block-library/blocks/post-featured-image/editor.css 397 B
build/block-library/blocks/post-featured-image/style-rtl.css 156 B
build/block-library/blocks/post-featured-image/style.css 156 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 391 B
build/block-library/blocks/post-template/style.css 392 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 60 B
build/block-library/blocks/post-title/style.css 60 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 378 B
build/block-library/blocks/pullquote/style.css 378 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 262 B
build/block-library/blocks/query-pagination/editor.css 255 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-title/editor-rtl.css 85 B
build/block-library/blocks/query-title/editor.css 85 B
build/block-library/blocks/query/editor-rtl.css 131 B
build/block-library/blocks/query/editor.css 132 B
build/block-library/blocks/quote/style-rtl.css 187 B
build/block-library/blocks/quote/style.css 187 B
build/block-library/blocks/quote/theme-rtl.css 220 B
build/block-library/blocks/quote/theme.css 222 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 374 B
build/block-library/blocks/search/style.css 375 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 99 B
build/block-library/blocks/separator/editor.css 99 B
build/block-library/blocks/separator/style-rtl.css 250 B
build/block-library/blocks/separator/style.css 250 B
build/block-library/blocks/separator/theme-rtl.css 172 B
build/block-library/blocks/separator/theme.css 172 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 769 B
build/block-library/blocks/site-logo/editor.css 769 B
build/block-library/blocks/site-logo/style-rtl.css 165 B
build/block-library/blocks/site-logo/style.css 165 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 165 B
build/block-library/blocks/social-link/editor.css 165 B
build/block-library/blocks/social-links/editor-rtl.css 812 B
build/block-library/blocks/social-links/editor.css 811 B
build/block-library/blocks/social-links/style-rtl.css 1.3 kB
build/block-library/blocks/social-links/style.css 1.3 kB
build/block-library/blocks/spacer/editor-rtl.css 307 B
build/block-library/blocks/spacer/editor.css 307 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 471 B
build/block-library/blocks/table/editor.css 472 B
build/block-library/blocks/table/style-rtl.css 481 B
build/block-library/blocks/table/style.css 481 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 146 B
build/block-library/blocks/tag-cloud/style.css 146 B
build/block-library/blocks/template-part/editor-rtl.css 636 B
build/block-library/blocks/template-part/editor.css 635 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/editor-rtl.css 90 B
build/block-library/blocks/term-description/editor.css 90 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 571 B
build/block-library/blocks/video/editor.css 572 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 815 B
build/block-library/common.css 812 B
build/block-library/reset-rtl.css 474 B
build/block-library/reset.css 474 B
build/block-library/theme-rtl.css 665 B
build/block-library/theme.css 669 B
build/block-serialization-default-parser/index.min.js 1.09 kB
build/block-serialization-spec-parser/index.min.js 2.79 kB
build/blocks/index.min.js 45.7 kB
build/components/index.min.js 217 kB
build/components/style-rtl.css 15.2 kB
build/components/style.css 15.2 kB
build/compose/index.min.js 10.4 kB
build/core-data/index.min.js 12.4 kB
build/customize-widgets/index.min.js 11.2 kB
build/customize-widgets/style-rtl.css 1.5 kB
build/customize-widgets/style.css 1.49 kB
build/data-controls/index.min.js 614 B
build/data/index.min.js 7.1 kB
build/date/index.min.js 31.5 kB
build/deprecated/index.min.js 428 B
build/dom-ready/index.min.js 304 B
build/dom/index.min.js 4.46 kB
build/edit-navigation/index.min.js 15.3 kB
build/edit-navigation/style-rtl.css 3.76 kB
build/edit-navigation/style.css 3.76 kB
build/edit-post/classic-rtl.css 492 B
build/edit-post/classic.css 494 B
build/edit-post/index.min.js 29.3 kB
build/edit-post/style-rtl.css 7.22 kB
build/edit-post/style.css 7.22 kB
build/edit-site/index.min.js 29.7 kB
build/edit-site/style-rtl.css 5.54 kB
build/edit-site/style.css 5.54 kB
build/edit-widgets/index.min.js 15.7 kB
build/edit-widgets/style-rtl.css 4.12 kB
build/edit-widgets/style.css 4.13 kB
build/editor/index.min.js 37.5 kB
build/editor/style-rtl.css 3.78 kB
build/editor/style.css 3.77 kB
build/element/index.min.js 3.17 kB
build/escape-html/index.min.js 517 B
build/format-library/index.min.js 5.93 kB
build/format-library/style-rtl.css 571 B
build/format-library/style.css 571 B
build/hooks/index.min.js 1.55 kB
build/html-entities/index.min.js 424 B
build/i18n/index.min.js 3.6 kB
build/is-shallow-equal/index.min.js 501 B
build/keyboard-shortcuts/index.min.js 1.72 kB
build/keycodes/index.min.js 1.3 kB
build/list-reusable-blocks/index.min.js 1.85 kB
build/list-reusable-blocks/style-rtl.css 838 B
build/list-reusable-blocks/style.css 838 B
build/media-utils/index.min.js 2.92 kB
build/notices/index.min.js 845 B
build/nux/index.min.js 2.03 kB
build/nux/style-rtl.css 747 B
build/nux/style.css 743 B
build/plugins/index.min.js 1.83 kB
build/primitives/index.min.js 921 B
build/priority-queue/index.min.js 582 B
build/react-i18n/index.min.js 671 B
build/redux-routine/index.min.js 2.63 kB
build/reusable-blocks/index.min.js 2.19 kB
build/reusable-blocks/style-rtl.css 256 B
build/reusable-blocks/style.css 256 B
build/rich-text/index.min.js 10.6 kB
build/server-side-render/index.min.js 1.52 kB
build/shortcode/index.min.js 1.48 kB
build/token-list/index.min.js 562 B
build/url/index.min.js 1.74 kB
build/viewport/index.min.js 1.02 kB
build/warning/index.min.js 248 B
build/widgets/index.min.js 7.11 kB
build/widgets/style-rtl.css 1.16 kB
build/widgets/style.css 1.16 kB
build/wordcount/index.min.js 1.04 kB

compressed-size-action

@andrewserong andrewserong linked an issue Sep 5, 2021 that may be closed by this pull request
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.

Great work here @stacimc and thanks for the detailed PR description, there sure are a lot of variables when it comes to styling and positioning Button(s) blocks!

I've left a comment about the legacy standalone Button block (when it's rendered without a Buttons container), and a comment about the 100% width for the vertical variation.

Another slight oddity for the vertical variation, which isn't really caused by your PR because it exists on trunk, is that we're subtracting the block gap from the width of the 25%, 50% and 75% buttons. I think this works great in the horizontal layout as you've described (we expect the editor to figure out the appropriate balance with the gap when we set widths), however with the vertical layout when adjusting the gap, it seems unexpected that the gap would affect the widths... would it be worth adding rules like the following, or do you think it'd get too messy?

.wp-block-buttons.is-vertical > .wp-block-button {
  &.wp-block-button__width-25 {
    width: 25%;
  }
  &.wp-block-button__width-50 {
    width: 50%;
  }
  &.wp-block-button__width-75 {
    width: 75%;
  }
}

Other than that, I think this is looking really good, and even without refactoring the CSS all that much, by adding in the gap variable, you've been able to get rid of quite a bit of CSS which neatens it a fair bit. I'm particularly excited that we'll be able to tweak layouts like the following with gap support, though, so I think it's going to be a great feature:

buttons-gap-sml

packages/block-library/src/button/style.scss Outdated Show resolved Hide resolved
packages/block-library/src/button/editor.scss Outdated Show resolved Hide resolved
@stacimc
Copy link
Contributor Author

stacimc commented Sep 9, 2021

Another slight oddity for the vertical variation, which isn't really caused by your PR because it exists on trunk, is that we're subtracting the block gap from the width of the 25%, 50% and 75% buttons.

Good point -- agreed, I would not expect gap to be accounted for in the widths of vertically oriented buttons. I'm not worried about adding in a few extra rules considering how much we're able to clean up with the rest of this PR :) Happy to add that in while I'm here!

@stacimc stacimc marked this pull request as ready for review September 9, 2021 20:35
@stacimc stacimc removed the [Status] In Progress Tracking issues with work in progress label Sep 9, 2021
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 fixing up those issues @stacimc! That's testing nicely in TT1-Blocks now, and I tested in the site editor, too 👍

I did a bit more testing today and tried out themes without a theme.json to test the fallback values, and noticed that it looks like Sass needs to use the interpolation syntax #{} to inject the Sass variable when used within a var().

To test, I switched my site to use the Maywood theme, then duplicated my test Buttons block and then removed the gap value so that the block doesn't have access to any --wp--style--block-gap and will use the var's fallback value.

Here's a before / after with the interpolation added, so I think that's all that's needed to get this working on non-theme.json themes 🤞

WIthout interpolation syntax With interpolation syntax
image image

Other than that, these changes are all testing well for me! Assuming others are fine with the change, I think the final step will be to see where the discussion arrives at in regards to switching on the block gap and where we expose the control in the UI. Once we arrive at a conclusion, hopefully we'll then be able to get this and the gallery and columns blocks opt-in PRs merged.

packages/block-library/src/button/style.scss Outdated Show resolved Hide resolved
@jasmussen
Copy link
Contributor

Thanks for this, gap feels like such a win here.

These two:

Another slight oddity for the vertical variation, which isn't really caused by your PR because it exists on trunk, is that we're subtracting the block gap from the width of the 25%, 50% and 75% buttons. I think this works great in the horizontal layout as you've described (we expect the editor to figure out the appropriate balance with the gap when we set widths), however with the vertical layout when adjusting the gap, it seems unexpected that the gap would affect the widths... would it be worth adding rules like the following, or do you think it'd get too messy?

Button widths take the gap into consideration. This means if you have two buttons in a horizontal orientation, both set to 50% width, they will fit on the same line and each take up 50% of the row minus the gap in between them. This can look odd if you set a huge gap because a button set to 50% width could end up smaller than expected, but is intuitive with more "normal" gap selections.

Do we still need the calc rules when applying those widths? I had been my hope that they were necessary mostly to account for the margins. I think I had this discussion with @kjellr as well, where we went back and forward on how and when to start supporting gap here.

In either case, setting two buttons both to 50% width, I'd expect that to be 50% of the allotted space, i.e. if there's a 50% wide gap, then actually the buttons are 33% of the full container space.

On a separate note, rows with multiple buttons is likely an edgecase, but it would be nice to upgrade the gap control to support axial configuration for the cases where you want a narrow horizontal gap and a tall vertical gap.

@@ -63,7 +63,6 @@
.is-selected & {
height: auto;
overflow: visible;
margin-top: $grid-unit-20;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know what this margin was for?

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 was added here, looks like it was applied to the LinkControl in the block toolbar. It's been refactored since then and I'm fairly confident this isn't getting used at all anymore, actually 🤔

I have tested adding/removing a button's link and the controls look good to me.

}
}

// If the browser supports column-gap, use that instead of the margins above.
@supports ( column-gap: #{ $blocks-block__margin } ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Speaking of browser support, it's so nice to see the important ones green in caniuse. But I also recall something about Safari that was context for adding this check in the first place. Do you recall, @kjellr?

@stacimc
Copy link
Contributor Author

stacimc commented Sep 10, 2021

Do we still need the calc rules when applying those widths? I had been my hope that they were necessary mostly to account for the margins. I think I had this discussion with @kjellr as well, where we went back and forward on how and when to start supporting gap here.

I was not able to get this to work without the calc rules, neither with width nor flex-basis. I played around with this today and could come up with nothing simpler than the calc, although I'm by no means a CSS expert. If it's possible I'd love to know how, it would be great to clean up even more rules here!

In either case, setting two buttons both to 50% width, I'd expect that to be 50% of the allotted space, i.e. if there's a 50% wide gap, then actually the buttons are 33% of the full container space.

@jasmussen I think we're on the same page here and I'm just confused by the % gap, so making sure I understand: if I have say a 400px-wide row containing two buttons, each set to 50% width, and an explicitly 200px gap, then I would expect the buttons to each be 100px or 25% of the full container space.

I would still expect this to be true if the gap was set as a percentage -- so if I set the gap to 50%, the gap itself is 50% of the container (200px), and then set my 2 buttons to 50% they would each take up 50% of the remaining allotted 200px, rather than being treated like a ratio to the gap. Is that what you're thinking?

Screen Shot 2021-09-10 at 4 29 15 PM

On a separate note, rows with multiple buttons is likely an edgecase, but it would be nice to upgrade the gap control to support axial configuration for the cases where you want a narrow horizontal gap and a tall vertical gap.

Definitely ➕ ! I think the block support initially explored that but it was decided that we should move that to a follow up. I like the idea of being able to control these separately for buttons.

I think the final step will be to see where the discussion arrives at in regards to switching on the block gap and where we expose the control in the UI. Once we arrive at a conclusion, hopefully we'll then be able to get this and the gallery and columns blocks opt-in PRs merged.

Thanks, I've updated the PR description to reflect this 👍

@paaljoachim
Copy link
Contributor

paaljoachim commented Sep 15, 2021

I quickly browsed through the above comments.
I am wondering if gap support should contain a feature to unlock top, right, bottom and left so one can choose to have gaps in either direction. Similar to turning unlocking border radius to defining which side to use a radius.

@stacimc stacimc changed the title [Buttons block]: Add gap support [Buttons block]: Add gap and vertical margin support Sep 16, 2021
@stacimc
Copy link
Contributor Author

stacimc commented Sep 16, 2021

I am wondering if gap support should contain a feature to unlock top, right, bottom and left

I agree, this would be really useful. #34529 is tracking the addition of axial gap support that would allow separate row/column gap configuration.

On a related note I think I'm going to have to do some more tinkering on this for backwards compatibility in non-blocks themes. The substantially smaller column spacing as compared to row spacing is one issue:

Screen Shot 2021-09-16 at 2 17 06 PM

This PR as it currently stands will also shrink the space between the Buttons container and blocks above/below it. I'm afraid we might not be able to get away with simplifying all the margin code after all.

@stacimc
Copy link
Contributor Author

stacimc commented Sep 30, 2021

This has been updated to implement the row gap using margin. This is unfortunately necessary to avoid backwards compatibility issues with classic themes such as Twenty Twenty, which style Buttons spacing by overriding the top and bottom margin on individual buttons.

The styling rules use --wp--style--block-gap and fallback to the existing default margin; this can cause styling regressions when there is no user-selected blockGap and it picks up the global variable instead. #35209 will fix this for classic themes, but as far as I can tell this is an unavoidable problem on blocks themes that don't explicitly opt out of both the gap block support and the global spacing config (by setting blockGap: null in their theme.json settings).

What that means specifically is that on TT1 blocks, for example, on trunk we have a 0.5em column-gap and 10px margins (so 20px row gap). With this PR applied, both column and row gap will adjust to the 24px global gap by default:

Before After
Screen Shot 2021-09-30 at 11 14 20 AM Screen Shot 2021-09-30 at 11 16 37 AM

Another problem caused by using margin to create the row gap is that the new gap spacing config already applies a margin-top: var( --wp--style--block-gap) to the Buttons container, with high specificity. Since the buttons have their own top margin, we effectively end up with 1.5 the size of the gap above the container:

Screen Shot 2021-09-30 at 3 00 23 PM
For dramatic effect, here blockGap is 120px. So there is a 120px margin-top applied to the Buttons container, and a 60px margin-top and margin-bottom on each individual button => 180px gap between the actual buttons and preceding content.

Using the margin block support, the user can control the margin applied to the container itself but not to the individual buttons. This would be easily remedied if we could just use row-gap, but then we're back to backwards compatibility issues. 🤔

@stacimc
Copy link
Contributor Author

stacimc commented Oct 1, 2021

This PR is at a bit of an impasse due to some tradeoffs with respect to backwards compatibility. @jasmussen I'm curious how you feel about some of the priorities here?

One big tradeoff is the use of margin to implement row spacing instead of row-gap, which I'm doing to preserve backwards compatibility in classic themes (more details in my above comment)-- but which causes increased spacing between the Buttons container and adjacent blocks on blocks themes. It also means that even with added margin block support, a user can't fully control the spacing between individual buttons and preceding blocks, because there's a margin on the button blocks themselves. It's not clear to me that's worth the trade.

There's also the issue of axial gap support. Since that hasn't been implemented just yet (#34529), we get a bit of a styling regression on themes that opt into the block support, as there's no way to preserve existing styles where column gap is smaller than row gap.

Finally the unified variable approach for blockGap will inherently cause some regression for themes that opt in, because the global --wp--style--block-gap will get picked up.

@jasmussen
Copy link
Contributor

jasmussen commented Oct 1, 2021

Backwards compatibility with regards to sufficient browser support is important, and I do recall there being an issue with gap in a particular version of Safari, however that's been addressed and support is looking good to me: https://caniuse.com/#search=gap

This has been updated to implement the row gap using margin. This is unfortunately necessary to avoid backwards compatibility issues with classic themes such as Twenty Twenty, which style Buttons spacing by overriding the top and bottom margin on individual buttons.

I don't think backwards compatibility with themes is something that should block forward movement on features that otherwise vastly improve the user experience. It is important to note changes like these in changelogs and dev notes, and it's important to ticket the changes. And it's a balance of course, if the change affects every existing theme, it's one thing. If it affects a single or two themes, it's quite another.

For example, TwentyTwentyOne did the very best it could with the Navigation block in the state it was in when the theme launched. It was frankly impressive. However it had to add a great deal of CSS to make things work, and that CSS is now technical debt. We had to move forward with the navigation block, despite the issues that were caused. This ticket details those issues, and there's now a patch to fix TT1: https://core.trac.wordpress.org/ticket/53220

Can we get a sense of how big the problem is? I do recognize the problem of providing blanket top/bottom block margins, often by targetting [data-block]. Those rules almost all predate horizontal blocks like Buttons, Social Icons or even Navigation, and mostly those rules are editor style problems only.

If the problem is widespread enough, it might be worth looking at creating some fallback rules and put them in https://github.com/WordPress/gutenberg/blob/trunk/packages/edit-post/src/classic.scss, so that the appearance is okay. What do you think?

@stacimc stacimc force-pushed the add/gap-block-support-to-buttons branch from cf4d1a1 to 9fd3a7d Compare October 13, 2021 18:46
@stacimc
Copy link
Contributor Author

stacimc commented Oct 13, 2021

But because the margin of child blocks should always be zero, it feels okay to add very high specificity here, almost bordering on !important. So if you like, and after all (I realize you've probably already been around this solution, sorry about that), we could push that small fix.

Pushed! When I originally suggested an !important here, there was concern about the tradeoff of shrinking the row-gap on classic themes that override it with margins (basically the Twenty Twenty issue). Since that is inevitable anyway I'm happy to make this change; the more dramatic visual bug with offset first buttons in Twenty Twenty One is now cleaned up.

I noticed that for TT1 specifically, the --wp--style--block-gap variable is present in the editor, but not on the frontend

Ah yes, this is caused by a bug where the variable would sometimes render in classic themes -- it was fixed in #35209 and I have now rebased just to pick that up and confirm that it's working.

@jasmussen
Copy link
Contributor

jasmussen commented Oct 14, 2021

Cool, I think we should try this. Gap vastly simplifies the user experience, as well as the implementation, and the behavior is both predictable and nearly always desirable when spacing items out. This takes a big step towards that, and doesn't preclude us from revisiting and tweaking in the future, as themes start to embrace this, or in case issues get surfaced.

And thank you for your work on this!

@stacimc stacimc merged commit 89deab1 into trunk Oct 14, 2021
@stacimc stacimc deleted the add/gap-block-support-to-buttons branch October 14, 2021 17:07
@github-actions github-actions bot added this to the Gutenberg 11.8 milestone Oct 14, 2021
@NicoHood
Copy link

Is this feature available in the recent 5.9 release? It seems that I am unable to use it. It might be caused by the astra theme or the astra gutenberg block plugin, and if so, I'd report it there. Any hints?

grafik

@andrewserong
Copy link
Contributor

Is this feature available in the recent 5.9 release?

It is available in 5.9, however themes need to explicitly opt-in to the blockGap support (or to all UI controls via the appearanceTools setting) in their theme.json file. There's more info on this in the block-editor handbook here. So, it sounds like the best bet here might be to contact the theme developer to see if they're planning on adding support.

@NicoHood
Copy link

Thanks! Is there a way to enable it for me with a code snippet in the meantime?

Do you also know how to properly set the margin after a button block? For me I always have to add a paragraph afterwards, as the following text, image, etc is sticked way to close. I know this is another issue, but I could not find any information why this happens. It seems to be an upstream issue or decision. This workaround fixes it in the frontend, but not in the editor. Maybe you got any hint?

.wp-block-button {
    margin-bottom: 0.5em;
}

@andrewserong
Copy link
Contributor

Is there a way to enable it for me with a code snippet in the meantime?

Unfortunately, no. The way that each theme implements support for Gutenberg features can be quite unique depending on how they're set up, so beyond what's in the handbook, it's probably best to contact the theme developers via their support forums if you're not building your own theme from scratch.

Do you also know how to properly set the margin after a button block?

Usually the Button block would be inside a Buttons block, so I don't think you'd need to add a bottom margin on the individual button? Again, this sounds like it's likely to do with how the theme handles spacing. In blocks-based themes that opt-in to block gap and margin you can select the optional margin control to adjust the spacing between the Buttons block and the following paragraph for a one-off change (however ideally the theme would set its desired spacing at the global level):

image

If the theme you're using doesn't support that, the theme developers will be the best ones to let you know what flexibility they provide.

If you think there's a bug in Gutenberg, though, and it doesn't look like an issue has been reported for it, feel free to create an issue!

@bradhogan
Copy link

Doesn't seem like there's a way to add a blockGap value to the core/button block in the theme.json, is that still an open issue? I've seen a solution like this that would be useful so a user wouldn't have to adjust the spacing every time they add buttons - assuming the default blockGap is larger.

"core/buttons": {
	"spacing": {
		"blockGap": ".5em"
	}
}

@andrewserong
Copy link
Contributor

Thanks for reporting @bradley2083! That's correct, the blockGap setting isn't currently supported at the block-level within global styles / theme.json, but we're hoping to add it in. I've opened up an issue in #39789 to track progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Buttons Affects the Buttons Block
Projects
None yet
8 participants