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

Border Panel: Move global styles forcing of split borders to relevant panel's onChange #49603

Merged
merged 4 commits into from
Apr 6, 2023
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 ) ) {
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
[ '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