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

Search Block: Add border radius support using CSS variable approach #27991

Closed
Closed
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
6 changes: 6 additions & 0 deletions docs/designers-developers/developers/themes/theme-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ h4 {
}
```

#### Border Properties

| Context | Radius |
| --- | --- |
| Search | Yes |

#### Color Properties

These are the current color properties supported by blocks:
Expand Down
1 change: 1 addition & 0 deletions lib/block-supports/border.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
if ( isset( $block_attributes['style']['border']['radius'] ) ) {
$border_radius = intval( $block_attributes['style']['border']['radius'] );
$styles[] = sprintf( 'border-radius: %dpx;', $border_radius );
$styles[] = sprintf( '--wp--style--border--radius: %dpx;', $border_radius );
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ class WP_Theme_JSON {
* - 'support': path to the block support in block.json.
*/
const PROPERTIES_METADATA = array(
'--wp--style--border--radius' => array(
'value' => array( 'border', 'radius' ),
'support' => array( '__experimentalBorder', 'radius' ),
),
'--wp--style--color--link' => array(
'value' => array( 'color', 'link' ),
'support' => array( 'color', 'link' ),
Expand Down
6 changes: 4 additions & 2 deletions packages/block-editor/src/hooks/border-radius.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ export function BorderRadiusEdit( props ) {
...style,
border: {
...style?.border,
radius: newRadius,
radius: newRadius && `${ newRadius }px`,
},
};

setAttributes( { style: cleanEmptyObject( newStyle ) } );
};

const value = style?.border?.radius.replace( 'px', '' );

return (
<RangeControl
value={ style?.border?.radius }
value={ value && parseInt( value ) }
label={ __( 'Border radius' ) }
min={ MIN_BORDER_RADIUS_VALUE }
max={ MAX_BORDER_RADIUS_VALUE }
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/test/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe( 'getInlineStyles', () => {
border: { radius: 10 },
} )
).toEqual( {
'--wp--style--border--radius': 10,
backgroundColor: 'black',
borderRadius: 10,
color: 'red',
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/search/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
},
"supports": {
"align": [ "left", "center", "right" ],
"html": false
"html": false,
"__experimentalBorder": {
"radius": true
}
},
"editorStyle": "wp-block-search-editor",
"style": "wp-block-search"
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/search/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

.wp-block-search__button {
height: auto;
border-radius: initial;

// This needs high specificity because it otherwise inherits styles from `components-button`.
// stylelint-disable-line no-duplicate-selectors
Expand Down
13 changes: 12 additions & 1 deletion packages/block-library/src/search/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
color: #32373c;
margin-left: 0.625em;
word-break: normal;
border-radius: var(--wp--style--border--radius);

&.has-icon {
line-height: 0;
Expand All @@ -23,6 +24,7 @@
flex: auto;
flex-wrap: nowrap;
max-width: 100%;
border-radius: var(--wp--style--border--radius);
}

.wp-block-search__label {
Expand All @@ -33,6 +35,7 @@
flex-grow: 1;
min-width: 3em;
border: 1px solid $gray-600;
border-radius: var(--wp--style--border--radius);
}

&.wp-block-search__button-only {
Expand All @@ -44,9 +47,14 @@
&.wp-block-search__button-inside .wp-block-search__inside-wrapper {
padding: $grid-unit-05;
border: 1px solid $gray-600;
// Determine what the minimum inner border radius should be. No outer radius equals 0, otherwise 1px.
// Must be 0px so max() can actually compare values later.
--wp--search--min-border-radius: clamp(0px, var(--wp--style--border--radius, 0px), 1px); /* stylelint-disable-line length-zero-no-unit */

.wp-block-search__input {
border-radius: 0;
// Adjust inner radius so it is visually consistent with outer radius. i.e. no "fat" corners.
// Maximum out of the outer radius minus padding or the minimum radius --wp--search--min-border-radius.
border-radius: max(var(--wp--style--border--radius, 0) - #{$grid-unit-05}, var(--wp--search--min-border-radius, 1px));
border: none;
padding: 0 0 0 0.25em;

Expand All @@ -56,6 +64,9 @@
}

.wp-block-search__button {
// Adjust inner radius so it is visually consistent with outer radius. i.e. no "fat" corners.
// Maximum out of the outer radius minus padding or the minimum radius --wp--search--min-border-radius.
border-radius: max(var(--wp--style--border--radius, 0) - #{$grid-unit-05}, var(--wp--search--min-border-radius, 1px));
padding: 0.125em 0.5em;
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/blocks/src/api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const DEPRECATED_ENTRY_KEYS = [
];

export const __EXPERIMENTAL_STYLE_PROPERTY = {
'--wp--style--border--radius': {
value: [ 'border', 'radius' ],
support: [ '__experimentalBorder', 'radius' ],
},
'--wp--style--color--link': {
value: [ 'color', 'link' ],
support: [ 'color', 'link' ],
Expand Down