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

Make global styles shape consistent with local styles shape. #22744

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 23 additions & 9 deletions docs/designers-developers/developers/themes/theme-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,32 +191,46 @@ This is being implemented, so it's not currently available.
},
core/paragraph: {
styles: {
line-height: <value>,
font-size: <value>,
color: <value>,
typography: {
lineHeight: <value>,
fontSize: <value>
},
color: {
text: <value>
}
}
},
/* core/heading/h1, core/heading/h2, etc */
core/heading/h*: {
styles: {
line-height: <value>,
font-size: <value>,
color: <value>,
typography: {
lineHeight: <value>,
fontSize: <value>
},
color: {
text: <value>
}
}
},
core/columns: {
styles: {
color: <value>,
color: {
text: <value>
}
}
},
core/group: {
styles: {
color: <value>,
color: {
text: <value>
}
}
},
core/media-text: {
styles: {
color: <value>,
color: {
text: <value>
}
}
},
}
Expand Down
34 changes: 33 additions & 1 deletion lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,35 @@ function gutenberg_experimental_global_styles_get_block_data() {
return $block_data;
}

/**
* Given an array contain the styles shape returns the css for this styles.
* A similar function exists on the client at /packages/block-editor/src/hooks/style.js.
*
* @param array $styles Array containing the styles shape from global styles.
*
* @return array Containing a set of css rules.
*/
function gutenberg_experimental_global_styles_flatten_styles_tree( $styles ) {
$mappings = array(
'line-height' => array( 'typography', 'lineHeight' ),
'font-size' => array( 'typography', 'fontSize' ),
'background' => array( 'color', 'gradient' ),
'background-color' => array( 'color', 'background' ),
'color' => array( 'color', 'text' ),
);

$result = array();

foreach ( $mappings as $key => $path ) {
$value = gutenberg_experimental_get( $styles, $path );
if ( null !== $value ) {
$result[ $key ] = $value;
}
}
return $result;

}

/**
* Takes a tree adhering to the theme.json schema and generates
* the corresponding stylesheet.
Expand Down Expand Up @@ -352,7 +381,10 @@ function gutenberg_experimental_global_styles_resolver( $tree ) {
$stylesheet .= gutenberg_experimental_global_styles_resolver_styles(
$block_data[ $block_name ]['selector'],
$block_data[ $block_name ]['supports'],
array_merge( $tree[ $block_name ]['styles'], $css_variables )
array_merge(
gutenberg_experimental_global_styles_flatten_styles_tree( $tree[ $block_name ]['styles'] ),
$css_variables
)
);
}
return $stylesheet;
Expand Down
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function gutenberg_is_experiment_enabled( $name ) {
}

require dirname( __FILE__ ) . '/compat.php';
require dirname( __FILE__ ) . '/utils.php';

require dirname( __FILE__ ) . '/blocks.php';
require dirname( __FILE__ ) . '/templates.php';
Expand Down
27 changes: 27 additions & 0 deletions lib/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* General utilities.
*
* @package gutenberg
*/

/**
* This function allows to easily access a part from a php array.
* It is equivalent to want lodash get provides for JavaScript and is useful to have something similar
* in php so functions that do the same thing on the client and sever can have identical code.
*
* @param array $array An array from where we want to retrieve some information from.
* @param array $path An array containing the path we want to retrieve.
*
* @return array Containing a set of css rules.
*/
function gutenberg_experimental_get( $array, $path ) {
$path_length = count( $path );
for ( $i = 0; $i < $path_length; ++$i ) {
if ( empty( $array[ $path[ $i ] ] ) ) {
return null;
}
$array = $array[ $path[ $i ] ];
}
return $array;
}