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

font-size presets: fix specificity in editor #21969

Merged
merged 2 commits into from
May 6, 2020

Conversation

oandregal
Copy link
Member

Fixes #21759

This is a leftover of an experiment where we introduced font-size declarations in the blocks by default that we reverted later.

@oandregal oandregal changed the title Remove root pseudoclass Remove root pseudoclass for font-size classes Apr 29, 2020
@oandregal oandregal self-assigned this Apr 29, 2020
@oandregal oandregal added [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Type] Bug An existing feature does not function as intended labels Apr 29, 2020
@oandregal oandregal added this to the Gutenberg 8.0 milestone Apr 29, 2020
@github-actions
Copy link

github-actions bot commented Apr 29, 2020

Size Change: +565 B (0%)

Total Size: 817 kB

Filename Size Change
build/annotations/index.js 3.62 kB -2 B (0%)
build/block-directory/index.js 6.23 kB +1 B
build/block-editor/style-rtl.css 10.2 kB +2 B (0%)
build/block-editor/style.css 10.2 kB +2 B (0%)
build/block-library/index.js 114 kB +7 B (0%)
build/block-library/style-rtl.css 7.18 kB +34 B (0%)
build/block-library/style.css 7.18 kB +34 B (0%)
build/components/index.js 179 kB -16 B (0%)
build/core-data/index.js 11.4 kB -1 B
build/edit-navigation/index.js 4.05 kB +512 B (12%) ⚠️
build/edit-post/index.js 27.6 kB -2 B (0%)
build/editor/index.js 43.6 kB -2 B (0%)
build/format-library/index.js 7.63 kB -1 B
build/keyboard-shortcuts/index.js 2.51 kB -1 B
build/media-utils/index.js 5.29 kB -1 B
build/server-side-render/index.js 2.68 kB -1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/api-fetch/index.js 4.08 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 761 B 0 B
build/block-editor/index.js 106 kB 0 B
build/block-library/editor-rtl.css 7.03 kB 0 B
build/block-library/editor.css 7.03 kB 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.1 kB 0 B
build/components/style-rtl.css 16.9 kB 0 B
build/components/style.css 16.9 kB 0 B
build/compose/index.js 6.66 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.44 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/style-rtl.css 485 B 0 B
build/edit-navigation/style.css 485 B 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/index.js 10.9 kB 0 B
build/edit-site/style-rtl.css 5.11 kB 0 B
build/edit-site/style.css 5.11 kB 0 B
build/edit-widgets/index.js 7.5 kB 0 B
build/edit-widgets/style-rtl.css 4.67 kB 0 B
build/edit-widgets/style.css 4.66 kB 0 B
build/editor/editor-styles-rtl.css 428 B 0 B
build/editor/editor-styles.css 431 B 0 B
build/editor/style-rtl.css 3.27 kB 0 B
build/editor/style.css 3.27 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.12 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.67 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 14.8 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@oandregal oandregal added CSS Styling Related to editor and front end styles, CSS-specific issues. [Type] Regression Related to a regression in the latest release and removed [Type] Bug An existing feature does not function as intended labels Apr 29, 2020
@aduth
Copy link
Member

aduth commented Apr 29, 2020

To clarify, it looks like this is merely intending to revert styles to as they existed prior to the changes here:

https://github.com/WordPress/gutenberg/pull/21153/files#diff-fded095b4f7622cd5e81d8193bf2f4b4

@aduth
Copy link
Member

aduth commented Apr 29, 2020

One thing I'm observing in testing is that when assigning "Huge" font size in the TwentyTwenty theme, it doesn't actually appear large.

It's because the style introduced here is being wrapped as .editor-styles-wrapper p (via transform-styles), and thus has higher specificity than the .has-huge-font-size style:

https://github.com/WordPress/gutenberg/pull/21428/files#diff-b175fef433360d2d2c873fdd717e5f05R97

Given the timing of #21428, I'm not sure if this would be "expected".

@oandregal
Copy link
Member Author

To clarify, it looks like this is merely intending to revert styles to as they existed prior to the changes here:

Yes, exactly.

One thing I'm observing in testing is that when assigning "Huge" font size in the TwentyTwenty theme, it doesn't actually appear large.

You mean, TwentyNnineteen, right? That's a known issue for that theme. The reason is that we stopped adding inline styles in the editor when the user selects a preset font (like huge), so it behaves like the front-end.

@youknowriad
Copy link
Contributor

I think the root removal is a good thing especially since it’s affecting the frontend but we do the same for the colors on the same file and I believe we should be consistent;
I believe we should probably not include the PR just to give a bit more time since it’s not a 8.0 regression and test the implications of removing the root selector in both editor and frontend properly.

@oandregal
Copy link
Member Author

I still think this is low risk/high reward, in case we still wanted to go ahead with this. I'm also fine with slating this for the next release given that it's just 2 weeks away (I just feel bad for not having spotted this earlier due to AFK issues).

I think the root removal is a good thing especially since it’s affecting the frontend but we do the same for the colors on the same file and I believe we should be consistent;

Based on my reading of #15167 I understand that, for colors, it was done because some hover/visited statuses that would have higher specificity than the classes. My archeology skills are failing to understand why we decided for increasing the specificity with :root and not adding an extra selector .color-class:hover, although it was mentioned at some point. So I guess colors is a different use case.

.has-normal-font-size {
font-size: 16px;
}
.has-small-font-size {
Copy link
Contributor

@kjellr kjellr Apr 29, 2020

Choose a reason for hiding this comment

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

This is working to fix the issue in #21759, however I'm not sure the bare .has-xx-font-size classes are actually specific enough to work out of the box now. When testing with a theme that supplies no editor styles (Gutenberg Starter Theme), it appears that these rules are just overridden by the default .editor-styles-wrapper p font size rule.

Screen Shot 2020-04-29 at 9 15 47 AM

Do we need to at least change these to .editor-styles-wrapper .has-xx-font-size? That should allow them to kick in by default, but still be overridden by theme styles (assuming the theme styles have the .editor-styles-wrapper selector added via add_editor_style()).

Copy link
Member Author

Choose a reason for hiding this comment

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

The root problem seems to me that we bump up the specificity for theme's stylesheets but not for its dependencies (the defaults provided by Gutenberg). The ideal solution would be that we also wrap the block-library stylesheets with editor-styles-wrapper at runtime. However, I fear that ship may have already sailed. I'll be more than extremely happy to be proved wrong and go with this approach, though.

Alternative solution: increase the specificity of these classes, only for the editor. My only concern is how would we do it. All the options I see make me sigh. I pushed a fix by adding the editor-styles-wrapper class in this file, although I know that we tend to avoid adding the editor wrapper class to anything that goes to the front-end. The alternatives I've considered include:

  • Replicate the classes in both files => we enqueue the classes twice + duplicate source of truth (if we change them in the future we'll have to update both places and can be easily forgotten).
  • Extract them to a file that will be imported here and in editor.scss => we enqueue the classes twice + it adds indirection.

Note that this happens in every theme that don't provide a font size palette themselves (for default themes: all but TwentyNineteen and TwentyTwenty) since we no longer inline the style declaration for presets.


[1] And also TwentyNineteen, but for different reasons: although it does provide a palette, it is identical to Gutenberg's except for the removal of Medium text size, so it doesn't care about enqueuing the classes itself. For that reason, it behaves like the themes that don't opt-in into the font-size palette. It could be argued that this case should be fixed in the theme itself. If we provide a fix for the other themes, we're also fixing this, though.

Copy link
Member Author

Choose a reason for hiding this comment

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

What if we extracted the palettes (color, font, gradients) to its own stylesheets and only enqueue them if the theme hasn't declared support?

It somehow makes extracting the files clearer for me, it'd be reasonable that they contain styles for both editor&front-end, and also is good practice not to ship CSS we don't use. At the same time, it is also a bit more work than what we have here and, given the sheer amount of things to do, I find it difficult to justify (although it's the approach I'd do otherwise).

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternative solution: increase the specificity of these classes, only for the editor. My only concern is how would we do it. All the options I see make me sigh. I pushed a fix by adding the editor-styles-wrapper class in this file, although I know that we tend to avoid adding the editor wrapper class to anything that goes to the front-end.

Can't we just have two sets of styles — one in editor.scss, and one in theme.scss? It would be in two separate places, but that's what we usually do when we need separate styles for the front and back end.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, but isn't theme.scss only enqueued if theme declares support for wp-block-styles (which is something themes that don't declare font-size palettes are less likely to do)? I thought that wouldn't fix the issue for most themes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for explaining so thoroughly, Kjell.

Master:

In other words, the custom theme rule will be ignored completely in the front end. 😞

So far, so good.

This PR:

.has-huge-font-size { font-size: 72px } // Custom Theme Rule
.has-huge-font-size { font-size: 72px } // Default Gutenberg Rule

Cool, works because it comes after, also good. Side question, though: when a theme referenced class name such as this one clashes with Gutenberg, is either the theme or the editor doing something wrong?

p { font-size: inherit; } rule that is prefixed with .editor-styles-wrapper. This has higher specificity than the new .has-xx-font-size, so it cancels out the custom font size rule in the editor.

Yes, I'm running into this with my own theme I'm building, where this:

p {
	margin-top: 1em;
	margin-bottom: 1em;
}

in the editor is cancelled out by:

.editor-styles-wrapper .block-editor-block-list__block {
	margin-top: 28px;
	margin-bottom: 28px;
}

It seems a similar issue, the tension between the editor wanting to provide some baseline styles, and the theme needing to trivially override those.

This works, but it provides an unnecessary .editor-styles-wrapper .has-xx-font-size rule to the frontend. Not a huge problem, but not ideal.

There we go, that seems to be the crux of the issue.

As I read this, my instinct is to think that if we provide a thorough comment, this is an okay tradeoff for now. Take the entire introduction in the normalization file, it's one big "ugh wp-admin bleed" disclaimer. So it all trails back to that.

But we are more aware than ever! An iframe is potentially on the horizon, which would allow us to throw out most, if not all, of the normalization stylesheet, prompting us to be able to refactor this also.

I wonder if we should make a specific keyword for such a refactor — in my own private projects, I often write @todo so I can search for that identifier. We could make a @todo-editor-styles, which we can then search for if either an iframe, or Shadow DOM protection, or better scoping of wp-admin styles land.

Copy link
Contributor

Choose a reason for hiding this comment

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

Side question, though: when a theme referenced class name such as this one clashes with Gutenberg, is either the theme or the editor doing something wrong?

That's a good philosophical question. 😄 I don't think either is doing it wrong — we expect some Gutenberg styles to be overridden by the themes in general.

As I read this, my instinct is to think that if we provide a thorough comment, this is an okay tradeoff for now. Take the entire introduction in the normalization file, it's one big "ugh wp-admin bleed" disclaimer. So it all trails back to that.

Good point. I think you're probably right. This works for now, and things will be (hopefully) changing soon. So that may be good enough for the moment. @nosolosw what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Reading @kjellr outline above it seems to me that the issue in twofold: how to make palettes work with 1) themes that don't provide one and 2) themes that provide one with the same class names that Gutenberg defaults.

In its current state, this PR fixes 1. In addition to this, should we also enqueue the palettes only if the theme hasn't provided one? That should fix 2 as well.

Sorry if I missed anything and this doesn't make any sense 😅 I'm from mobile and AFK so my brain is in low-power mode. I join the wishes that we can remove this altogether in the not so distant future!

Copy link
Member Author

Choose a reason for hiding this comment

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

Re-reading this thread I gather we're ok with this solution for now. Is that right @kjellr @jasmussen ? If so I'd need an 💚

I was thinking this could land shorty so it can be in for next week release. I'd like to avoid having the :root for font-sizes for much longer as it was only added for 7.9 because we forgot to remove it after some experiments we did (can be considered a regression).

If the other issue I mentioned (conflicts with themes that provide the same class names than Gutenberg) is pressing I can prepare a different PR to solve that (I believe the answer to that issue would be to not enqueue the color & font-sizes classes if the theme has declared its own).

Copy link
Contributor

Choose a reason for hiding this comment

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

If the other issue I mentioned (conflicts with themes that provide the same class names than Gutenberg) is pressing I can prepare a different PR to solve that (I believe the answer to that issue would be to not enqueue the color & font-sizes classes if the theme has declared its own).

That sounds like a promising solution. 👍

@aduth aduth removed this from the Gutenberg 8.0 milestone Apr 29, 2020
@oandregal oandregal added this to the Gutenberg 8.1 milestone Apr 29, 2020
Copy link
Contributor

@kjellr kjellr left a comment

Choose a reason for hiding this comment

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

As discussed, this fixes the problem for now. 👍 Thanks, @nosolosw!

@oandregal oandregal merged commit 7da5571 into master May 6, 2020
@oandregal oandregal deleted the fix/remove-root-pseudoclass branch May 6, 2020 13:22
@oandregal oandregal changed the title Remove root pseudoclass for font-size classes font-size presets: fix specificity in editor May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Type] Regression Related to a regression in the latest release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Global Styles: Root font-size utility classes mismatch theme settings.
5 participants