Skip to content

Commit

Permalink
Switch to only opting in for navigation block
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Oct 30, 2020
1 parent 23967b1 commit c35f246
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
56 changes: 47 additions & 9 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@ function gutenberg_register_typography_support( $block_type ) {
* @return array Font size CSS classes and inline styles.
*/
function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$has_font_size_support = false;
$classes = array();
$styles = array();
if ( property_exists( $block_type, 'supports' ) ) {
$has_font_size_support = gutenberg_experimental_get( $block_type->supports, array( 'fontSize' ), false );
if ( ! property_exists( $block_type, 'supports' ) ) {
return $attributes;
}

$has_line_height_support = false;
if ( property_exists( $block_type, 'supports' ) ) {
$has_line_height_support = gutenberg_experimental_get( $block_type->supports, array( 'lineHeight' ), false );
}
$classes = array();
$styles = array();

$has_font_size_support = gutenberg_experimental_get( $block_type->supports, array( 'fontSize' ), false );
$has_line_height_support = gutenberg_experimental_get( $block_type->supports, array( 'lineHeight' ), false );
$has_text_transform_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalTextTransform' ), false );

// Font Size.
if ( $has_font_size_support ) {
Expand All @@ -87,6 +86,14 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
}
}

// Text Transform.
if ( $has_text_transform_support ) {
$text_transform_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'textTransform', 'text-transform' );
if ( $text_transform_style ) {
$styles[] = $text_transform_style;
}
}

$attributes = array();
if ( ! empty( $classes ) ) {
$attributes['class'] = implode( ' ', $classes );
Expand All @@ -106,3 +113,34 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
'apply' => 'gutenberg_apply_typography_support',
)
);

/**
* Generates an inline style for a typography feature e.g. text decoration,
* text transform, and font style.
*
* @param array $attributes Block's attributes.
* @param string $feature Key for the feature within the typography styles.
* @param string $css_property Slug for the CSS property the inline style sets.
*
* @return string CSS inline style.
*/
function gutenberg_typography_get_css_variable_inline_style( $attributes, $feature, $css_property ) {
// Retrieve current attribute value or skip if not found.
$style_value = gutenberg_experimental_get( $attributes, array( 'style', 'typography', $feature ), false );
if ( ! $style_value ) {
return;
}

// If we don't have a preset CSS variable, we'll assume it's a regular CSS value.
if ( strpos( $style_value, "var:preset|{$css_property}|" ) === false ) {
return sprintf( '%s:%s;', $css_property, $style_value );
}

// We have a preset CSS variable as the style.
// Get the style value from the string and return CSS style.
$index_to_splice = strrpos( $style_value, '|' ) + 1;
$slug = substr( $style_value, $index_to_splice );

// Return the actual CSS inline style e.g. `text-decoration:var(--wp--preset--text-decoration--underline);`.
return sprintf( '%s:var(--wp--preset--%s--%s);', $css_property, $css_property, $slug );
}
1 change: 0 additions & 1 deletion packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
},
"fontSize": true,
"lineHeight": true,
"__experimentalTextTransform": true,
"__experimentalSelector": {
"core/heading/h1": "h1",
"core/heading/h2": "h2",
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"html": false,
"inserter": true,
"fontSize": true,
"__experimentalTextTransform": true,
"color": {
"textColor": true,
"backgroundColor": true
Expand Down

0 comments on commit c35f246

Please sign in to comment.