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

Global Styles: Add Global styles to inline block styles #39571

Closed
wants to merge 1 commit into from

Conversation

scruffian
Copy link
Contributor

What?

Only load the Global Styles CSS for blocks that are used on the page.

Why?

So we only load the required CSS.

How?

This adds the Global Styles CSS for each block to the inline CSS for that block. This mean that the CSS is loaded in a different place to at present. An alternative approach would be to keep the CSS in the global-styles-inline-css so that the load order is the same. I tried to do this, but I couldn't find where the CSS is actually enqueued for each block.

Testing Instructions

  • Switch to TT2
  • Go to a page without a quote block on
  • Check that this CSS isn't in either the global-styles-inline-css or wp-block-quote-css:
.wp-block-quote {
	border-width: 1px;
}
  • Go to a page with a quote block on
  • Check that this CSS is not in the global-styles-inline-css but is in wp-block-quote-css:
.wp-block-quote {
	border-width: 1px;
}

@scruffian scruffian added the Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json label Mar 18, 2022
@scruffian scruffian requested a review from oandregal March 18, 2022 09:22
@scruffian scruffian self-assigned this Mar 18, 2022
@@ -76,13 +76,13 @@ function gutenberg_get_global_styles( $path = array(), $context = array() ) {
function gutenberg_get_global_stylesheet( $types = array() ) {
// Return cached value if it can be used and exists.
// It's cached by theme to make sure that theme switching clears the cache.
$can_use_cached = (
$can_use_cached = false; /* (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just for testing...

@aristath aristath self-requested a review March 18, 2022 09:29
$general_selector_rules = static::to_ruleset( $selector, $declarations );
if ( 'styles' === $metadata['path'][0] && 'blocks' === $metadata['path'][1] ) {
$block_name = str_replace( 'core/', '', $metadata['path'][2] );
wp_add_inline_style(
Copy link
Member

Choose a reason for hiding this comment

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

It terms of approach, I think we want to tap into the existing API: it already has logic to decide whether to enqueue the block's CSS as a single stylesheet with them all or a separate one per block. Otherwise, we'll need to replicate that logic for the global styles.

In terms of implementation, WP_Theme_JSON has remained agnostic about WordPress APIs (get_stylesheet, wp_add_inline_style, etc), which was the WP_Theme_JSON_Resolver responsibility. I think it's benefitial to separate those concerns.

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 already has logic to decide whether to enqueue the block's CSS as a single stylesheet with them all or a separate one per block

Do you know where this is? I can't find it!

Copy link
Member

Choose a reason for hiding this comment

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

I don't! Looking for should_load_separate_block_assets can perhaps help to start the investigation.

Copy link
Member

@aristath aristath Apr 1, 2022

Choose a reason for hiding this comment

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

We could maybe also try using the wp_enqueue_block_style() function 🤔
Otherwise yes, you can just look up where wp_should_load_separate_core_block_assets() is used to see the details of the current implementation 😉
If you need any assistance with this, don't hesitate to reach out

@scruffian
Copy link
Contributor Author

An alternative version of this is here: #40513

@scruffian scruffian closed this May 25, 2022
@scruffian scruffian deleted the update/global-styles-block-styles branch May 25, 2022 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants