Skip to content

Commit e121644

Browse files
Allow for combining width and style in border panel menu
1 parent 3475693 commit e121644

File tree

1 file changed

+88
-22
lines changed

1 file changed

+88
-22
lines changed

packages/block-editor/src/hooks/border.js

Lines changed: 88 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
* WordPress dependencies
33
*/
44
import { getBlockSupport } from '@wordpress/blocks';
5-
import { __experimentalToolsPanelItem as ToolsPanelItem } from '@wordpress/components';
5+
import {
6+
__experimentalGrid as Grid,
7+
__experimentalToolsPanelItem as ToolsPanelItem,
8+
} from '@wordpress/components';
69
import { Platform } from '@wordpress/element';
710
import { __ } from '@wordpress/i18n';
811

@@ -77,32 +80,74 @@ export function BorderPanel( props ) {
7780
},
7881
} );
7982

83+
const getWidthAndStyleLabel = () => {
84+
if ( isWidthSupported && isStyleSupported ) {
85+
return __( 'Width & Style' );
86+
}
87+
88+
return isWidthSupported ? __( 'Width' ) : __( 'Style' );
89+
};
90+
91+
const getWidthAndStyleFilter = () => {
92+
if ( isWidthSupported && isStyleSupported ) {
93+
return ( newAttributes ) => ( {
94+
...newAttributes,
95+
style: {
96+
...newAttributes.style,
97+
border: {
98+
...newAttributes.style?.border,
99+
width: undefined,
100+
style: undefined,
101+
},
102+
},
103+
} );
104+
}
105+
106+
return isWidthSupported
107+
? createResetAllFilter( 'width' )
108+
: createResetAllFilter( 'style' );
109+
};
110+
111+
const hasWidthAndStyleValue = () => {
112+
if ( isWidthSupported && isStyleSupported ) {
113+
return hasBorderWidthValue( props ) || hasBorderStyleValue( props );
114+
}
115+
116+
return isWidthSupported
117+
? hasBorderWidthValue( props )
118+
: hasBorderStyleValue( props );
119+
};
120+
121+
const isWidthAndStyleDefault =
122+
defaultBorderControls?.widthAndStyle ||
123+
( isWidthSupported && defaultBorderControls?.width ) ||
124+
( isStyleSupported && defaultBorderControls?.style );
125+
126+
const createResetWidthAndStyle = () => () => {
127+
if ( isWidthSupported && isStyleSupported ) {
128+
return resetBorderWidthAndStyle( props );
129+
}
130+
131+
return isWidthSupported
132+
? resetBorderWidth( props )
133+
: resetBorderStyle( props );
134+
};
135+
80136
return (
81137
<InspectorControls __experimentalGroup="border">
82-
{ isWidthSupported && (
138+
{ ( isWidthSupported || isStyleSupported ) && (
83139
<ToolsPanelItem
84-
className="single-column"
85-
hasValue={ () => hasBorderWidthValue( props ) }
86-
label={ __( 'Width' ) }
87-
onDeselect={ () => resetBorderWidth( props ) }
88-
isShownByDefault={ defaultBorderControls?.width }
89-
resetAllFilter={ createResetAllFilter( 'width' ) }
140+
hasValue={ () => hasWidthAndStyleValue() }
141+
label={ getWidthAndStyleLabel() }
142+
onDeselect={ createResetWidthAndStyle() }
143+
isShownByDefault={ isWidthAndStyleDefault }
144+
resetAllFilter={ getWidthAndStyleFilter() }
90145
panelId={ clientId }
91146
>
92-
<BorderWidthEdit { ...props } />
93-
</ToolsPanelItem>
94-
) }
95-
{ isStyleSupported && (
96-
<ToolsPanelItem
97-
className="single-column"
98-
hasValue={ () => hasBorderStyleValue( props ) }
99-
label={ __( 'Style' ) }
100-
onDeselect={ () => resetBorderStyle( props ) }
101-
isShownByDefault={ defaultBorderControls?.style }
102-
resetAllFilter={ createResetAllFilter( 'style' ) }
103-
panelId={ clientId }
104-
>
105-
<BorderStyleEdit { ...props } />
147+
<Grid gap="4">
148+
{ isWidthSupported && <BorderWidthEdit { ...props } /> }
149+
{ isStyleSupported && <BorderStyleEdit { ...props } /> }
150+
</Grid>
106151
</ToolsPanelItem>
107152
) }
108153
{ isColorSupported && (
@@ -213,3 +258,24 @@ export function removeBorderAttribute( style, attribute ) {
213258
},
214259
} );
215260
}
261+
262+
/**
263+
* Resets both the border width and style block support attributes
264+
*
265+
* @param {Object} props Block props.
266+
* @param {Object} props.attributes Block's attributes.
267+
* @param {Object} props.setAttributes Function to set block's attributes.
268+
*/
269+
export function resetBorderWidthAndStyle( { attributes = {}, setAttributes } ) {
270+
const { style } = attributes;
271+
setAttributes( {
272+
style: cleanEmptyObject( {
273+
...style,
274+
border: {
275+
...style?.border,
276+
width: undefined,
277+
style: undefined,
278+
},
279+
} ),
280+
} );
281+
}

0 commit comments

Comments
 (0)