-
Notifications
You must be signed in to change notification settings - Fork 808
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
Add/button block border support #41147
base: trunk
Are you sure you want to change the base?
Changes from 4 commits
4aff99a
f1393dd
14d93ac
a18d0de
fb6550a
1b98857
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: enhancement | ||
|
||
Added support for border width, style and color to Button block. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,6 +171,12 @@ | |
$has_typography_styles = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] ); | ||
$has_custom_font_size = $has_typography_styles && array_key_exists( 'fontSize', $attributes['style']['typography'] ); | ||
$has_custom_text_transform = $has_typography_styles && array_key_exists( 'textTransform', $attributes['style']['typography'] ); | ||
$border_styles = array(); | ||
$has_named_border_color = array_key_exists( 'borderColor', $attributes ); | ||
$has_custom_border_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'border', $attributes['style'] ) && array_key_exists( 'color', $attributes['style']['border'] ); | ||
$has_border_style = array_key_exists( 'style', $attributes ) && array_key_exists( 'border', $attributes['style'] ) && array_key_exists( 'style', $attributes['style']['border'] ); | ||
$has_border_width = array_key_exists( 'style', $attributes ) && array_key_exists( 'border', $attributes['style'] ) && array_key_exists( 'width', $attributes['style']['border'] ); | ||
$has_individual_borders = array_key_exists( 'style', $attributes ) && array_key_exists( 'border', $attributes['style'] ) && ( array_key_exists( 'top', $attributes['style']['border'] ) || array_key_exists( 'right', $attributes['style']['border'] ) || array_key_exists( 'bottom', $attributes['style']['border'] ) || array_key_exists( 'left', $attributes['style']['border'] ) ); | ||
|
||
if ( $has_font_family ) { | ||
$styles[] = sprintf( 'font-family: %s;', $attributes['fontFamily'] ); | ||
|
@@ -211,6 +217,36 @@ | |
$styles[] = 'max-width: 100%'; | ||
} | ||
|
||
if ( $has_named_border_color ) { | ||
$border_styles['color'] = "var:preset|color|{$attributes['borderColor']}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be working, although looking at the style engine output, I think we'll have to ensure the classname is output on the front end instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I've fixed it to output classname instead of styles. |
||
} elseif ( $has_custom_border_color ) { | ||
$border_styles['color'] = $attributes['style']['border']['color']; | ||
} | ||
|
||
if ( $has_border_style ) { | ||
$border_styles['style'] = $attributes['style']['border']['style']; | ||
} | ||
|
||
if ( $has_border_width ) { | ||
$border_styles['width'] = $attributes['style']['border']['width']; | ||
} | ||
|
||
if ( $has_individual_borders ) { | ||
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { | ||
$border = $attributes['style']['border'][ $side ] ?? null; | ||
$border_side_values = array( | ||
'width' => isset( $border['width'] ) ? $border['width'] : null, | ||
Check failure on line 238 in projects/plugins/jetpack/extensions/blocks/button/button.php GitHub Actions / Static analysis
|
||
'color' => isset( $border['color'] ) ? $border['color'] : null, | ||
Check failure on line 239 in projects/plugins/jetpack/extensions/blocks/button/button.php GitHub Actions / Static analysis
|
||
'style' => isset( $border['style'] ) ? $border['style'] : null, | ||
Check failure on line 240 in projects/plugins/jetpack/extensions/blocks/button/button.php GitHub Actions / Static analysis
|
||
); | ||
$border_styles[ $side ] = $border_side_values; | ||
} | ||
} | ||
|
||
if ( isset( wp_style_engine_get_styles( array( 'border' => $border_styles ) )['css'] ) ) { | ||
$styles[] = wp_style_engine_get_styles( array( 'border' => $border_styles ) )['css']; | ||
} | ||
|
||
return implode( ' ', $styles ); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The border block support also allows "per-side" or split borders e.g. border-top, border-left etc.
The style engine's get styles function will convert a style object to the appropriate CSS classes and inline styles. Here's core's version, wp_style_engine_get_styles, and Gutenberg's.
An example of its use can be found in the border block support linked above. The results are effectively the same as those on the JS side via
useBorderProps
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated to add styles for each individual side, based on the Gutenberg border supports you linked above.