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: Reduce specificity of reset & classic styles. #32659

Merged
merged 2 commits into from Aug 9, 2021

Conversation

@jasmussen
Copy link
Contributor

@jasmussen jasmussen commented Jun 14, 2021

Description

We have two stylesheets that are created to normalize the editing canvas: reset.scss and classic.scss.

  • The first one exists because the editing canvas in the post editor is not inside an iframe, so it is subject to CSS bleed from wp-admin and other styles. The reset unsets a number of element styles to provide a blank canvas to build theme styles on.
  • The other one, classic.scss is loaded for classic themes, and primarily serves to provide left/right/wide/full-wide alignment styles. Block themes do not need these, as those alignments are served by a layout property and handled by the system.

In both cases, while the styles are necessary, they should have as low specificity as is possible. The lower the specificity, the easier it is for a theme to style things in a nice way.

There is a new selector, called :where, which is able to provide default styles with lowered specificity. Some of the details on how that works are outlined in this article, but you can also check out this codepen for an example. That selector is theoretically ideal for these resets, because it allows us to reduce the specificity quite a bit, letting both theme and block styles win any specificity fight they get into. In this PR, I tried employing that selector for those resets.

Overall, it worked very well, especially for the reset which just feels perfect. Here's before and after using "empty theme" — and they should look identical, only with the new version having lower specificity:

Screenshot 2021-06-14 at 12 39 12

After:

Screenshot 2021-06-14 at 12 40 05

For other themes, here I tested TT1, it solved this problem surfaced by @carolinan, where margin-left: auto; margin-right: auto; overrides the default navigation item margin, causing them to collapse. Before:

beftt1

(don't mind the padding, that's a separate issue)

After:

afttt1

In these screenshots, the navigation menu is justified right.

The primary "gotcha" I encountered is that themes commonly apply a margin-left: 0; on ol and ul elements, which seems an innocent and sensible change over the defaults. The problem is that for classic themes, the margin-left: auto; margin-right: auto; rules exist (along with a max-width) to center a block inside a post content column that supports wide and fullwide images. When left margins are zeroed out, it effectively means a left-aligned list block. In trunk, the auto-margins win due to their intrinsic specificity. But with the lowered specificity of this branch, the zero margin now wins.

For that reason, I included a targeted rule for ol and ul elements, to keep their specificity. It's not an inclusion I love, but it feels necessary as the change feels very common. What do you think?

How has this been tested?

  • Please test a variety of blocks and content in this branch. The demo content is good, but please at least include a justified-right navigation block, an image, and a list block.
  • Please test wide and fullwide blocks.
  • Please test primarily in classic themes like TT1 or older.
  • Please also test Empty Theme and verify that before and after are identical.

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).
@jasmussen jasmussen requested a review from ajitbohra as a code owner Jun 14, 2021
@jasmussen jasmussen self-assigned this Jun 14, 2021
@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jun 14, 2021

Note to self, need to push a fix for the Gallery block:

Screenshot 2021-06-14 at 12 57 09

This rule is now winning:

Screenshot 2021-06-14 at 12 57 04

Easy fix, but will let you all add some thoughts first.

@jasmussen jasmussen mentioned this pull request Jun 14, 2021
6 of 7 tasks
@youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Jun 14, 2021

For that reason, I included a targeted rule for ol and ul elements, to keep their specificity. It's not an inclusion I love, but it feels necessary as the change feels very common. What do you think?

Interesting use-case, potentially we could consider doing this PR for just "resets" for now maybe? I think there might be other cases where themes could inadvertantly override the default centering margins (like figure maybe)

@jasmussen jasmussen marked this pull request as draft Jun 14, 2021
@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jun 14, 2021

Interesting use-case, potentially we could consider doing this PR for just "resets" for now maybe? I think there might be other cases where themes could inadvertantly override the default centering margins (like figure maybe)

Agreed, even if we don't land this for the classic styles, it seems a win to land it for the reset. That said, this PR does solve an issue with navigation block justification in classic themes, which isn't trivial to fix otherwise: if I increase the specificity in the navigation block, I break global styles.

@github-actions
Copy link

@github-actions github-actions bot commented Jun 14, 2021

Size Change: +54 B (0%)

Total Size: 1.08 MB

Filename Size Change
build/block-library/reset-rtl.css 527 B +13 B (+3%)
build/block-library/reset.css 527 B +13 B (+3%)
build/edit-post/classic-rtl.css 492 B +13 B (+3%)
build/edit-post/classic.css 494 B +13 B (+3%)
build/edit-post/index.min.js 59.4 kB +2 B (0%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 1.12 kB
build/admin-manifest/index.min.js 1.46 kB
build/annotations/index.min.js 2.93 kB
build/api-fetch/index.min.js 2.44 kB
build/autop/index.min.js 2.28 kB
build/blob/index.min.js 673 B
build/block-directory/index.min.js 6.62 kB
build/block-directory/style-rtl.css 1.01 kB
build/block-directory/style.css 1.01 kB
build/block-editor/index.min.js 127 kB
build/block-editor/style-rtl.css 13.9 kB
build/block-editor/style.css 13.8 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/button/editor-rtl.css 474 B
build/block-library/blocks/button/editor.css 474 B
build/block-library/blocks/button/style-rtl.css 605 B
build/block-library/blocks/button/style.css 604 B
build/block-library/blocks/buttons/editor-rtl.css 315 B
build/block-library/blocks/buttons/editor.css 315 B
build/block-library/blocks/buttons/style-rtl.css 370 B
build/block-library/blocks/buttons/style.css 370 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 189 B
build/block-library/blocks/columns/editor.css 188 B
build/block-library/blocks/columns/style-rtl.css 474 B
build/block-library/blocks/columns/style.css 475 B
build/block-library/blocks/cover/editor-rtl.css 666 B
build/block-library/blocks/cover/editor.css 670 B
build/block-library/blocks/cover/style-rtl.css 1.23 kB
build/block-library/blocks/cover/style.css 1.23 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 400 B
build/block-library/blocks/embed/style.css 400 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 711 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 707 B
build/block-library/blocks/gallery/editor.css 706 B
build/block-library/blocks/gallery/style-rtl.css 1.05 kB
build/block-library/blocks/gallery/style.css 1.05 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 93 B
build/block-library/blocks/group/theme.css 93 B
build/block-library/blocks/heading/editor-rtl.css 152 B
build/block-library/blocks/heading/editor.css 152 B
build/block-library/blocks/heading/style-rtl.css 76 B
build/block-library/blocks/heading/style.css 76 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 283 B
build/block-library/blocks/html/editor.css 284 B
build/block-library/blocks/image/editor-rtl.css 728 B
build/block-library/blocks/image/editor.css 728 B
build/block-library/blocks/image/style-rtl.css 482 B
build/block-library/blocks/image/style.css 487 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 63 B
build/block-library/blocks/list/style.css 63 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 488 B
build/block-library/blocks/media-text/style.css 485 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 474 B
build/block-library/blocks/navigation-link/editor.css 474 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/editor-rtl.css 1.67 kB
build/block-library/blocks/navigation/editor.css 1.68 kB
build/block-library/blocks/navigation/style-rtl.css 1.65 kB
build/block-library/blocks/navigation/style.css 1.64 kB
build/block-library/blocks/navigation/view.min.js 2.84 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 310 B
build/block-library/blocks/page-list/editor.css 310 B
build/block-library/blocks/page-list/style-rtl.css 242 B
build/block-library/blocks/page-list/style.css 242 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 248 B
build/block-library/blocks/paragraph/style.css 248 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-content/editor-rtl.css 138 B
build/block-library/blocks/post-content/editor.css 138 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 412 B
build/block-library/blocks/post-featured-image/editor.css 412 B
build/block-library/blocks/post-featured-image/style-rtl.css 143 B
build/block-library/blocks/post-featured-image/style.css 143 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 378 B
build/block-library/blocks/post-template/style.css 379 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 361 B
build/block-library/blocks/pullquote/style.css 360 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 270 B
build/block-library/blocks/query-pagination/editor.css 262 B
build/block-library/blocks/query-pagination/style-rtl.css 168 B
build/block-library/blocks/query-pagination/style.css 168 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 169 B
build/block-library/blocks/quote/style.css 169 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 209 B
build/block-library/blocks/search/editor.css 209 B
build/block-library/blocks/search/style-rtl.css 368 B
build/block-library/blocks/search/style.css 372 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 462 B
build/block-library/blocks/site-logo/editor.css 464 B
build/block-library/blocks/site-logo/style-rtl.css 153 B
build/block-library/blocks/site-logo/style.css 153 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.33 kB
build/block-library/blocks/social-links/style.css 1.33 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 1.29 kB
build/block-library/common.css 1.29 kB
build/block-library/editor-rtl.css 9.87 kB
build/block-library/editor.css 9.85 kB
build/block-library/index.min.js 147 kB
build/block-library/style-rtl.css 10.2 kB
build/block-library/style.css 10.2 kB
build/block-library/theme-rtl.css 688 B
build/block-library/theme.css 692 B
build/block-serialization-default-parser/index.min.js 1.3 kB
build/block-serialization-spec-parser/index.min.js 3.06 kB
build/blocks/index.min.js 47.2 kB
build/components/index.min.js 197 kB
build/components/style-rtl.css 15.8 kB
build/components/style.css 15.8 kB
build/compose/index.min.js 10.2 kB
build/core-data/index.min.js 12.6 kB
build/customize-widgets/index.min.js 10.8 kB
build/customize-widgets/style-rtl.css 1.5 kB
build/customize-widgets/style.css 1.49 kB
build/data-controls/index.min.js 831 B
build/data/index.min.js 7.22 kB
build/date/index.min.js 31.8 kB
build/deprecated/index.min.js 738 B
build/dom-ready/index.min.js 576 B
build/dom/index.min.js 4.78 kB
build/edit-navigation/index.min.js 13.9 kB
build/edit-navigation/style-rtl.css 3.1 kB
build/edit-navigation/style.css 3.1 kB
build/edit-post/style-rtl.css 7.18 kB
build/edit-post/style.css 7.17 kB
build/edit-site/index.min.js 26 kB
build/edit-site/style-rtl.css 5.01 kB
build/edit-site/style.css 5.01 kB
build/edit-widgets/index.min.js 16.6 kB
build/edit-widgets/style-rtl.css 4.01 kB
build/edit-widgets/style.css 4.02 kB
build/editor/index.min.js 38.3 kB
build/editor/style-rtl.css 3.92 kB
build/editor/style.css 3.91 kB
build/element/index.min.js 3.44 kB
build/escape-html/index.min.js 739 B
build/format-library/index.min.js 5.72 kB
build/format-library/style-rtl.css 668 B
build/format-library/style.css 669 B
build/hooks/index.min.js 1.76 kB
build/html-entities/index.min.js 628 B
build/i18n/index.min.js 3.73 kB
build/is-shallow-equal/index.min.js 710 B
build/keyboard-shortcuts/index.min.js 1.74 kB
build/keycodes/index.min.js 1.49 kB
build/list-reusable-blocks/index.min.js 2.07 kB
build/list-reusable-blocks/style-rtl.css 838 B
build/list-reusable-blocks/style.css 838 B
build/media-utils/index.min.js 3.08 kB
build/notices/index.min.js 1.07 kB
build/nux/index.min.js 2.31 kB
build/nux/style-rtl.css 747 B
build/nux/style.css 743 B
build/plugins/index.min.js 1.99 kB
build/primitives/index.min.js 1.06 kB
build/priority-queue/index.min.js 791 B
build/react-i18n/index.min.js 924 B
build/redux-routine/index.min.js 2.82 kB
build/reusable-blocks/index.min.js 2.56 kB
build/reusable-blocks/style-rtl.css 256 B
build/reusable-blocks/style.css 256 B
build/rich-text/index.min.js 10.8 kB
build/server-side-render/index.min.js 1.64 kB
build/shortcode/index.min.js 1.68 kB
build/token-list/index.min.js 848 B
build/url/index.min.js 1.95 kB
build/viewport/index.min.js 1.28 kB
build/warning/index.min.js 1.16 kB
build/widgets/index.min.js 6.48 kB
build/widgets/style-rtl.css 1.04 kB
build/widgets/style.css 1.04 kB
build/wordcount/index.min.js 1.24 kB

compressed-size-action

@youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Jun 14, 2021

🤔 What happens if we do this to the editor styles as well (the ones injected dynamically)?

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jun 14, 2021

Blowing my mind with that idea. Do you happen to recall where we rewrite body to editor-styles-wrapper ? Happy to give it a shot.

I wanted to also add the thought that we could make the changes suggested here to everything except the left/right auto margins. While that breaks the nav block issue I noted (I can look for other fixes), it would still let us lower the specificity of the top/bottom margin rules, which would be a nice win.

@youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Jun 14, 2021

@jasmussen it happens here https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/src/utils/transform-styles/transforms/wrap.js

basically, the ideal there would be to wrap without altering the specificity, not sure that's possible though

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jun 14, 2021

I gave it a shot, but I think that lowers the wrong specificity:

Screenshot 2021-06-14 at 13 15 30

So this margin rule from the theme:

Screenshot 2021-06-14 at 13 15 46

Now it loses to the block style:

Screenshot 2021-06-14 at 13 15 37

In other words, it does work as intended. But we need to apply this to everything we want to be overridden. So in the case of the left aligned gallery, I can add the where to the editor style and it'd work.

basically, the ideal there would be to wrap without altering the specificity, not sure that's possible though

We could potentially use :is here: https://css-tricks.com/almanac/selectors/i/is/#specificity-of-is — but I may be missing some nuance of what you're saying.

@gwwar
Copy link
Contributor

@gwwar gwwar commented Jun 29, 2021

There is a new selector, called :where, which is able to provide default styles with lowered specificity.

TIL, that's pretty neat!

Zooming out a bit, did we have specificity targets for different parts of the editor? Using a comma delimited notation (style, id, class, element) with resets we'd probably stick with might have scores from (0,0,0,X), while blocks might be aiming for something like (0,0,1,X) to (0,0,2,X)?

As a reminder for folks 👀 CSS specificity is mostly counting. And no matter the number of items in one category it can't beat the next one. Using a comma delimited notation, if I say had 15 element rules in a selector a single class selector would still beat it.

In Joen's example in #32659 (comment)

This would be (0,0,1,1)
121884045-c1d50280-cd12-11eb-97c9-e9adb084008f

vs (0,0,1,0) (with the :where pseudo selector).
121884026-bc77b800-cd12-11eb-96d3-f19adde3e0eb

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jun 29, 2021

Zooming out a bit, did we have specificity targets for different parts of the editor?

It's been adhoc so far, but it might be good to start thinking about this in a more systematic way, in part because we have this new :where tool to provide very soft defaults, but perhaps more importantly because for global styles to work, the specificity of block styles has to be as low as possible for any theme.json specified overrides to take hold.

Do you have any good suggestions on how to start systematizing that? Are there any principles we can extract in documentation? Any linters we can add?

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jun 29, 2021

Curious, though, how did you calculate the specificity of .editor-styles-wrapper :where([data-block])? I suspect your value is correct, but I wouldn't know how to parse that particularly gnarly selector into the three categories, and online calculators get it wrong, probably due to unawareness of the particular qualities of :where; this one gives me 0,3,0.

@gwwar
Copy link
Contributor

@gwwar gwwar commented Jun 29, 2021

Curious, though, how did you calculate the specificity of .editor-styles-wrapper :where([data-block])?

Manually counting, checkout also https://www.w3.org/TR/selectors/#specificity.

From the spec :where and it's argument are always zero

The Specificity-adjustment pseudo-class, :where() [...] neither the :where() pseudo-class, nor any of its arguments, contribute to the specificity of the selector—its specificity is always zero.

So with that in mind .editor-styles-wrapper :where([data-block]) simplifies to (0,0,1,0). And we can see that figure.wp-block-gallery does indeed beat it, which makes sense since it has a specificity of (0,0,1,1).

Do you have any good suggestions on how to start systematizing that? Are there any principles we can extract in documentation? Any linters we can add?

So probably the hierarchy of styling will probably be something like:

Reset / Base Styles -> Block Styles -> [ Custom Theme / Editor Styles ] -> Global Styles -> Specific Block User Overrides

If we can provide consistent specificity targets at the first two layers, then it should be easier to provide guidelines for folks building themes, and the necessary overrides for anything above that. Note that we don't necessarily have to use strict specificity guidelines if we're sandboxing styling in other ways (say isolating rules via iframes and such).

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jun 29, 2021

Thank you as always for teaching me, I very much appreciate that.

Your hierarchy reminds me of #20331, which seems like healthy serendipity worth building on. The tricky bit is the theme, as it requires some care on part of the author to not mess up the global style user overrides. However as those same properties are provided by theme.json directly, hopefully that helps manage the complexity.

@jasmussen jasmussen added this to 👀 PRs needing review in Navigation block Jun 30, 2021
@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jul 6, 2021

I've got two navigation block PRs that are blocked on this one landing first: #33021 and #32574. I'd really love a review of this one, and ideally a green check because all it really does it reduce specificity so it should be safe. Thanks.

@jasmussen jasmussen requested a review from WordPress/gutenberg-core Jul 6, 2021
@youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Jul 6, 2021

Reset / Base Styles -> Block Styles -> [ Custom Theme / Editor Styles ] -> Global Styles -> Specific Block User Overrides

This hierarchy makes sense but let me clarify the difficulty a bit here. In the frontend, the equivalent hierarchy is:

Block Styles ->  Custom Theme -> Global Styles -> Specific Block User Overrides

So our goal is to match both hierarchies as closely as possible, there are thought two main differences:

1- in the editor we need a Reset / Base Styles, this reset styles is basically something like:

.editor-styles-wrapper p { all: revert; }

that's just an example, the important bit here is that we're trying to reset the styles of the base elements to the browser's defaults by overriding the WP-Admin common.css styles. But by doing so we increased the specificity of p which means this creates a potential issue with the styles that are loaded later "Block Styles" and "Theme Editor Styles".

2- To solve the issue above what we did (a long time ago) is to add .editor-styles-wrapper to all editor styles. Meaning a theme writing p { color: red } in its editor styles will result in the following being loaded:

.editor-styles-wrapper p { color: red; }

The problem remains though for Block Styles which we don't wrap in .editor-styles-wrapper yet. So we have two potential solutions:

1- Try to wrap all the styles that can affect blocks with .editor-styles-wrapper, the important remaining one for us is "Block Styles" (the block-library package stylesheets)

2- Make sure the "reset" and the editor styles don't affect the specificity of their rules when they wrap the CSS with. .editor-styles-wrapper which this PR is attempting (I think), I think we're not there yet though.

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jul 7, 2021

Thanks @youknowriad and @gwwar for the reviews. Before we go deeper on the path of a hierarchy, is this PR okay to land as an interim step?

@jasmussen jasmussen force-pushed the try/reduced-specificity-resets branch from aceeea3 to 641ec16 Jul 8, 2021
@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jul 8, 2021

Thank you for the review, your reviews are the best, and excellent catch with the onselect alignment. Here's trunk:

trunk

Here's this branch before rebase:

this branch

Here's the branch after rebase:

after rebase

Two immediate take-aways: you were completely right about the onselect alignment thing. I actually had a separate fix for that in #32574, but that may no longer be necessary, since it's possible it was inadvertently fixed by #33088 (which intended to be a more general refactor).

The other take-away is the added margin and padding. This is a side-effect of the reduced specificity of these styles, you can see in this GIF how the added margin and padding rules are from TT1:

tt1

Those rules are rather old and were wrong even before this PR, I've trac'ed them here: https://core.trac.wordpress.org/ticket/53220

There might be a few other such issues caused by theme styles having previously being overridden by higher specificity, now winning out. But I still think it's worth it to land this PR and "de-escalate" the specificity wars.

Thanks again for the review, I'll ask for a sanity check again before I merge.

@Mamaduka
Copy link
Member

@Mamaduka Mamaduka commented Jul 8, 2021

In general, I think this looks good. I found few minor issues that can be addressed in follow-up PRs.

Tested with two WP installations side by side, using Demo content + Navigation (horizontal) justified to the right.

Installations:

A. Gutenberg build with this branch
B. Gutenberg 11.0

Themes tested

  • TT1
  • TT1 Blocks
  • 2020
  • 2019

Minor issues I spotted on this branch

TT1 Blocks

  • None

TT1

  • The gallery with no alignment isn’t centered.

2020

  • Quote with no alignment isn’t centered.
  • Gallery with no alignment isn’t centered.

2019

  • Lists are centered.

Screenshots

this branch 11.0
CleanShot 2021-07-08 at 13 14 58 CleanShot 2021-07-08 at 13 15 08
this branch 11.0
CleanShot 2021-07-08 at 13 21 38 CleanShot 2021-07-08 at 13 21 55
@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jul 8, 2021

Thanks for looking. I have this feeling those are all issues best addressed in the theme itself, does that seem right to you? If so, I'd be happy to create tickets for those and/or look at patches. Or did you get a sense that this was fixable in the block editor, without raising specificity again?

@Mamaduka
Copy link
Member

@Mamaduka Mamaduka commented Jul 8, 2021

I'm not sure. I think some should be fixed in Gutenberg and some in themes.

In the case of the Gallery with no alignment, margin: 0 is coming from block's editor.css - https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/gallery/editor.scss#L22.

2020 theme adds the margin: 0 rule for blockquote in the editor, but then blockquotes are correctly centered on the front-end.

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jul 8, 2021

Excellent catch on the gallery. It appears to not be a problem with block themes as the left/right auto margins are enforced as !important.

But it is indeed a problem in classic themes, as the margin: 0 rule now wins out:

Screenshot 2021-07-08 at 12 59 08

While I still think lowering that specificity, and actually updating the rule of the Gallery block to not have such a blanket zero margin output, it does make me question whether it's actually a good idea to update those classic rules at all. We might want to do it for the reset styles only after all 🤔

Block themes will still benefit from the new lowered specificity of the reset style. What do you think? And thanks again for looking.

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jul 8, 2021

Though it is worth noting, that if we don't include the lowered specificity, then improvements from #33021, just as an example, can't benefit classic themes. This will likely expand more widely as more properties like margins and paddings are absorbed into global styles.

@Mamaduka
Copy link
Member

@Mamaduka Mamaduka commented Jul 8, 2021

The Gallery block issue would most visible, so it might be good to fix that as a follow-up.

I think it would be easier to gather more feedback if we merged this PR.

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Jul 8, 2021

I'm waffling on merging it, as I'm going on vacation next week, so I won't have time to follow up on this. So at this moment I'm leaning towards pausing this one until I get back.

@jasmussen jasmussen force-pushed the try/reduced-specificity-resets branch from 641ec16 to f2cb2e8 Aug 5, 2021
@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Aug 5, 2021

I'm back from AFK and hope to land this soon. Since it also appears to improve a 2019 theme issue, it would be good to land soon. I've rebased to test that it's still valid, and will be investigating the gallery block issue and how that can be fixed, and then either push a fix to this branch, or have a followup ready.

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Aug 5, 2021

Alright, after investigating a bit further, it appears that the issue, as so well reviewed by @Mamaduka in #32659 (comment), is primarily an issue in classic themes. Here's what happens in 2020 that causes the gallery to be left aligned. The theme outputs this rule:

Screenshot 2021-08-05 at 11 35 10

This rule is common for themes, as the figure element is born with intrinsic margins, including left and right margins. But with the new lowered specificity for the left/right auto margins, the following rule that is intended to center the top level block now loses out in specificity:

Screenshot 2021-08-05 at 11 35 00

So there are two things we can do here.

1. Restore specificity for those auto margins.

This would be a bit of a bummer as I recall those margins being frustrating when I mostly worked on classic themes. But it might be a pragmatic choice for backward compatibility. Block themes won't have to worry about them, at least. Keep something like this in classic.scss, in other words:

// Provide baseline auto margin for centering blocks.
// Specificity is kept at this level as many classic themes output
// rules like figure { margin: 0; } which would break centering.
.editor-styles-wrapper .wp-block {
	margin-left: auto;
	margin-right: auto;
}

2. Expand the list of blocks with specific auto margins.

This PR already had the following in place, to center lists:

// This needs specificity to override theme editor styles
// such as ol { margin-left: 0; }.
.editor-styles-wrapper ol.wp-block,
.editor-styles-wrapper ul.wp-block {
	margin-left: auto;
	margin-right: auto;
}

We could expand that definition to include the figure element, which fixes the 2020 issue and still keeps the generic .wp-block class low-specificity. Something like this:

// This needs specificity to override theme editor styles
// such as ol { margin-left: 0; }.
.editor-styles-wrapper figure.wp-block,
.editor-styles-wrapper ol.wp-block,
.editor-styles-wrapper ul.wp-block {
	margin-left: auto;
	margin-right: auto;
}

It fixes it for 2020:

Screenshot 2021-08-05 at 11 47 07

The downside to that approach is that it very slightly increases the specificity of those margin rules for classic themes. 0,2,1 for .editor-styles-wrapper figure.wp-block compared to 0,2,0 for .editor-styles-wrapper .wp-block.

Personally I'm slightly in favor of option 2, because those margins are annoying. But it's not a strong preference, and I'd be happy to go the potentially more pragmatic route.

Before I push anything, I'd love your thoughts. Thanks.

@youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Aug 5, 2021

  1. Expand the list of blocks with specific auto margins.

Personally I think this is a mistake and should be removed from all blocks, it shouldn't be the responsibility of the block to define its margins but should be the responsibility of its container ideally. Of course there might be special cases to clear out margins... but margin auto is very specific to the "flow" layout.

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Aug 5, 2021

Going forward with block themes, there won't be any auto margin applied. But given how legacy themes have assumed those auto margins for a while, would you be okay with option 1 then?

@youknowriad
Copy link
Contributor

@youknowriad youknowriad commented Aug 5, 2021

Yes, I think option 1 is better. These "classic" styles are not "reset" styles meaning It's not clear whether they should have less specificity or more than editor styles. So I think keeping that rule as is is a good approach.

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Aug 5, 2021

Awesome. Things still work well here:

Screenshot 2021-08-05 at 12 17 09

.wp-block {
// Deprecated style needed for the block widths and alignments.
// for themes that don't support the new layout (theme.json).
html :where(.wp-block) {
max-width: $content-width;

// Provide every block with a default base margin. This margin provides a consistent spacing

This comment has been minimized.

@youknowriad

youknowriad Aug 5, 2021
Contributor

Noting that these margins can also get removed by figure { margin: 0; } do you think we should also move them up?

This comment has been minimized.

@jasmussen

jasmussen Aug 5, 2021
Author Contributor

I'd personally prefer to keep them there, as they as I recall were the most disruptive to work with, and the reduced specificity of those margins were the biggest win of this approach. It should also be less disruptive as it's vertical margins.

However I'm happy to move them up either in this PR or as a followup if it turns out to be necessary?

This comment has been minimized.

@youknowriad

youknowriad Aug 5, 2021
Contributor

the biggest win of this approach.

I'm curious, in which situation the smaller specificity benefit classic themes here. What kind of styles classic themes apply here?

This comment has been minimized.

@jasmussen

jasmussen Aug 5, 2021
Author Contributor

I'm thinking mostly of developing new classic themes, or refactoring older ones.

This comment has been minimized.

@kjellr

kjellr Aug 5, 2021
Contributor

I agree with @jasmussen that it should be fine to keep these where they are. I tested in a few themes and didn't notice this causing any issues. If they did interfere with that figure rule, it'll just be the vertical margin (and may actually have no effect if there's margin on the items above + below the figure anyway).

This comment has been minimized.

@jasmussen

jasmussen Aug 5, 2021
Author Contributor

Thanks for looking Kjell. Cool, in any case it's an easy follow up which I will be keeping my eyes peeled on and ready to change if need be. I'd like to land this PR tomorrow or Monday, so there's still time to chime in here.

@jasmussen
Copy link
Contributor Author

@jasmussen jasmussen commented Aug 9, 2021

Alright, it's Monday and I'll be here for any followups. This one after recent iterations feels very safe to me, so I expect when I merge it in a moment it will be a non issue. Nevertheless, let's keep an eye out on theme styles in the editing canvas!

@jasmussen jasmussen merged commit 7f97d5c into trunk Aug 9, 2021
20 checks passed
20 checks passed
@github-actions
Compute current and next stable release branches
Details
@github-actions
test
Details
@github-actions
Check (14)
Details
@github-actions
Checks (12)
Details
@github-actions
Admin - 1 Admin - 1
Details
@github-actions
Run performance tests
Details
@github-actions
pull-request-automation (14)
Details
@github-actions
test (12.2, gutenberg-editor-initial-html, 14)
Details
@github-actions
JavaScript (12)
Details
@github-actions
Checks (14)
Details
@github-actions
Admin - 2 Admin - 2
Details
@github-actions
JavaScript (14)
Details
@github-actions
Admin - 3 Admin - 3
Details
@github-actions
Admin - 4 Admin - 4
Details
@github-actions
Bump version
Details
@github-actions
Build Release Artifact
Details
@github-actions
Mobile
Details
@github-actions
Create Release Draft and Attach Asset
Details
Navigation block automation moved this from 👀 PRs needing review to ✅ Done Aug 9, 2021
@jasmussen jasmussen deleted the try/reduced-specificity-resets branch Aug 9, 2021
@github-actions github-actions bot added this to the Gutenberg 11.3 milestone Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants