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

Support adding opt-in visual styles for core blocks #6947

Merged
merged 3 commits into from May 31, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions core-blocks/separator/index.js
Expand Up @@ -8,6 +8,7 @@ import { createBlock } from '@wordpress/blocks';
* Internal dependencies
*/
import './style.scss';
import './theme.scss';

export const name = 'core/separator';

Expand Down
2 changes: 2 additions & 0 deletions core-blocks/separator/theme.scss
@@ -0,0 +1,2 @@
// TODO: Remove this comment when adding theme styles.
// Including an empty file for now so webpack will build an aggregate build/core-blocks/theme.css.
8 changes: 8 additions & 0 deletions docs/extensibility/theme-support.md
Expand Up @@ -151,3 +151,11 @@ body.gutenberg-editor-page .editor-block-list__block[data-align="full"] {
You can use those editor widths to match those in your theme. You can use any CSS width unit, including `%` or `px`.

Further reading: [Applying Styles with Stylesheets](https://wordpress.org/gutenberg/handbook/blocks/applying-styles-with-stylesheets/).

## Default block styles

Core blocks include default styles. The styles are enqueued for editing but are not enqueued for viewing unless the theme opts-in to the core styles. If you'd like to use default styles in your theme, add theme support for `wp-block-styles`:

```php
add_theme_support( 'wp-block-styles' );
```
17 changes: 15 additions & 2 deletions lib/client-assets.php
Expand Up @@ -426,19 +426,32 @@ function gutenberg_register_scripts_and_styles() {
wp_register_style(
'wp-core-blocks',
gutenberg_url( 'build/core-blocks/style.css' ),
array(),
current_theme_supports( 'wp-block-styles' ) ? array( 'wp-core-blocks-theme' ) : array(),
filemtime( gutenberg_dir_path() . 'build/core-blocks/style.css' )
);
wp_style_add_data( 'wp-core-blocks', 'rtl', 'replace' );

wp_register_style(
'wp-edit-blocks',
gutenberg_url( 'build/core-blocks/edit-blocks.css' ),
array( 'wp-components', 'wp-editor' ),
array(
'wp-components',
'wp-editor',
// Always include visual styles so the editor never appears broken.
'wp-core-blocks-theme',
),
filemtime( gutenberg_dir_path() . 'build/core-blocks/edit-blocks.css' )
);
wp_style_add_data( 'wp-edit-blocks', 'rtl', 'replace' );

wp_register_style(
'wp-core-blocks-theme',
gutenberg_url( 'build/core-blocks/theme.css' ),
array(),
filemtime( gutenberg_dir_path() . 'build/core-blocks/theme.css' )
);
wp_style_add_data( 'wp-core-blocks-theme', 'rtl', 'replace' );

wp_register_script(
'wp-plugins',
gutenberg_url( 'build/plugins/index.js' ),
Expand Down
85 changes: 85 additions & 0 deletions phpunit/class-core-block-theme-test.php
@@ -0,0 +1,85 @@
<?php
/**
* Core block theme tests.
*
* @package Gutenberg
*/

/**
* Test inclusion of opt-in core block theme.
*/
class Core_Block_Theme_Test extends WP_UnitTestCase {
private $old_wp_styles;
private $old_wp_scripts;

function setUp() {
parent::setUp();

$this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null;
remove_action( 'wp_default_scripts', 'wp_default_scripts' );

$GLOBALS['wp_scripts'] = new WP_Scripts();
$GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );

$this->old_wp_styles = isset( $GLOBALS['wp_styles'] ) ? $GLOBALS['wp_styles'] : null;
remove_action( 'wp_default_styles', 'wp_default_styles' );

$GLOBALS['wp_styles'] = new WP_Styles();
$GLOBALS['wp_styles']->default_version = get_bloginfo( 'version' );
}

function tearDown() {
$GLOBALS['wp_scripts'] = $this->old_wp_scripts;
add_action( 'wp_default_scripts', 'wp_default_scripts' );

$GLOBALS['wp_styles'] = $this->old_wp_styles;
add_action( 'wp_default_styles', 'wp_default_styles' );

if ( current_theme_supports( 'wp-block-styles' ) ) {
remove_theme_support( 'wp-block-styles' );
}

parent::tearDown();
}

function test_block_theme_in_editor_without_theme_support() {
Copy link
Member

Choose a reason for hiding this comment

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

I think you could add a comment to (at least) this test (and maybe the next) stating default block styles are always loaded for the editor and why. I know you've explained it elsewhere in this PR but that documentation won't be obvious to someone reading this test/just this file, so it bears repeating.

It's reasonably obvious what these tests are doing but not obvious why. I think a bit more explanation would make them clearer.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed. I'll make the update.

// Confirm we are without theme support by default.
$this->assertFalse( current_theme_supports( 'wp-block-styles' ) );

gutenberg_register_scripts_and_styles();

$this->assertFalse( wp_style_is( 'wp-core-blocks-theme' ) );
wp_enqueue_style( 'wp-edit-blocks' );
$this->assertTrue( wp_style_is( 'wp-core-blocks-theme' ) );
}

function test_block_theme_in_editor_with_theme_support() {
add_theme_support( 'wp-block-styles' );
gutenberg_register_scripts_and_styles();

$this->assertFalse( wp_style_is( 'wp-core-blocks-theme' ) );
wp_enqueue_style( 'wp-edit-blocks' );
$this->assertTrue( wp_style_is( 'wp-core-blocks-theme' ) );
}

function test_no_block_theme_on_front_end_without_theme_support() {
Copy link
Member

Choose a reason for hiding this comment

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

I'd rename this from test_no_block_theme_on_front_end_without_theme_support to test_no_block_styles_in_theme_without_theme_support or something like that. Same for the next test.

// Confirm we are without theme support by default.
$this->assertFalse( current_theme_supports( 'wp-block-styles' ) );

gutenberg_register_scripts_and_styles();

$this->assertFalse( wp_style_is( 'wp-core-blocks-theme' ) );
wp_enqueue_style( 'wp-core-blocks' );
$this->assertFalse( wp_style_is( 'wp-core-blocks-theme' ) );
}

function test_block_theme_on_front_end_with_theme_support() {
add_theme_support( 'wp-block-styles' );

gutenberg_register_scripts_and_styles();

$this->assertFalse( wp_style_is( 'wp-core-blocks-theme' ) );
wp_enqueue_style( 'wp-core-blocks' );
$this->assertTrue( wp_style_is( 'wp-core-blocks-theme' ) );
}
}
13 changes: 13 additions & 0 deletions webpack.config.js
Expand Up @@ -28,6 +28,11 @@ const blocksCSSPlugin = new ExtractTextPlugin( {
filename: './build/core-blocks/style.css',
} );

// CSS loader for default visual block styles.
const themeBlocksCSSPlugin = new ExtractTextPlugin( {
filename: './build/core-blocks/theme.css',
} );

// Configuration for the ExtractTextPlugin.
const extractConfig = {
use: [
Expand Down Expand Up @@ -237,6 +242,13 @@ const config = {
],
use: editBlocksCSSPlugin.extract( extractConfig ),
},
{
test: /\/theme\.s?css$/,
include: [
/core-blocks/,
],
use: themeBlocksCSSPlugin.extract( extractConfig ),
},
{
test: /\.s?css$/,
exclude: [
Expand All @@ -249,6 +261,7 @@ const config = {
plugins: [
blocksCSSPlugin,
editBlocksCSSPlugin,
themeBlocksCSSPlugin,
mainCSSExtractTextPlugin,
// Create RTL files with a -rtl suffix
new WebpackRTLPlugin( {
Expand Down