-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Take block data for Global Styles from block.json #22698
Conversation
Size Change: +46 B (0%) Total Size: 1.12 MB
ℹ️ View Unchanged
|
lib/global-styles.php
Outdated
'color' => '__experimentalColor', | ||
'line-height' => '__experimentalLineHeight', | ||
'font-size' => '__experimentalFontSize', | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure how important is it for this PR but noting that some of these features are not booleans.
For example for the "color" one, you can enable "colors" and "gradients" if the support for these is enabled, theme.json should be able to define three things:
background: gradient;
backgroundColor: red;
color: green;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I've been reviewing the existing style properties and also noticed that background (solid or gradients) was missing from the original hard-coded list in global styles. I'll fix it in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noting also that priority is important here (we can't apply the backgroundColor before the gradient otherwise it's overridden)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking that priority can work like it works today (and in CSS, so people's mental models are aligned): all declarations are transformed to CSS in the order they appear in the theme.json. You can't have both a solid & a gradient background, so I think this assumption should be fine.
I just wanted to raise that by using a top-level field in the
I probably missed something :) |
@@ -1,6 +1,7 @@ | |||
{ | |||
"name": "core/columns", | |||
"category": "layout", | |||
"selector": ".wp-block-columns", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be the default and only needed for blocks that don't have the generated class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, so would you prefer doing:
selector: true
, for blocks that use the generated classselector: <some-other-selector>
for blocks that don't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After Greg's comment I'm thinking of moving this under the supports key and do the same as other experimental features, so we'd have:
supports: {
__experimentalSelector: {boolean|string|object}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
selector: true, for blocks that use the generated class
Why do we need the selector: true
. Can't we just consider it as the default if the "selector" is not specified?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not part of the block type RCF is it? @gziolo ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As automatic selectors we should also use the block styles:
e.g: 'core/quote/large' automatically mapped to .wp-block-quote.is-style-large.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I agree if no selector is specified at all we should target the block by the default class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just consider it as the default if the "selector" is not specified?
Yeah, I think we can.
My mind-frame in coming to this was that it'd be an opt-in mechanism, but it can perhaps work fine by generating the class if no selector is provided. Thinking out loud: what would be the worst that can happen if this isn't opt-in? This is what I can think of:
-
We're duplicating the class generation logic client & server-side. I think this should be fine because that's an area that I don't expect to change.
-
Class conflicts due to namespacing not matching the class name. Ex:
core/media-text
block name =>.wp-block-media-text
class name.custom-library/media-text
block name =>.wp-block-media-text
class name.
I don't think third-party blocks are overriding core's class names, otherwise, this is problematic on its own. So, perhaps what we should do is to generate this class by default: .wp-{namespace}-{block-name}
to prevent conflicts. With the exception that we don't add the namespace for core blocks, so they'll be .wp-{block-name}
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As automatic selectors we should also use the block styles:
e.g: 'core/quote/large' automatically mapped to .wp-block-quote.is-style-large.
@jorgefilipecosta I think adding support for style variations merits its own PR / discussion. The first step would be to look at them as "sets of style attributes" as suggested at #20331 Perhaps after that experiment we end up doing some things (like inlining those values) that remove the need of targeting the existing classes.
Should this new field be added to the block type api? |
Currently, we are using selectors like “core/heading/h1” for global styles. Which means that blocks will need away in the client to know if they are currently affected by a given selector (so their UI can show default values set in theme.json e.g.: font line, height, for a heading 1 etc):
But in the future, we may also want matchers on the server, e.g: to output CSS only for selectors currently used by some block. And that would require a matcher on the server as well. Regarding the sector specification: |
"h1": "h1", | ||
"h2": "h2", | ||
"h3": "h3", | ||
"h4": "h4", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As referred in #22698 (comment), we will need some way to know if a given block instance is h4 or not.
@@ -1,6 +1,7 @@ | |||
{ | |||
"name": "core/columns", | |||
"category": "layout", | |||
"selector": ".wp-block-columns", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As automatic selectors we should also use the block styles:
e.g: 'core/quote/large' automatically mapped to .wp-block-quote.is-style-large.
@jorgefilipecosta that's a neat idea! To make sure I fully follow, there are few things to unpack.
|
@spacedmonkey I think we want to move this under the I saw that there's a task to add the missing fields to the block type API, so I guess whatever we do for the |
I guess blocks could specify something like |
2f90eb7
to
61afe88
Compare
lib/global-styles.php
Outdated
@@ -80,6 +80,13 @@ function gutenberg_experimental_global_styles_get_from_file( $file_path ) { | |||
file_get_contents( $file_path ), | |||
true | |||
); | |||
|
|||
$json_decoding_error = json_last_error_msg(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If theme.json
contains a single comma in the wrong place makes json_decode
to output a void array. As the file grows, this is more likely to happen and more difficult to spot. Adding this code at least helps in quickly determining what's happening by inspecting the error log.
'core/media-text' => array( | ||
'selector' => '.wp-block-media-text', | ||
'supports' => array( 'color' ), | ||
'supports' => array( 'background-color' ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one-liner allows the global context to generate CSS for background-color. Adding it in this PR (which is a refactor to take data from the block.json) because it's so small a change.
The last bit for this PR is figuring out how the block can determine which selector it uses, which was brought up by @jorgefilipecosta I've done some unsuccessful explorations with block variations and now I'm looking at attribute sources. However, I'd think this is PR is ready to be merged and we can iterate on that part in a follow-up PR, in the context in which that part is needed. By merging this I'll be able to unblock some work I plan to do this week, which is to revive the PR to enable the global styles sidebar in edit-site. So, if @jorgefilipecosta or anyone wants to give this a try and perhaps ✔️ if it's ready, I'd happy to merge this as soon as possible. |
This is what I'm thinking for "dynamic selectors" (selectors for blocks that represent many HTML elements, such as core/heading -h1 to h6- and core/list -ol, ul-). They could be captured like this:
block.json for core/list
Essentially: I'm thinking this could work and avoids having a JavaScript callable/function within block.json, so it's parseable by any means we want (JavaScript, PHP, mobile). If folks are convinced about this approach, I can implement it. However, given that this PR doesn't have a use case for this yet (it's a refactor from hardcoding data to take them from block.json) I think I'll still want to merge as it is and explore this approach where it makes sense (for example, in the edit-site/global styles PR I plan to work on next). |
Hi @nosolosw,
This approach makes sense to me. My only worry is that we are just specifying a set of attribute values. What is the difference between block variations? Conceptually block variations are just a set of attribute values. To see if the block matches core/heading/h1 we will need to see if the attributes of the block are equal to attributes. Could we just do the same with variations and verify if the attributes of a block match a variation? |
There is a also a possible solution that would not involve specifying the set of attributes. It is using dom functions and verifying if the block dom container matches the selector using the matches function https://developer.mozilla.org/en-US/docs/Web/API/Element/matches. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can merge this PR so we can continue the iterations.
There are some followups that should be proposed after this PR is merged:
- Have a way to match a selector to a given block.
- Automatically generate selectors based on block styles. So block styles can be defined with theme.json.
If a block doesn't provide a selector, we'll generate the class name: .wp-{namespace}-{block-name}
3346f24
to
41d378b
Compare
Rebased to fix a legit CI error that was presumably fixed by this. |
Follow-up to #20290 and #22491
This PR takes the block data necessary for Global Styles (block selector + the supported properties of a block) from the block registry.
How to test
Make sure that it works as before:
#global-styles-inline-css
has -among other things- these selectors:Make sure changes in block.json affect the global styles
__experimentalColor
to false.npm run build
.Make sure that blocks can only output the properties they support at the block level
Some examples to try: