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

Global Styles: Add back the global styles logic that forces the solid border when color or width applied. #50498

Merged
merged 3 commits into from
May 11, 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
114 changes: 0 additions & 114 deletions packages/edit-site/src/components/global-styles/border-panel.js

This file was deleted.

77 changes: 74 additions & 3 deletions packages/edit-site/src/components/global-styles/screen-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { store as coreStore } from '@wordpress/core-data';
import {
PanelBody,
__experimentalVStack as VStack,
__experimentalHasSplitBorders as hasSplitBorders,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';

Expand All @@ -21,6 +22,41 @@ import { unlock } from '../../private-apis';
import Subtitle from './subtitle';
import { useBlockVariations, VariationsPanel } from './variations-panel';

function applyFallbackStyle( border ) {
if ( ! border ) {
return border;
}

const hasColorOrWidth = border.color || border.width;

if ( ! border.style && hasColorOrWidth ) {
return { ...border, style: 'solid' };
}

if ( border.style && ! hasColorOrWidth ) {
return undefined;
}

return border;
}

function applyAllFallbackStyles( border ) {
if ( ! border ) {
return border;
}

if ( hasSplitBorders( border ) ) {
return {
top: applyFallbackStyle( border.top ),
right: applyFallbackStyle( border.right ),
bottom: applyFallbackStyle( border.bottom ),
left: applyFallbackStyle( border.left ),
};
}

return applyFallbackStyle( border );
}

const {
useHasDimensionsPanel,
useHasTypographyPanel,
Expand Down Expand Up @@ -108,6 +144,41 @@ function ScreenBlock( { name, variation } ) {
} );
}
};
const onChangeBorders = ( newStyle ) => {
if ( ! newStyle?.border ) {
setStyle( newStyle );
return;
}

// 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 { radius, ...newBorder } = newStyle.border;
const border = applyAllFallbackStyles( newBorder );
const updatedBorder = ! hasSplitBorders( border )
? {
top: border,
right: border,
bottom: border,
left: border,
}
: {
color: null,
style: null,
width: null,
...border,
};

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

return (
<>
Expand Down Expand Up @@ -152,15 +223,15 @@ function ScreenBlock( { name, variation } ) {
<StylesBorderPanel
inheritedValue={ inheritedStyle }
value={ style }
onChange={ setStyle }
onChange={ onChangeBorders }
settings={ settings }
/>
) }
{ hasEffectsPanel && (
<StylesEffectsPanel
inheritedValue={ inheritedStyleWithLayout }
value={ styleWithLayout }
onChange={ onChangeDimensions }
onChange={ setStyle }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrelated change but it's a small optim/typo fix (same for the next change)

settings={ settings }
includeLayoutControls
/>
Expand All @@ -169,7 +240,7 @@ function ScreenBlock( { name, variation } ) {
<StylesFiltersPanel
inheritedValue={ inheritedStyleWithLayout }
value={ styleWithLayout }
onChange={ onChangeDimensions }
onChange={ setStyle }
settings={ {
...settings,
color: {
Expand Down