Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -186,39 +186,20 @@ export default function BorderPanel( {
const onBorderChange = ( newBorder ) => {
// Ensure we have a visible border style when a border width or
// color is being selected.
const newBorderWithStyle = applyAllFallbackStyles( newBorder );
const updatedBorder = applyAllFallbackStyles( newBorder );

// As we can't conditionally generate styles based on if other
// style properties have been set we need to force split border
// definitions for user set border styles. Border radius is derived
// from the same property i.e. `border.radius` if it is a string
// that is used. The longhand border radii styles are only generated
// if that property is an object.
//
// For borders (color, style, and width) those are all properties on
// the `border` style property. This means if the theme.json defined
// split borders and the user condenses them into a flat border or
// vice-versa we'd get both sets of styles which would conflict.
const updatedBorder = ! hasSplitBorders( newBorderWithStyle )
? {
top: newBorderWithStyle,
right: newBorderWithStyle,
bottom: newBorderWithStyle,
left: newBorderWithStyle,
}
: {
color: null,
style: null,
width: null,
...newBorderWithStyle,
};

[ 'top', 'right', 'bottom', 'left' ].forEach( ( side ) => {
updatedBorder[ side ] = {
...updatedBorder[ side ],
color: encodeColorValue( updatedBorder[ side ]?.color ),
};
} );
if ( hasSplitBorders( updatedBorder ) ) {
[ 'top', 'right', 'bottom', 'left' ].forEach( ( side ) => {
if ( updatedBorder[ side ] ) {
updatedBorder[ side ] = {
...updatedBorder[ side ],
color: encodeColorValue( updatedBorder[ side ]?.color ),
};
}
} );
} else if ( updatedBorder ) {
updatedBorder.color = encodeColorValue( updatedBorder.color );
}

// As radius is maintained separately to color, style, and width
// maintain its value. Undefined values here will be cleaned when
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/border.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function styleToAttributes( style ) {

const borderColorValue = style?.border?.color;
const borderColorSlug = borderColorValue?.startsWith( 'var:preset|color|' )
? borderColorSlug.substring( 'var:preset|color|'.length )
? borderColorValue.substring( 'var:preset|color|'.length )
: undefined;
const updatedStyle = { ...style };
updatedStyle.border = {
Expand Down
33 changes: 32 additions & 1 deletion packages/edit-site/src/components/global-styles/border-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { __experimentalHasSplitBorders as hasSplitBorders } from '@wordpress/components';

/**
* Internal dependencies
Expand Down Expand Up @@ -29,11 +30,41 @@ export default function BorderPanel( { name, variation = '' } ) {
const [ rawSettings ] = useGlobalSetting( '', name );
const settings = useSettingsForBlockElement( rawSettings, name );

const onChange = ( newStyle ) => {
// As Global Styles can't conditionally generate styles based on if
// other style properties have been set, we need to force split
// border definitions for user set global border styles. Border
// radius is derived from the same property i.e. `border.radius` if
// it is a string that is used. The longhand border radii styles are
// only generated if that property is an object.
//
// For borders (color, style, and width) those are all properties on
// the `border` style property. This means if the theme.json defined
// split borders and the user condenses them into a flat border or
// vice-versa we'd get both sets of styles which would conflict.
const { border } = newStyle;
const updatedBorder = ! hasSplitBorders( border )
? {
top: border,
right: border,
bottom: border,
left: border,
}
: {
color: null,
style: null,
width: null,
...border,
};

setStyle( { ...newStyle, border: updatedBorder } );
};

return (
<StylesBorderPanel
inheritedValue={ inheritedStyle }
value={ style }
onChange={ setStyle }
onChange={ onChange }
settings={ settings }
/>
);
Expand Down