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

[WIP] Design Tools: Add section based styling via theme.json #56234

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c3a7233
Draft generation of section styles from theme.json
aaronrobertshaw Nov 10, 2023
f0fc4e1
Move section block node generation to get_block_nodes
aaronrobertshaw Nov 14, 2023
7f54bb0
Add section class to blocks in the editor
aaronrobertshaw Nov 14, 2023
a2b97c2
Add section inspector controls group
aaronrobertshaw Nov 14, 2023
b21508f
Include section inspector controls group
aaronrobertshaw Nov 14, 2023
85fc9ac
Add overly simplistic control to set section attribute
aaronrobertshaw Nov 14, 2023
9b2a306
Hack at the Global Styles Output hook to get semi functional
aaronrobertshaw Nov 15, 2023
ed0146b
Fix section block element scoping
aaronrobertshaw Nov 15, 2023
efb279e
Try refactoring style node generation
aaronrobertshaw Nov 15, 2023
79b58ff
Try refactoring PHP theme.json block node generation
aaronrobertshaw Nov 16, 2023
196fe52
Fix useGlobalStylesOutput test
aaronrobertshaw Nov 16, 2023
d6640f5
Quick hack to add global styles data to block editor settings
aaronrobertshaw Nov 16, 2023
86bddfc
Replace text control to set section index on attributes
aaronrobertshaw Nov 16, 2023
8d6bf92
Revert "Replace text control to set section index on attributes"
aaronrobertshaw Nov 17, 2023
67f0dcc
Switch section styles to use SelectControl
aaronrobertshaw Nov 17, 2023
16fdbf1
Fix linting errors
aaronrobertshaw Nov 17, 2023
ac8ab80
Update stylesheet generation test to include section styles
aaronrobertshaw Nov 19, 2023
2127f17
Add some quick tests for section styles
aaronrobertshaw Nov 19, 2023
fd9b6de
Clean up section class application
aaronrobertshaw Nov 20, 2023
808a829
Try completing fallback logic for section class in the editor
aaronrobertshaw Nov 20, 2023
e282885
Revert "Try completing fallback logic for section class in the editor"
aaronrobertshaw Nov 20, 2023
122da37
Complete section class fallback in the editor
aaronrobertshaw Nov 20, 2023
cecdfd9
Fix allow list for section support in editor
aaronrobertshaw Nov 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ Gather blocks in a layout container. ([Source](https://github.com/WordPress/gute

- **Name:** core/group
- **Category:** design
- **Supports:** align (full, wide), anchor, ariaLabel, background (backgroundImage), color (background, button, gradients, heading, link, text), dimensions (minHeight), layout (allowSizingOnChildren), position (sticky), spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Supports:** align (full, wide), anchor, ariaLabel, background (backgroundImage), color (background, button, gradients, heading, link, text), dimensions (minHeight), layout (allowSizingOnChildren), position (sticky), section, spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** allowedBlocks, tagName, templateLock

## Heading
Expand Down
1 change: 1 addition & 0 deletions lib/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function gutenberg_get_block_editor_settings( $settings ) {

$settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() );

$settings['__experimentalStyles'] = gutenberg_get_global_styles();
$settings['__experimentalFeatures'] = gutenberg_get_global_settings();
// These settings may need to be updated based on data coming from theme.json sources.
if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) {
Expand Down
97 changes: 97 additions & 0 deletions lib/block-supports/sections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Section support to enable per-section styling via theme.json partials.
*
* @package gutenberg
*/

/**
* Get the section class name.
*
* If the block's section index isn't available, a fallback will be determined
* and the generated class name will reflect that.
*
* @param array $block Block object.
* @return string The section's class name.
*/
function gutenberg_get_section_class_name( $block ) {
if ( ! isset( $block['attrs']['section'] ) ) {
return null;
}

$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data();
$theme_json = $tree->get_raw_data();

// Confirm that a section at the specified index exists.
$section_count = count( $theme_json['styles']['sections'] ?? array() );

if ( ! $section_count ) {
return null;
}

$section_index = min( $block['attrs']['section'], $section_count - 1 );

return 'wp-section-' . $section_index;
}

/**
* Registers the section attribute, if needed.
*
* @param WP_Block_Type $block_type Block type.
*/
function gutenberg_register_section_support( $block_type ) {
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}

$has_section_support = block_has_support( $block_type, array( 'section' ), false );

if ( $has_section_support && ! array_key_exists( 'section', $block_type->attributes ) ) {
$block_type->attributes['section'] = array( 'type' => 'number' );
}
}

/**
* Update the block content with the section's class name.
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
*
* @return string Filtered block content.
*/
function gutenberg_render_section_support( $block_content, $block ) {
if ( ! $block_content || ! isset( $block['attrs']['section'] ) ) {
return $block_content;
}

// While section styling is being explored, the blocks allowed to support
// it will be restricted.
$allow_list = array( 'core/group' );

if ( ! in_array( $block['blockName'], $allow_list, true ) ) {
return $block_content;
}

// Apply the section's classname. Like the layout hook, this assumes the
// hook only applies to blocks with a single wrapper.
$tags = new WP_HTML_Tag_Processor( $block_content );
$section_class = gutenberg_get_section_class_name( $block );

if ( ! $section_class ) {
return $block_content;
}

if ( $tags->next_tag() ) {
$tags->add_class( $section_class );
}

return $tags->get_updated_html();
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'section',
array( 'register_attribute' => 'gutenberg_register_section_support' )
);

add_filter( 'render_block', 'gutenberg_render_section_support', 10, 2 );
Loading
Loading