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

Block API: Allow more than 1 block stylesheets #32510

Merged
merged 20 commits into from
Oct 7, 2021

Conversation

aristath
Copy link
Member

@aristath aristath commented Jun 8, 2021

Description

This PR aims to allow enqueing multiple stylesheets per-block.

Inside a block.json file:

Allow defining style and editorStyle as an array. In the case of the post-comments-form block we needed to also enqueue styles for the button block. This is now possible by changing the value from

{
	"style": "wp-block-post-comments-form"
}

to

{
 	"style": [ "wp-block-post-comments-form", "wp-block-buttons", "wp-block-button" ]
}

From a theme/plugin

If a theme needs to add styles to a block, they can now do it using the wp_enqueue_block_style function:

add_action( 
	'after_setup_theme',
	function() {
		$args = array( // Same args used for wp_enqueue_style().
			'handle' => 'my-theme-site-title',
			'src'    => get_theme_file_uri( 'assets/blocks/site-title.css' ),
		);
		// Add "path" to allow inlining asset if the theme opts-in.
		$args['path'] = get_theme_file_path( 'assets/blocks/site-title.css' );

		// Enqueue asset.
		wp_enqueue_block_style( 'core/site-title', $args );
	}
);

How has this been tested?

  • Tested in a block theme, confirming that the post-comments-form block still loads the button block assets.
  • Added a file in a block-based theme, assets/blocks/site-title.css and confirmed that the code above adds the styles when the template contains a site-title block.
  • Added the same file in a classic theme and confirmed the same behavior.
  • Confirmed the same behavior both in the frontend and the editor.

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).

@aristath aristath force-pushed the add/multiple-block-styles branch from 210590e to 384e64b Compare June 9, 2021 11:46
@aristath aristath changed the title WIP - Don't look here yet Allow additiona block stylesheets Jun 9, 2021
@aristath aristath changed the title Allow additiona block stylesheets Allow additional block stylesheets Jun 9, 2021
@aristath aristath changed the title Allow additional block stylesheets Allow more than 1 block stylesheets Jun 9, 2021
@aristath aristath requested a review from gziolo June 9, 2021 13:15
@aristath aristath marked this pull request as ready for review June 9, 2021 13:35
@gziolo gziolo added [Feature] Block API API that allows to express the block paradigm. [Type] New API New API to be used by plugin developers or package users. [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible labels Jun 9, 2021
@gziolo gziolo mentioned this pull request Jun 16, 2021
7 tasks
@aristath aristath force-pushed the add/multiple-block-styles branch from 74bab5a to 9ee2d1f Compare June 16, 2021 13:16
@leeshadle

This comment has been minimized.

@aristath

This comment has been minimized.

@leeshadle

This comment has been minimized.

@aristath aristath force-pushed the add/multiple-block-styles branch from 9ee2d1f to f5217a5 Compare June 17, 2021 06:33
@aristath

This comment has been minimized.

@gziolo
Copy link
Member

gziolo commented Jul 5, 2021

The same issue was raised in https://make.wordpress.org/core/2021/06/23/block-api-enhancements-in-wordpress-5-8/#comment-41466 for scripts. It isn't possible to provide in a simple way multiple scripts of the same type for the block using block.json.

lib/blocks.php Outdated Show resolved Hide resolved
lib/blocks.php Outdated Show resolved Hide resolved
lib/blocks.php Outdated Show resolved Hide resolved
lib/blocks.php Outdated Show resolved Hide resolved
Copy link
Member

@ellatrix ellatrix left a comment

Choose a reason for hiding this comment

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

This will need to be tested in the iframed editors (template editor and FSE).

@aristath aristath force-pushed the add/multiple-block-styles branch from f5217a5 to 5d6373c Compare July 15, 2021 11:55
lib/blocks.php Outdated Show resolved Hide resolved
@aristath
Copy link
Member Author

Many of these things would be a lot easier to do directly in wp-core... I expect the logic to be greatly simplified when this (or a similar implementation) gets backported to core 🤞

lib/blocks.php Show resolved Hide resolved
lib/blocks.php Outdated Show resolved Hide resolved
@aristath aristath force-pushed the add/multiple-block-styles branch from 7433886 to 1d836c1 Compare July 21, 2021 06:14
@aristath aristath force-pushed the add/multiple-block-styles branch from a09a7bc to baac268 Compare October 7, 2021 05:39
lib/blocks.php Outdated Show resolved Hide resolved
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

Let's give it a try. I think it's in good shape after several iterations and covers all I can think of for now. We can further improve e2e tests as noted in the most recent comment.

The biggest remaining tasks are to prepare the dev note and find a way to backport those changes to WordPress core.

The next step to think about is how to bring feature parity for JS assets 😄

Awesome work as always. It's a pleasure to review your outstanding work.

@aristath aristath merged commit 023c113 into trunk Oct 7, 2021
@aristath aristath deleted the add/multiple-block-styles branch October 7, 2021 12:58
@github-actions github-actions bot added this to the Gutenberg 11.8 milestone Oct 7, 2021
@aristath aristath added the [Type] Performance Related to performance efforts label Oct 7, 2021
aristath added a commit to aristath/wordpress-develop that referenced this pull request Nov 4, 2021
noisysocks pushed a commit to noisysocks/wordpress-develop that referenced this pull request Nov 5, 2021
noisysocks pushed a commit to noisysocks/wordpress-develop that referenced this pull request Nov 8, 2021
noisysocks pushed a commit to noisysocks/wordpress-develop that referenced this pull request Nov 8, 2021
noisysocks pushed a commit to noisysocks/wordpress-develop that referenced this pull request Nov 8, 2021
noisysocks pushed a commit to noisysocks/wordpress-develop that referenced this pull request Nov 8, 2021
noisysocks pushed a commit to noisysocks/wordpress-develop that referenced this pull request Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] New API New API to be used by plugin developers or package users. [Type] Performance Related to performance efforts
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants