|
2 | 2 | * WordPress dependencies |
3 | 3 | */ |
4 | 4 | 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'; |
6 | 9 | import { Platform } from '@wordpress/element'; |
7 | 10 | import { __ } from '@wordpress/i18n'; |
8 | 11 |
|
@@ -77,32 +80,74 @@ export function BorderPanel( props ) { |
77 | 80 | }, |
78 | 81 | } ); |
79 | 82 |
|
| 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 | + |
80 | 136 | return ( |
81 | 137 | <InspectorControls __experimentalGroup="border"> |
82 | | - { isWidthSupported && ( |
| 138 | + { ( isWidthSupported || isStyleSupported ) && ( |
83 | 139 | <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() } |
90 | 145 | panelId={ clientId } |
91 | 146 | > |
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> |
106 | 151 | </ToolsPanelItem> |
107 | 152 | ) } |
108 | 153 | { isColorSupported && ( |
@@ -213,3 +258,24 @@ export function removeBorderAttribute( style, attribute ) { |
213 | 258 | }, |
214 | 259 | } ); |
215 | 260 | } |
| 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